Mat1 and mat2 shapes cannot be multiplied - Batch VS No batch

Dev2150: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/4/1415bf46d1dc1127039e0d8aec831569d537a1c2.png" data-download-href="https://discuss.pytorch.org/uploads/default/1415bf46d1dc1127039e0d8aec831569d537a1c2" title="Untitled"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/4/1415bf46d1dc1127039e0d8aec831569d537a1c2_2_321x500.png" alt="Untitled" data-base62-sha1="2RG8PPmmnbNjyt2sP3FRitq6mHM" width="321" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/4/1415bf46d1dc1127039e0d8aec831569d537a1c2_2_321x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/1/4/1415bf46d1dc1127039e0d8aec831569d537a1c2_2_481x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/4/1415bf46d1dc1127039e0d8aec831569d537a1c2_2_642x1000.png 2x" data-dominant-color="4D4E50"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Untitled</span><span class="informations">1316×2044 276 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><strong>The problem</strong>:<br>
If I <strong>try to match</strong> the 2 matrices in either case (1X or 64X (batch)), it will fail in the other</p>
<p>Case 1: 1X: mat1 and mat2 shapes cannot be multiplied (64x<strong>512</strong> and <strong>32</strong>x256)<br>
Case 2: 64X: mat1 and mat2 shapes cannot be multiplied (64x<strong>32</strong> and <strong>512</strong>x256)</p>
<p>By <strong>trying to match</strong>, I mean changing the size of <strong>layer4</strong>, the deeply connected layer which follows the flattened layer, where the error triggers in both cases</p>
<p><strong>Extra info</strong>:<br>
Input.size is (64 batches,) 3 channels, 16 rows, 8 columns</p>
<p>What am I missing?</p>

vmoens: <p>Hi <a class="mention" href="/u/dev2150">@Dev2150</a><br>
This is way too short for us to help you.<br>
What are you trying to do? What does and doesn’t work exactly? Where does the error occur, what’s the size of the input and the config of the layer etc.<br>
Don’t hesitate to be prolific, better to give too much info than not enough</p>

vmoens: <p>Sorry I saw that you edited your question but I still can’t make sense out of it.<br>
What’s case 1 and 2? What X in 1X? The batch size? What do you mean by no batch? A batch of 1 or no batch dimensions at all?</p>
<p>What I understand is that you are flattening the tensor from dim 0 to dim 1 in both cases, but in the 1X case it seems that you don’t have a first dimension. Maybe that should only be applied when you have a batch?</p>
<p>What’s layer4 exactly? What are you trying to do with it? What size do you expect for the input tensor? What do you want to do with the flatten layer? What do you mean by “if I try to match the two matrices”? What are you highlighting in your case 1 and case 2?</p>
<p>If you could re-write the question in more details it would be great.</p>

Dev2150: <p>I’ve tried reposting with more information but it was flagged and taken down.<br>
<code>What I understand is that you are flattening the tensor from dim 0 to dim 1 in both cases, but in the 1X case it seems that you don’t have a first dimension. Maybe that should only be applied when you have a batch?</code><br>
ooooooooooh… this must be it… i must always provide the state with the same dimensions, even if one of them has size 1. Thank you</p>
<p>Edited post:</p>
<p><strong>The problem</strong>:<br>
If I <strong>try to match</strong> the 2 matrices in either case (64X (batch) or 1X), it will fail in the other</p>
<p><strong>Case 1</strong>:<br>
<code>mat1 and mat2 shapes cannot be multiplied (64x512 and 32x256)</code><br>
state tensor size: 64x3x16x8<br>
Stack trace: Training Loop → agent.learn () → Forward()</p>
<p>All taken decisions are random</p>
<p><strong>Case 2</strong>:<br>
<code>mat1 and mat2 shapes cannot be multiplied (64x32 and 512x256)</code><br>
state tensor size: 3x16x8<br>
Stack trace: Training Loop → agent.choose_action() → Forward()</p>
<p>This error happens after the other one, when it has to take (the first?) decision that is not random</p>
<p>By <strong>trying to match</strong>, I mean changing the size of <strong>layer4</strong>, the deeply connected layer which follows the flattened layer, where the error triggers in both cases</p>
<p>My interpretation: In case 2, layer4 expects a way smaller dataset because it’s not a batch</p>
<p><strong>Extra info</strong>:<br>
Input.size is (64 batches,) 3 channels, 16 rows, 8 columns</p>
<p>What am I missing?</p>