I have been working on a project where I use CNNs to restore punctuation to text, but I am still a little bit inexperienced with PyTorch. I’m having some trouble figuring out how to arrange the dimensions for the network. I also don’t think I am using the most effective framework for this problem either, but that’s for another time. The error that I am getting is at the bottom. I am unsure of what is helpful to show and what is not, so I have shown what seemed important to me.
Here are the important parts of my code:
Model:
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(1, 64, kernel_size=(1, 50))
self.conv2 = nn.Conv2d(64, 128, (1, 50))
self.conv3 = nn.Conv2d(128, 128, (1, 50))
self.fc1 = nn.Linear(32000, 1000)
self.fc2 = nn.Linear(1000, 250)
self.fc3 = nn.Linear(250, 3)
self.dropout = nn.Dropout(p=0.2)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
x.view(-1, 32000)
x = self.dropout(F.relu(self.fc1(x)))
x = self.dropout(F.relu(self.fc2(x)))
x = self.fc3(x)
return x
model = ConvNet()
print(model)
Output:
ConvNet(
(conv1): Conv2d(1, 64, kernel_size=(1, 50), stride=(1, 1))
(conv2): Conv2d(64, 128, kernel_size=(1, 50), stride=(1, 1))
(conv3): Conv2d(128, 128, kernel_size=(1, 50), stride=(1, 1))
(fc1): Linear(in_features=32000, out_features=1000, bias=True)
(fc2): Linear(in_features=1000, out_features=250, bias=True)
(fc3): Linear(in_features=250, out_features=3, bias=True)
(dropout): Dropout(p=0.2, inplace=False)
)
Training loader:
training_data= []
train_tensor_wv_np = training_tensor_wordvecs.numpy()
train_tensor_label_np = training_tensor_labels.numpy()
for i in range(len(training_tensor_wordvecs)):
training_data.append([train_tensor_wv_np[i], train_tensor_label_np[i]])
trainloader = torch.utils.data.DataLoader(training_data, shuffle=False, batch_size=1)
i1, l1 = next(iter(trainloader))
print(i1.shape, l1.shape)
Output:
torch.Size([1, 5, 50]) torch.Size([1, 5])
Training loop:
epochs = 20
model.train()
for epoch in range(epochs):
training_loss = 0.0
for data, target in trainloader:
optimizer.zero_grad()
output=model(data)
loss = criterion(ouput, target)
loss.backward()
optimizer.step()
training_loss+= loss.tem()*data.size(0)
training_loss = training_loss/len(trainloader.sampler)
print('Epoch: {} \tTraining Loss: {:.6f}'.format(
epoch+1,
training_loss
))
Output:
RuntimeError Traceback (most recent call last)
<ipython-input-136-a20e1a302cfe> in <module>
8 for data, target in trainloader:
9 optimizer.zero_grad()
---> 10 output=model(data)
11 loss = criterion(ouput, target)
12 loss.backward()
D:\Users\user\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
538 result = self._slow_forward(*input, **kwargs)
539 else:
--> 540 result = self.forward(*input, **kwargs)
541 for hook in self._forward_hooks.values():
542 hook_result = hook(self, input, result)
<ipython-input-133-ce6a4f5b562c> in forward(self, x)
14
15 def forward(self, x):
---> 16 x = F.relu(self.conv1(x))
17 x = F.relu(self.conv2(x))
18 x = F.relu(self.conv3(x))
D:\Users\user\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
538 result = self._slow_forward(*input, **kwargs)
539 else:
--> 540 result = self.forward(*input, **kwargs)
541 for hook in self._forward_hooks.values():
542 hook_result = hook(self, input, result)
D:\Users\user\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
346
347 def forward(self, input):
--> 348 return self._conv_forward(input, self.weight)
349
350 class Conv3d(_ConvNd):
D:\Users\user\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in _conv_forward(self, input, weight)
343 _pair(0), self.dilation, self.groups)
344 return F.conv2d(input, weight, self.bias, self.stride,
--> 345 self.padding, self.dilation, self.groups)
346
347 def forward(self, input):
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [64, 1, 1, 50], but got 3-dimensional input of size [1, 5, 50] instead
I don’t really know how to fix the dimensionality error, and I’m generally unsure if my current approach will work. Any guidance would be extremely appreciated. I am still relatively inexperienced, so I apologize if I have not presented my issue in the most readable way.