I want to create nn.Module in Pytorch. I used following code for text related problem (In fact I use Glove 300d pre-trained embedding and weighted average of words in a sentence to do classification).
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv1d(300, 128, kernel_size=5)
self.conv2 = nn.Conv1d(128, 64, kernel_size=2)
self.conv2_drop = nn.Dropout()
self.fc1 = nn.Linear(64, 20)
self.fc2 = nn.Linear(20, 2)
def forward(self, x):
x = F.relu(F.avg_pool1d(self.conv1(x), 2))
x = F.relu(F.avg_pool1d(self.conv2_drop(self.conv2(x)), 2))
x = x.view(-1, 1)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
return self.fc2(x)
But it gives me following error:
Traceback (most recent call last):
x = F.relu(F.avg_pool1d(self.conv1(x), 2))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/torch/nn/modules/module.py", line 224, in __call__
result = self.forward(*input, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/torch/nn/modules/conv.py", line 154, in forward
self.padding, self.dilation, self.groups)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/torch/nn/functional.py", line 83, in conv1d
return f(input, weight, bias)
RuntimeError: expected Double tensor (got Float tensor)
I fairly new to Conv1d, and most of tutorial used Conv1d for image problems. Can anybody give me some idea what's the problem?
I also added model.double() inside forward method but gives me another error:
RuntimeError: Given input size: (300 x 1 x 1). Calculated output size: (128 x 1 x -3). Output size is too small