pytorch’s Variable is too confusing to use well, and I got an error which spent my whole day to solve but I stil have no idea where is the problem?
here is part of my code,and the ‘train_batches’ is just an iterator of my trainset
for batch in train_batches:
loss=0
Encoder_optimizer.zero_grad()
Attention_optimizer.zero_grad()
Score_optimizer.zero_grad()
for idx in range(args.batch_size):
question=Variable(torch.LongTensor(batch['question_token_ids'][idx]))
answer_passage=batch['answer_passage'][idx]
label=torch.zeros(args.max_paragraph_num)
label[answer_passage]=1
label=Variable(label)
label.requires_grad=True
scores = Variable(torch.zeros(args.max_paragraph_num))
Encoder.init_hidden()
_,question=Encoder(question)
j=0
for pidx in range(idx*args.max_paragraph_num,(idx+1)*args.max_paragraph_num):
passage=Variable(torch.LongTensor(batch['passage_token_ids'][pidx]))
Encoder.init_hidden()
passage,_=Encoder(passage)
passage=Attention(passage,question)
score=Score(passage,question)
scores[j]=score
j+=1
scores=F.softmax(scores,0)
loss+=loss_func(label,scores.view(1,5))
the error is:
Traceback (most recent call last):
File “/home/k/PycharmProjects/PassageRanking/run.py”, line 154, in
run()
File “/home/k/PycharmProjects/PassageRanking/run.py”, line 146, in run
train(args)
File “/home/k/PycharmProjects/PassageRanking/run.py”, line 134, in train
loss+=loss_func(label,scores.view(1,5))
File “/home/k/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 357, in call
result = self.forward(*input, **kwargs)
File “/home/k/anaconda3/lib/python3.6/site-packages/torch/nn/modules/loss.py”, line 677, in forward
_assert_no_grad(target)
File “/home/k/anaconda3/lib/python3.6/site-packages/torch/nn/modules/loss.py”, line 11, in _assert_no_grad
"nn criterions don’t compute the gradient w.r.t. targets - please "
AssertionError: nn criterions don’t compute the gradient w.r.t. targets - please mark these variables as volatile or not requiring gradients