代码之家  ›  专栏  ›  技术社区  ›  Milo Lu

如何使用python3读取CKPT文件,而使用python2_¼_保存该文件

  •  0
  • Milo Lu  · 技术社区  · 7 年前

    我试图用pytorch读取检查点文件

    checkpoint = torch.load(xxx.ckpt)
    

    该文件由使用python 2.7编写的程序生成。我尝试使用python 3.6读取文件,但得到以下错误

    UnicodeDecodeError: 'ascii' codec can't decode byte 0x8c in position 16: ordinal not in range(128)
    

    是否可以在不降级python的情况下读取文件?

    2 回复  |  直到 6 年前
        1
  •  1
  •   iacolippo    7 年前

    pickle

    torch.save(filename, model.state_dict())
    

    model

    model = Model() # construct a new model
    model.load_state_dict(torch.load(filename))
    

    np.savez

    torch.load tell it to decode ASCII strings to Python3 strings

        2
  •  0
  •   Milo Lu    6 年前

    pytorch pickle

    checkpoint = torch.load("xxx.ckpt")
    with open("xxx.pkl", "wb") as outfile:
        pickle.dump(checkpointfile, outfile)
    

    pkl_file = open("xxx.pkl", "rb")
    data = pickle.load(pkl_file, encoding="latin1")
    torch.save(data, "xxx.ckpt")