I’m trying to implement a skip-gram model. When I pass the input vector to the hidden layer, I get the error:
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 ‘mat2’
ONE_HOT_SIZE = len(dictionary)
EMBEDDING_DIM = 200
EPOCH_NUM = 5
def one_hot(index):
one_hot_array = torch.zeros(ONE_HOT_SIZE, dtype=torch.long)
one_hot_array[index] = 1.
return one_hot_array
class SkipGramModel(nn.Module):
def __init__(self, one_hot_size, embedding_dim):
super(SkipGramModel, self).__init__()
self.hidden = nn.Linear(one_hot_size, embedding_dim)
self.linear_out = nn.Linear(embedding_dim, one_hot_size)
def forward(self, x):
embed = self.hidden(x)
out = self.linear_out(embed)
out = F.log_softmax(out, dim=0)
return out
model = SkipGramModel(ONE_HOT_SIZE, EMBEDDING_DIM)
loss_function = nn.NLLLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
losses = []
total_count = EPOCH_NUM * len(targets)
with tqdm(total = total_count) as pbar:
for epoch in range(EPOCH_NUM):
total_loss = 0
for target in targets:
# Convert input word and target to one-hot vectors
word = one_hot(target[0])
target = one_hot(target[1])
# Reset gradient
model.zero_grad()
# Get the output from the model
prediction = model(word)
# Calculate loss
loss = loss_function(prediction, target)
# Optimize
loss.backward()
optimizer.step()
# Add loss to total loss
total_loss += loss
# Update progress bar
pbar.update(1)
losses.append(total_loss)
Error message:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-39-884ed7fa9d4b> in <module>
11
12 # Get the output from the model
---> 13 prediction = model(word)
14
15 # Calculate loss
c:\users\archan\appdata\local\programs\python\python36\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
--> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)
<ipython-input-36-238e5eb99042> in forward(self, x)
5 self.linear_out = nn.Linear(embedding_dim, one_hot_size)
6 def forward(self, x):
----> 7 embed = self.hidden(x)
8 out = self.linear_out(embed)
9 out = F.log_softmax(out, dim=0)
c:\users\archan\appdata\local\programs\python\python36\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
--> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)
c:\users\archan\appdata\local\programs\python\python36\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)
90 @weak_script_method
91 def forward(self, input):
---> 92 return F.linear(input, self.weight, self.bias)
93
94 def extra_repr(self):
c:\users\archan\appdata\local\programs\python\python36\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)
1406 ret = torch.addmm(bias, input, weight.t())
1407 else:
-> 1408 output = input.matmul(weight.t())
1409 if bias is not None:
1410 output += bias
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'mat2'