How to open pretrained models in python
Hi I'm trying to load some pretrained models from .sav files and so far nothing is working. The models were originally made in pytorch and when I open the raw file in vs-code I can see that all the appropiate information was stored correctly.

I've tried the following libraries:

sklearn.externals.joblib

pickle

scipy.io

pyreadstat

Each library either gave me an error (such as wrong timestamp or signature mismatch) or just return an int instead of a python object.

The models can be downloaded from this link.

You need to use PyTorch to load the models. On top of this, you also need the original model definition, so you need to need the clone the authors repository. In your example this repo:

git clone https://github.com/tbepler/protein-sequence-embedding-iclr2019.git


Then you can open the model with torch.load(). Note that you need the model definition on your path (you can simply launch python from the repo directory).

Then it's straightforward to open a file: 

import torch
model = torch.load('&lt;downloaded models&gt;/&lt;model name&gt;.sav')
print(model)


The last line prints the model definition. For example,  me_L1_100d_lstm3x512_lm_i512_mb64_tau0.5_p0.05_epoch100.sav produced the following output:

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()
)

