padded_sent = [S] * N + sent + [S] * N
// Step through the sentence
total_loss = 0
for i in range(N, len(sent) + N):
// c is the context vector
c = torch.tensor(padded_sent[i - N:i] + padded_sent[i + 1:i + N + 1]).type(type)
t = torch.tensor([padded_sent[i]]).type(type) // This is the target vector
log_prob = model(c)
loss = criterion(log_prob, t) // loss for predicting target from context vector
if not inference:
// Back prop while training only
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.data.cpu().item()return total_loss
MAX_LEN = 100
After Change
logits = model(c)
loss = criterion(logits, t) // loss for predicting target from context vector
losses.append(loss)
return torch.stack(losses).sum()
MAX_LEN = 100