如何在 python 中打开预训练模型
How to open pretrained models in python
您好,我正在尝试从 .sav
文件加载一些预训练模型,但到目前为止没有任何效果。这些模型最初是在 pytorch 中制作的,当我在 vs-code 中打开原始文件时,我可以看到所有适当的信息都已正确存储。
我试过以下库:
sklearn.externals.joblib
pickle
scipy.io
pyreadstat
每个库要么给我一个错误(例如 wrong timestamp
或 signature mismatch
),要么只是 return 一个 int
而不是 python 对象。
可以从 this link 下载模型。
您需要使用 PyTorch 加载模型。除此之外,您还需要原始模型定义,因此您需要克隆作者存储库。在您的示例中,此回购协议:
git clone https://github.com/tbepler/protein-sequence-embedding-iclr2019.git
然后你可以用torch.load()
打开模型。请注意,您需要在路径中定义模型(您可以简单地从 repo 目录启动 python)。
然后直接打开一个文件:
import torch
model = torch.load('<downloaded models>/<model name>.sav')
print(model)
最后一行打印模型定义。例如,me_L1_100d_lstm3x512_lm_i512_mb64_tau0.5_p0.05_epoch100.sav
产生以下输出:
OrdinalRegression(
(embedding): StackedRNN(
(embed): LMEmbed(
(lm): BiLM(
(embed): Embedding(22, 21, padding_idx=21)
(dropout): Dropout(p=0)
(rnn): ModuleList(
(0): LSTM(21, 1024, batch_first=True)
(1): LSTM(1024, 1024, batch_first=True)
)
(linear): Linear(in_features=1024, out_features=21, bias=True)
)
(embed): Embedding(21, 512, padding_idx=20)
(proj): Linear(in_features=4096, out_features=512, bias=True)
(transform): ReLU()
)
(dropout): Dropout(p=0)
(rnn): LSTM(512, 512, num_layers=3, batch_first=True, bidirectional=True)
(proj): Linear(in_features=1024, out_features=100, bias=True)
)
(compare): L1()
)
您好,我正在尝试从 .sav
文件加载一些预训练模型,但到目前为止没有任何效果。这些模型最初是在 pytorch 中制作的,当我在 vs-code 中打开原始文件时,我可以看到所有适当的信息都已正确存储。
我试过以下库:
sklearn.externals.joblib
pickle
scipy.io
pyreadstat
每个库要么给我一个错误(例如 wrong timestamp
或 signature mismatch
),要么只是 return 一个 int
而不是 python 对象。
可以从 this link 下载模型。
您需要使用 PyTorch 加载模型。除此之外,您还需要原始模型定义,因此您需要克隆作者存储库。在您的示例中,此回购协议:
git clone https://github.com/tbepler/protein-sequence-embedding-iclr2019.git
然后你可以用torch.load()
打开模型。请注意,您需要在路径中定义模型(您可以简单地从 repo 目录启动 python)。
然后直接打开一个文件:
import torch
model = torch.load('<downloaded models>/<model name>.sav')
print(model)
最后一行打印模型定义。例如,me_L1_100d_lstm3x512_lm_i512_mb64_tau0.5_p0.05_epoch100.sav
产生以下输出:
OrdinalRegression(
(embedding): StackedRNN(
(embed): LMEmbed(
(lm): BiLM(
(embed): Embedding(22, 21, padding_idx=21)
(dropout): Dropout(p=0)
(rnn): ModuleList(
(0): LSTM(21, 1024, batch_first=True)
(1): LSTM(1024, 1024, batch_first=True)
)
(linear): Linear(in_features=1024, out_features=21, bias=True)
)
(embed): Embedding(21, 512, padding_idx=20)
(proj): Linear(in_features=4096, out_features=512, bias=True)
(transform): ReLU()
)
(dropout): Dropout(p=0)
(rnn): LSTM(512, 512, num_layers=3, batch_first=True, bidirectional=True)
(proj): Linear(in_features=1024, out_features=100, bias=True)
)
(compare): L1()
)