class CNNText(nn.Module):
def __init__(self):
super(CNNText, self).__init__()
self.encoder_tit = nn.Embedding(3281, 64)
self.encoder_con = nn.Embedding(496037, 512)
self.title_conv_1 = nn.Sequential(
nn.Conv1d(in_channels = 1,
out_channels = 1,
kernel_size = (1, 64)),
nn.ReLU(),
nn.MaxPool1d(kernel_size=1),
)
self.title_conv_2 = nn.Sequential(
nn.Conv1d(in_channels = 1,
out_channels = 1,
kernel_size = (2, 64)),
nn.ReLU(),
nn.MaxPool1d(kernel_size=1),
)
self.content_conv_3 = nn.Sequential(
nn.Conv1d(in_channels = 1,
out_channels = 1,
kernel_size = (3, 512)),
nn.ReLU(),
nn.MaxPool1d(kernel_size = 50)
)
self.content_conv_4 = nn.Sequential(
nn.Conv1d(in_channels = 1,
out_channels = 1,
kernel_size = (3, 512)),
nn.ReLU(),
nn.MaxPool1d(kernel_size = 50)
)
self.content_conv_5 = nn.Sequential(
nn.Conv1d(in_channels = 1,
out_channels = 1,
kernel_size = (3, 512)),
nn.ReLU(),
nn.MaxPool1d(kernel_size = 50)
)
self.fc = nn.Linear(5, 9)
def forward(self, title, content):
title = self.encoder_tit(title)
print(title.size())
title_out_1 = self.title_conv_1(title)
title_out_2 = self.title_conv_2(title)
content = self.encoder_con(content)
content_out_3 = self.content_conv_3(content)
content_out_4 = self.content_conv_4(content)
content_out_5 = self.content_conv_5(content)
conv_out = t.cat((title_out_1,title_out_2,content_out_3,content_out_4,content_out_5),dim=1)
logits = self.fc(conv_out)
return F.log_softmax(logits)
cnnt = CNNText()
optimizer = optim.Adam(cnnt.parameters(), lr=.001)
Loss = nn.NLLLoss()
for epoch in range(50):
loss = 0
t = ''.join(title[epoch])
c = ''.join(content[epoch])
T, C = variables_from_pair(t, c)
# print(T.squeeze(1).unsqueeze(0))
T = T.squeeze(1).unsqueeze(0)
C = C.squeeze(1).unsqueeze(0)
optimizer.zero_grad()
out = cnnt(T, C)
target = cla[epoch]
loss += Loss(out, target)
loss.backward()
optimizer.step()
print("Loss is {} at {} epoch".format(loss, epoch))
Error:
torch.Size([1, 3, 64])
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-34-328d44896eef> in <module>()
15 optimizer.zero_grad()
16
---> 17 out = cnnt(T, C)
18 target = cla[epoch]
19 loss += Loss(out, target)
/home/quoniammm/anaconda3/envs/py3Tfgpu/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
222 for hook in self._forward_pre_hooks.values():
223 hook(self, input)
--> 224 result = self.forward(*input, **kwargs)
225 for hook in self._forward_hooks.values():
226 hook_result = hook(self, input, result)
<ipython-input-31-fe95ab78725e> in forward(self, title, content)
52 title = self.encoder_tit(title)
53 print(title.size())
---> 54 title_out_1 = self.title_conv_1(title)
55 title_out_2 = self.title_conv_2(title)
56
/home/quoniammm/anaconda3/envs/py3Tfgpu/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
222 for hook in self._forward_pre_hooks.values():
223 hook(self, input)
--> 224 result = self.forward(*input, **kwargs)
225 for hook in self._forward_hooks.values():
226 hook_result = hook(self, input, result)
/home/quoniammm/anaconda3/envs/py3Tfgpu/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input)
65 def forward(self, input):
66 for module in self._modules.values():
---> 67 input = module(input)
68 return input
69
/home/quoniammm/anaconda3/envs/py3Tfgpu/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
222 for hook in self._forward_pre_hooks.values():
223 hook(self, input)
--> 224 result = self.forward(*input, **kwargs)
225 for hook in self._forward_hooks.values():
226 hook_result = hook(self, input, result)
/home/quoniammm/anaconda3/envs/py3Tfgpu/lib/python3.6/site-packages/torch/nn/modules/conv.py in forward(self, input)
152 def forward(self, input):
153 return F.conv1d(input, self.weight, self.bias, self.stride,
--> 154 self.padding, self.dilation, self.groups)
155
156
/home/quoniammm/anaconda3/envs/py3Tfgpu/lib/python3.6/site-packages/torch/nn/functional.py in conv1d(input, weight, bias, stride, padding, dilation, groups)
81 f = ConvNd(_single(stride), _single(padding), _single(dilation), False,
82 _single(0), groups, torch.backends.cudnn.benchmark, torch.backends.cudnn.enabled)
---> 83 return f(input, weight, bias)
84
85
RuntimeError: expected 3D tensor
The title has been a 3D tensor.Why RuntimeError is expected 3D tensor