Hi, I wonder if it is possible to instantiating modules in “init” as entries in a dictionary.
For example, is the following allowed?
class MyNet(nn.Module):
def __init__(self):
self.myModules = {}
self.myModule['dog'] = nn.Linear(4096, 300)
self.myModule['cat'] = nn.Linear(4096, 300)
self.myModule['flower'] = nn.Linear(4096, 300)
def forward(self, img):
...
This example is simple that it does not have to use dictionary, but what I actually need to do is something like following:
class MyNet(nn.Module):
def __init__(self, number_of_modules):
self.myModules = {}
for i in range(0, number_of_modules):
self.myModule[ 'dog' + str(i) ] = nn.Linear(4096, 300)
def forward(self, img):
...
The modules are of same shape, but I don’t want them to share weights.