How to use binary cross entropy with logits in binary target and 3d output

Pritesh_Gohil: <p>I have batch size = 5<br>
my network output is given by the following code <code>Output = F.upsample(per_frame_logits, t, mode='linear')</code></p>
<p>Shape of output is = torch.Size([5, 2, 64])<br>
Shape of target is = torch.Size([5]) (i.e. ex [1.0, 0.0, 0.0, 1.0, 1.0])</p>
<p>Then i pass it to following loss function <code>loss = F.binary_cross_entropy_with_logits(output, target)</code></p>
<p>I get the following value error</p>
<pre><code class="lang-auto">raise ValueError("Target size ({}) must be the same as input size ({})".format(target.size(), input.size()))
ValueError: Target size (torch.Size([5])) must be the same as input size (torch.Size([5, 2, 64]))

</code></pre>

ptrblck: <p>Could you explain your current use case a bit?<br>
It seems your model outputs a batch of 5 samples, each containing the logits (probabilities) for 2 classes for a tensor of length 64.<br>
Also, it seems you are dealing with a multi-label classification, i.e. each sample might contain none, one, or both classes. Is this correct?</p>
<p>If so, what are the targets correspond to? It seems you are just passing a binary target for each sample in the batch, which would point to a vanilla binary classification.</p>

Pritesh_Gohil: <p>I’m a beginner to pytorch and implementing i3d network for binary classification. I have RGB video (64 frames simultaneously) input to the network and each video have a single label which is 0 (failure) or 1 (success).</p>
<p>I kept my batch size to 5 just to check if my network or code is working or not. (I would call it a debug run)</p>
<aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="52784">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>Also, it seems you are dealing with a multi-label classification, i.e. each sample might contain none, one, or both classes. Is this correct?</p>
</blockquote>
</aside>
<p>Each class have a single label. So I guess I should change network output from 2 to 1.</p>

ptrblck: <p>So the output shape would correspond to <code>[batch_size, nb_classes, frames]</code>?<br>
I’m not that familiar with the i3d model, but I assume the temporal (and spatial) dimensions were reduced somehow?</p>
<p>The current output format would correspond to a frame-wise multi-label classification.</p>
<aside class="quote no-group" data-username="Pritesh_Gohil" data-post="3" data-topic="52784">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pritesh_gohil/48/14828_2.png" class="avatar"> Pritesh_Gohil:</div>
<blockquote>
<p>Each class have a single label. So I guess I should change network output from 2 to 1.</p>
</blockquote>
</aside>
<p>In that case, you could use <code>nn.BCEWithLogitsLoss</code> (or <code>nn.BCELoss</code> + sigmoid) with a since output channel. Alternatively you could keep the two output channels and use <code>nn.CrossEntropyLoss</code>.</p>

Pritesh_Gohil: <p>Below is the link to the author’s i3d network. In their case they frame-wise multi-label classification.<br>
[<a href="https://github.com/piergiaj/pytorch-i3d" class="inline-onebox" rel="noopener nofollow ugc">GitHub - piergiaj/pytorch-i3d</a>](<a href="http://i3d" rel="noopener nofollow ugc">http://i3d</a> Network for charades dataset)</p>
<p>I’m using Visual-Tactile dataset and</p>
<aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="52784">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>So the output shape would correspond to <code>[batch_size, nb_classes, frames]</code> ?</p>
</blockquote>
</aside>
<p>I3D is designed on kinetics dataset and I didn’t change default architecture from the above link having file “pytorch_i3d.py”.</p>
<aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="52784">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>I’m not that familiar with the i3d model, but I assume the temporal (and spatial) dimensions were reduced somehow?</p>
</blockquote>
</aside>
<p>I’m also new to this. But according to the author input frames to the network is 64. So each video i have converted to 64 frames. because I do not have to do multilabel classification.</p>
<aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="52784">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>The current output format would correspond to a frame-wise multi-label classification.</p>
</blockquote>
</aside>
<p>Probably I need to change the final layer since i don’t want multi-label classification.</p>
<aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="52784">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>In that case, you could use <code>nn.BCEWithLogitsLoss</code> (or <code>nn.BCELoss</code> + sigmoid) with a since output channel. Alternatively you could keep the two output channels and use <code>nn.CrossEntropyLoss</code> .</p>
</blockquote>
</aside>
<p>I still have the issue with dimension because it is clear that target and output are not of the same shape or as expected input to the loss function.</p>

ptrblck: <p>If you would like to classify each video sequence (64 frames) to a single class (binary classification), your output and target should both have the shape <code>[batch_size, 1]</code>.<br>
To achieve this you would need to reduce the model’s output, e.g. using an <code>nn.Linear</code> layer as the final classifier.</p>

Pritesh_Gohil: <p>Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a><br>
Now it’s working. Yes, that’s a possible solution I tried and it worked. But instead disturbing the i3d architecture I converted the output of network into<code>[batch_size, 1]</code> by max-pooling with the dimension of 1 and then squeezed the output which makes my target and output shape the same.</p>