def forward(self, title, content):
title = self.encoder_tit(title)
print(title.size())
title_rehape = torch.index_select(title, 2, Variable(torch.LongTensor([0,2,1])))
print("title_reshape size is {}".format(title_rehape.size()))
The result is:
torch.Size([1, 3, 64])
title_reshape size is torch.Size([1, 3, 3])
The expected result is [1, 64, 3].Why it’s [1,3,3].I’m confused.
And there is another question emerging.
class Text(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 = 64,
out_channels = 1,
kernel_size = (1, 64)),
nn.ReLU(),
nn.MaxPool1d(kernel_size=1),
)
self.fc = nn.Linear(5, 9)
def forward(self, title, content):
title = self.encoder_tit(title)
print(title.size())
title_rehape = torch.index_select(title, 2, Variable(torch.LongTensor([0,2,1])))
print("title_reshape size is {}".format(title_rehape.size()))
title_out_1 = self.title_conv_1(title_rehape)
The error is:
torch.Size([1, 3, 64])
title_reshape size is torch.Size([1, 3, 3])
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-277-dffba3242fa0> 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-276-f39b5fc5f928> in forward(self, title, content)
54 title_rehape = torch.index_select(title, 2, Variable(torch.LongTensor([0,2,1])))
55 print("title_reshape size is {}".format(title_rehape.size()))
---> 56 title_out_1 = self.title_conv_1(title_rehape)
57 title_out_2 = self.title_conv_2(title_rehape)
58 content = self.encoder_con(content)
/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_shape has been a 3D tensor.The error makes me very confused.Can you tell me why?
Thanks