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

username: Brando_Miranda
cooked: <p>I have string of a sequential model saved in a file:</p>
<pre><code class="lang-auto">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)'
</code></pre>
<p>and I also have the weights somewhere else where I can easily load as a dict with the standard:</p>
<pre><code class="lang-auto"># https://pytorch.org/tutorials/beginner/saving_loading_models.html

# Saving &amp; 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()
</code></pre>
<p>Thus, how do I create an actual python object from the sequential string?</p>
<p>I tried using the (probably dangerous eval function) but it didn’t work since the string isn’t valid python.</p>
<pre><code class="lang-auto">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 "&lt;ipython-input-2-c53f3ae3ac55&gt;", line 1, in &lt;module&gt;
    eval(str(base_model))
  File "&lt;string&gt;", line 2
    (fc1_l1): Linear(in_features=1, out_features=15, bias=True)
            ^
SyntaxError: invalid syntax

</code></pre>
<p>After trying that I assumed that most likely there is a standard way to do that isn’t this hacky.</p>
<p>Is it possible to do it?</p>
<hr>
<p>crossposted:</p>
<ul>
<li><a href="https://discuss.pytorch.org/t/how-does-load-a-sequential-model-from-a-string/97648" class="inline-onebox">How does one load a sequential model from a string in Pytorch?</a></li>
<li><a href="https://www.reddit.com/r/pytorch/comments/j20d16/how_does_one_load_a_sequential_model_from_a/" rel="noopener nofollow ugc">https://www.reddit.com/r/pytorch/comments/j20d16/how_does_one_load_a_sequential_model_from_a/</a></li>
<li><a href="https://stackoverflow.com/questions/64109883/how-does-one-load-a-sequential-model-from-a-string-in-pytorch" rel="noopener nofollow ugc">https://stackoverflow.com/questions/64109883/how-does-one-load-a-sequential-model-from-a-string-in-pytorch</a></li>
</ul>