【error】RuntimeError: multi-target not supported at /pytorch/torch/lib/THCUNN/generic/ClassNLLCriteri

错误:

RuntimeError: multi-target not supported at /pytorch/torch/lib/THCUNN/generic/ClassNLLCriterion.cu

错误反馈:

  File "/users4/zsun/pytorch/paper_image_caption/sz-spatial5-chunk/model_msoft_gh.py", line 214, in forward
    loss += F.cross_entropy( scores, captions.index_select(1, Variable(self.num_step_id+1)) )
  File "/users4/zsun/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 1140, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, size_average, ignore_index, reduce)
  File "/users4/zsun/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 1049, in nll_loss
    return torch._C._nn.nll_loss(input, target, weight, size_average, ignore_index, reduce)

RuntimeError: multi-target not supported at /pytorch/torch/lib/THCUNN/generic/ClassNLLCriterion.cu:16

出错代码:

loss += F.cross_entropy( scores, captions )

出错原因:

scores, captions的维度与F.cross_entropy()函数要求的维度不匹配,

例如出错的维度为scores=[batch, vocab_size], captions=[batch, 1]

解决:

loss += F.cross_entropy( scores, captions.squeeze() )

你可能感兴趣的:(随记)