Help me, please. I looked at similar questions and tried some of the solutions, but it didn’t help.
This is my code
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(in_channels=6, out_channels=6, kernel_size=3, padding=1)
self.act1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.bn1 = torch.nn.BatchNorm2d(num_features=6)
self.conv3 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=3, padding=1)
self.conv4 = nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3, padding=1)
self.act2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.bn2 = torch.nn.BatchNorm2d(num_features=16)
self.fc1 = nn.Linear(in_features=56 * 56 * 16, out_features=56)
self.fc2 = nn.Linear(56, 10)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.act1(x)
x = self.pool1(x)
x = self.bn1(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.act2(x)
x = self.pool2(x)
print(x.shape)
x = self.bn2(x)
print(x.shape)
x = self.fc1(x)
print(x.shape)
x = self.fc2(x)
return x
And this is code for training:
def train(net, n_epoch):
#device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
#net = net.to(device)
loss = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=1.0e-3)
test_accuracy_history = []
test_loss_history = []
dataloader = train_dataloader
for epoch in range(n_epoch):
for phase in ['train', 'val']:
if phase == 'train':
dataloader = train_dataloader
#scheduler.step()
net.train() # Set model to training mode
else:
dataloader = val_dataloader
net.eval()
running_loss = 0.
running_acc = 0.
for inputs, labels in tqdm(dataloader):
#inputs = inputs.to(device)
#labels = labels.to(device)
optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
preds = net.forward(inputs)
loss_value = loss(preds, labels)
preds_class = preds.argmax(dim=1)
if phase == 'train':
loss_value.backward()
optimizer.step()
else:
test_loss_history.append(loss(inputs, labels).data.cpu())
accuracy = (preds.argmax(dim=1) == labels).float().mean().data.cpu()
test_accuracy_history.append(accuracy)
running_loss += loss_value.item()
running_acc += (preds_class == labels.data).float().mean()
epoch_loss = running_loss / len(dataloader)
epoch_acc = running_acc / len(dataloader)
print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc), flush=True)
return test_loss_history, test_accuracy_history
And I get this traceback:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-14-a7c73a6166af> in <module>
----> 1 accuracy_train, loss_train = train(15)
2 #
<ipython-input-13-dcc24964612c> in train(n_epoch)
15 optimizer.zero_grad()
16
---> 17 preds = network(inputs)
18 losses = loss(preds, labels)
19 losses.backward()
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
<ipython-input-12-978f558521c2> in forward(self, x)
37 x = self.pool2(x)
38 print(x.shape)
---> 39 x = self.fc1(x)
40 print(x.shape)
41 x = self.act3(x)
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/linear.py in forward(self, input)
89
90 def forward(self, input: Tensor) -> Tensor:
---> 91 return F.linear(input, self.weight, self.bias)
92
93 def extra_repr(self) -> str:
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
1674 ret = torch.addmm(bias, input, weight.t())
1675 else:
-> 1676 output = input.matmul(weight.t())
1677 if bias is not None:
1678 output += bias
RuntimeError: size mismatch, m1: [7168 x 56], m2: [50176 x 56] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:41
I tried changing value of in_features to 7186 and value of out_features to 56 just to see what happens and got the following mistake:
RuntimeError: size mismatch, m1: [7168 x 56], m2: [7168 x 56] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:41
self.fc1 = nn.Linear(in_features=7168, out_features=56)