Hi folks,
I am trying to build an ensemble with the following code:
class PytorchMnistEnsModel(nn.Module):
""" Basic MNIST model from github
https://github.com/rickiepark/pytorch-examples/blob/master/mnist.ipynb
"""
def __init__(self, num_models=5):
super(PytorchMnistEnsModel, self).__init__()
# input is 28x28
# padding=2 for same padding
self.conv1 = {}
self.conv2 = {}
self.fc1 = {}
self.fc2 = {}
self.num_models = num_models
for i in range(self.num_models):
self.conv1[i] = nn.Conv2d(1, 32, 5, padding=2)
# feature map size is 14*14 by pooling
# padding=2 for same padding
self.conv2[i] = nn.Conv2d(32, 64, 5, padding=2)
# feature map size is 7*7 by pooling
self.fc1[i] = nn.Linear(64 * 7 * 7, 1024)
self.fc2[i] = nn.Linear(1024, 10)
def forward(self, x):
preds_list = []
y = {}
for i in range(self.num_models):
y[i] = torch.tensor(x, requires_grad=True)
y[i] = F.relu((self.conv1[i](y[i])))
y[i] = F.max_pool2d(y[i], 2)
y[i] = F.relu(self.conv2[i](y[i]))
y[i] = F.max_pool2d(y[i], 2)
y[i] = y[i].view(-1, 64 * 7 * 7) # reshape Variable
y[i] = F.relu(self.fc1[i](y[i]))
y[i] = self.fc2[i](y[i])
preds_list.append(F.log_softmax(y[i], dim=-1))
preds_avg = torch.mean(torch.stack(preds_list), dim = 0)
return preds_avg
When I call model.parameters() on this, it returns an empty list. I am guessing it has something to do with my usage of dictionary for defining conv and fc layers. How do I go about fixing this?
Thanks in advance!