How does one load a sequential model from a string in Pytorch?

I have string of a sequential model saved in a file:

str(base_model)
Out[2]: 'Sequential(\n  (fc1;l1): Linear(in_features=1, out_features=15, bias=True)\n  (bn1;l1): BatchNorm1d(15, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False)\n  (relu1): ReLU()\n  (fc2;l1): Linear(in_features=15, out_features=15, bias=True)\n  (bn2;l1): BatchNorm1d(15, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False)\n  (relu2): ReLU()\n  (fc3;l1): Linear(in_features=15, out_features=15, bias=True)\n  (bn3;l1): BatchNorm1d(15, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False)\n  (relu3): ReLU()\n  (fc4;final;l2): Linear(in_features=15, out_features=1, bias=True)\n)'

and I also have the weights somewhere else where I can easily load as a dict with the standard:

# https://pytorch.org/tutorials/beginner/saving_loading_models.html

# Saving & Loading Model for Inference
# Save/Load state_dict (Recommended)
# Save:
torch.save(model.state_dict(), PATH)

# Load:
model = TheModelClass(*args, **kwargs)
model.load_state_dict(torch.load(PATH))
model.eval()

Thus, how do I create an actual python object from the sequential string?

I tried using the (probably dangerous eval function) but it didn’t work since the string isn’t valid python.

base_model
Python 3.8.2 (default, Mar 26 2020, 10:43:30) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.1 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 7.16.1
Out[1]: 
Sequential(
  (fc1_l1): Linear(in_features=1, out_features=15, bias=True)
  (bn1_l1): BatchNorm1d(15, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False)
  (relu1): ReLU()
  (fc2_l1): Linear(in_features=15, out_features=15, bias=True)
  (bn2_l1): BatchNorm1d(15, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False)
  (relu2): ReLU()
  (fc3_l1): Linear(in_features=15, out_features=15, bias=True)
  (bn3_l1): BatchNorm1d(15, eps=1e-05, momentum=0.1, affine=True, track_running_stats=False)
  (relu3): ReLU()
  (fc4_final_l2): Linear(in_features=15, out_features=1, bias=True)
)
eval(str(base_model))
Traceback (most recent call last):
  File "/Users/brando/anaconda3/envs/my-env/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3343, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-c53f3ae3ac55>", line 1, in <module>
    eval(str(base_model))
  File "<string>", line 2
    (fc1_l1): Linear(in_features=1, out_features=15, bias=True)
            ^
SyntaxError: invalid syntax

After trying that I assumed that most likely there is a standard way to do that isn’t this hacky.

Is it possible to do it?


crossposted: