Hello,
I am getting an index error in the model I made, and I’m not sure how to troubleshoot.
Here is the code:
class NLP_model(nn.Module):
def init(self, vocab_size, embedding_dim, hidden_dim, num_classes):
super(NLP_model, self).init()
self.char_embedding = nn.Embedding(vocab_size, embedding_dim)
self.gru = nn.GRU(embedding_dim, hidden_dim, num_layers = 5, bidirectional= True)
self.fc = nn.Linear(hidden_dim*2,num_classes)
#self.att = nn.MultiheadAttention(embed_dim, num_heads, …)
def forward(self, x):
x = self.char_embedding(x)
output, hidden = self.gru(x)
hidden = torch.cat((hidden[0][-2,:,:], hidden[0][-1,:,:]), dim=1)
x = self.fc(hidden[0])
return x
model = NLP_model(len(alphabet), 12, 24, assigned[‘fismaid’].nunique()) #Find the number of fismaids for last variable
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters(), lr = 0.001)
for epoch in range(100): #Look into DataLoader for batch processing
y = list()
z = list()
for sentence, label in zip(ls_X_train, ls_y_train): #training_data should be an array of hostnames and labels
model.zero_grad()
output = model(sentence_to_id(sentence)) #sentence is the hostname, label is the fismaid
#print(label)
#print(output.shape)
temp_label = label
label = torch.zeros(assigned[‘fismaid’].nunique())
label[temp_label] = 1.0
#label = torch.tensor(label).unsqueeze(0)
#label = torch.tensor([label]).unsqueeze(1)
#label = torch.tensor(label).unsqueeze(1)
#print(label.shape)
loss = criterion(output, label)
loss.backward()
optimizer.step()
y.append(loss.item())
The error I’m seeing is the following:
IndexError Traceback (most recent call last)
in
8 for sentence, label in zip(ls_X_train, ls_y_train): #training_data should be an array of hostnames and labels
9 model.zero_grad()
—> 10 output = model(sentence_to_id(sentence)) #sentence is the hostname, label is the fismaid
11 #print(label)
12 #print(output.shape)
~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
→ 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
in forward(self, x)
10 x = self.char_embedding(x)
11 output, hidden = self.gru(x)
—> 12 hidden = torch.cat((hidden[0][-2,:,:], hidden[0][-1,:,:]), dim=2)
13 x = self.fc(hidden[0])
14
IndexError: too many indices for tensor of dimension 2