我正在嘗試將 pytorch 模型的權重保存到 .txt 或 .json 中。將其寫入 .txt 時,
#import torch
model = torch.load("model_path")
string = str(model)
with open('some_file.txt', 'w') as fp:
fp.write(string)
我得到一個沒有保存所有權重的檔案,即整個文本檔案都有省略號。我無法將其寫入 JSON,因為該模型具有不可序列化 JSON 的張量 [除非有一種我不知道的方法?] 如何將 .pth 檔案中的權重保存為某種格式,以便沒有資訊丟失了,很容易被看到嗎?
謝謝
uj5u.com熱心網友回復:
當你在做的時候str(model.state_dict()),它遞回地使用str它包含的元素的方法。所以問題是如何構建單個元素字串表示。您應該增加以單個字串表示形式列印的行數限制:
torch.set_printoptions(profile="full")
查看與此的區別:
import torch
import torchvision.models as models
mobilenet_v2 = models.mobilenet_v2()
torch.set_printoptions(profile="default")
print(mobilenet_v2.state_dict()['features.2.conv.0.0.weight'])
torch.set_printoptions(profile="full")
print(mobilenet_v2.state_dict()['features.2.conv.0.0.weight'])
張量目前不是 JSON 可序列化的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/475294.html
上一篇:梯度下降演算法中的偏導項
下一篇:擬合-變換
