UnpicklingError:无效的加载密钥'\ x0a'

时间:2019-01-09 20:37:32

标签: python-3.x neural-network pytorch

model_save_name = 'classifier.pt'
path = F"/content/gdrive/My Drive/Others/{model_save_name}"

model.load_state_dict(torch.load(path), strict = False)

试图从路径加载到模型state_dict中。稍后,相同的模型state_dict将用于保存检查点,例如torch.save(model.state_dict(), path)

错误回溯:

UnpicklingError                           Traceback (most recent call last)
<ipython-input-69-7cb7940c24e3> in <module>()
----> 1 model.load_state_dict(torch.load(path), strict = False)

/usr/local/lib/python3.6/dist-packages/torch/serialization.py in load(f, map_location, pickle_module)
    356         f = open(f, 'rb')
    357     try:
--> 358         return _load(f, map_location, pickle_module)
    359     finally:
    360         if new_fd:

/usr/local/lib/python3.6/dist-packages/torch/serialization.py in _load(f, map_location, pickle_module)
    530             f.seek(0)
    531 
--> 532     magic_number = pickle_module.load(f)
    533     if magic_number != MAGIC_NUMBER:
    534         raise RuntimeError("Invalid magic number; corrupt file?")

UnpicklingError: invalid load key, '\x0a'.

1 个答案:

答案 0 :(得分:-1)

是否使用与尝试加载的Python版本相同的python版本保存了模型。如果不是,则各个版本中的pickle会有一些更改。另外,如果不是这种情况,您还可以发布正在发出的torch.save()调用。