Is there a parser for strings of Sequential Models?

I have a set of strings of sequential models e.g.

'Sequential( (conv1): Conv2d(3, 4, kernel_size=(3, 3), stride=(1, 1))
  (relu1): ReLU() (conv2): Conv2d(4, 2, kernel_size=(3, 3), stride=(1, 1)) (relu2):
  ReLU() (Flatten): Flatten() (fc): Linear(in_features=1568, out_features=10, bias=True)
  )'

I want to be able to parse it to extract the layers, the hyper parameters and sometimes even make a new model with that setting.

Is there an easy way to do this in PyTorch?

There’s always regex! :wink:

It looks as though you could split the string on whitespace + open bracket to have separate strings for each layer. Then write functions to read each type of layer. Not a particularly quick or clean solution though…

I don’t work with sequential models much, but is this basically the syntax you’d use to define one? If so, to make a new model with this setting you could just run mymodel = eval(mystring)