I have a JSON file wherein I have defined a network (Consists of Conv and Dense layers)
I want to create a Network dynamically based on this JSON file, thus I want my network to have layers according to file. Thus my layer can either have 4 layers or 10 layers.
If this is exported from some other program, if you can export it in onnx or caffe format, my understanding is that pytorch has native importers for one or both of these types.
otherwise, if it’s a proprietary format description, you can write code to convert your json format into a python object hierarchy (json.loads(json_string)), then iterate over it.
you can do something like:
class MyNetwork(nn.Module):
def __init__(self, net_string):
super().__init__()
self.module_list = nn.ModuleList()
for layer_def in json.loads(net_string):
layer = self._create_layer(layer_def)
self.module_list.append(layer)
def forward(self, x):
for layer in self.module_list:
x = layer(x)
return x
(edit: ie, nn.ModuleList is quite useful for such dynamic layer lists)
And what about the non linearities?
Can you please explain with an example, as MaxPooling and Relu is applied in forward function on the example given in Official documentation
EDIT: if you don’t know all of that dimension sizes you could also pass -1 for one dimension. With -1 the dimension is set in a way that it fits to the number of entries in the tensor and is calculated with respect to the other dimensions
An init method is necessary because torch.nn.Module is an abstract class with the init being an abstract method and subclasses of abstract classes should implement all abstract methods.
Despite that fact the rest of your class should work the way you want it to