TypeError: cross_entropy_loss(): argument 'target' (position 2) must be Tensor, not list

username: Sharique123
cooked: <pre><code class="lang-auto">class Criterion(object):
    """Weighted CrossEntropyLoss."""
    def __init__(self, dictionary, device_id=None, bad_toks=[], reduction='mean'):
        w = torch.Tensor(len(dictionary)).fill_(1)
        for tok in bad_toks:
            w[dictionary.get_idx(tok)] = 0.0
        if device_id is not None:
            w = w.cuda(device_id)
        # https://pytorch.org/docs/stable/nn.html
        self.crit = nn.CrossEntropyLoss(w, reduction=reduction)

    def __call__(self, out, tgt):
        return self.crit(out,tgt)

</code></pre>
<p>This error is throwing at last line :<code>return self.crit(out,tgt)</code></p>
<p>Complete error log:</p>
<pre><code class="lang-auto">C:\Users\Mohammad Sharique\Downloads\end-to-end-negotiator-master\end-to-end-negotiator-master\src&gt;python train.py
dataset data/negotiate\train.txt, total 687919, unks 8718, ratio 1.27%
dataset data/negotiate\val.txt, total 74653, unks 914, ratio 1.22%
dataset data/negotiate\test.txt, total 70262, unks 847, ratio 1.21%
Traceback (most recent call last):
  File "C:\Users\Mohammad Sharique\Downloads\end-to-end-negotiator-master\end-to-end-negotiator-master\src\train.py", line 125, in &lt;module&gt;
    main()
  File "C:\Users\Mohammad Sharique\Downloads\end-to-end-negotiator-master\end-to-end-negotiator-master\src\train.py", line 119, in main
    train_loss, valid_loss, select_loss, extra = engine.train(corpus)
  File "C:\Users\Mohammad Sharique\Downloads\end-to-end-negotiator-master\end-to-end-negotiator-master\src\engines\__init__.py", line 148, in train
    _, valid_loss, valid_select_loss, extra = self.iter(epoch, lr, traindata, validdata)
  File "C:\Users\Mohammad Sharique\Downloads\end-to-end-negotiator-master\end-to-end-negotiator-master\src\engines\__init__.py", line 111, in iter
    train_loss, train_time = self.train_pass(trainset)
  File "C:\Users\Mohammad Sharique\Downloads\end-to-end-negotiator-master\end-to-end-negotiator-master\src\engines\__init__.py", line 78, in train_pass
    loss = self.train_batch(batch)
  File "C:\Users\Mohammad Sharique\Downloads\end-to-end-negotiator-master\end-to-end-negotiator-master\src\engines\rnn_engine.py", line 31, in train_batch
    loss += self.sel_crit(sel_out, sel_tgt) * self.model.args.sel_weight
  File "C:\Users\Mohammad Sharique\Downloads\end-to-end-negotiator-master\end-to-end-negotiator-master\src\engines\__init__.py", line 36, in __call__
    return self.crit(out,tgt)
  File "C:\Users\Mohammad Sharique\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Users\Mohammad Sharique\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\nn\modules\loss.py", line 1163, in forward
    return F.cross_entropy(input, target, weight=self.weight,
  File "C:\Users\Mohammad Sharique\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\nn\functional.py", line 2996, in cross_entropy
    return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
TypeError: cross_entropy_loss(): argument 'target' (position 2) must be Tensor, not list

</code></pre>

username: sebk26
cooked: <p>Maybe I’m wrong, but the TypeError Message tells you that your second argument is a list but should be a <code>torch.Tensor</code> for the <code>cross_entropy_loss()</code> to work.</p>
<p>If I’m reading the stacktrace right you have to look in <code>self.train_batch</code> before</p>
<pre><code class="lang-python">loss += self.sel_crit(sel_out, sel_tgt) * self.model.args.sel_weight
</code></pre>
<p>and change the type of <code>sel_tgt</code> to <code>torch.Tensor</code>.</p>
<h3>
<a name="edit-1" class="anchor" href="#edit-1"></a>EDIT:</h3>
<p>Maybe look at the example given here: <a href="https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html" class="inline-onebox" rel="noopener nofollow ugc">CrossEntropyLoss — PyTorch 1.11.0 documentation</a></p>