PyTorch shape_mismatch error between tensors of same size?

Arham_Khan: <p>I am using a boolean tensor to index another tensor. I am building a very basic Deep Q Learning model with experience replay, the below code is meant to NOT the mask denoting which state transitions are terminal, using this to index the state value estimates tensor and place the needed values there.</p>
<p>In the end the tensor should be arranged such that if terminal_mask[i]==1 then discounted_Q-estimates = reward otherwise discounted_Q_estimates = reward + GAMMA* max_a’(Q(s,a’)).</p>
<p>The error this returns is:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "C:\Users\Arham\Desktop\DeepQBreakout\main.py", line 121, in &lt;module&gt;
    discounted_Q_estimates[torch.logical_not(terminal_mask)] = GAMMA * ns_preds
RuntimeError: shape mismatch: value tensor of shape [32] cannot be broadcast to indexing result of shape [31]
</code></pre>
<p>which is strange because every print statement in the below snippet returns “torch.size([32])” indicating that the value and index tensors have the same size, can anyone help me debug this? Thank you!</p>
<pre><code class="lang-auto">states, actions, rewards, next_states, terminal_mask = replay_buffer.sample()
          #if terminal state label is reward else reward + discounted Q estimate
          print(terminal_mask.shape) #torch.Size([32])
          print(torch.logical_not(terminal_mask).shape) #torch.Size([32])
          with torch.no_grad():
              ns_preds, _ = torch.max(model(next_states.to(device)), dim=1)
              discounted_Q_estimates = torch.zeros(BATCHSIZE, device=device)
              print(ns_preds.shape) #torch.Size([32])
              print(discounted_Q_estimates.shape) #torch.Size([32])
              discounted_Q_estimates[torch.logical_not(terminal_mask)] = GAMMA * ns_preds
              y = rewards + discounted_Q_estimates
</code></pre>

ptrblck: <p>Based on the error message the indexed tensor has a shape of <code>[31]</code>, while the value tensor has a shape of <code>[32]</code>. This code snippet illustrates the issue:</p>
<pre><code class="lang-python">x = torch.randn(32)
idx = torch.ones(32).bool()
x[idx] = torch.randn(32) # works

idx[0] = False
x[idx] = torch.randn(32) # fails
# &gt; RuntimeError: shape mismatch: value tensor of shape [32] cannot be broadcast to indexing result of shape [31]
</code></pre>
<p>so you won’t be able to assign <code>32</code> values to the <code>x[idx]</code> which is indexing <code>31</code> values only.</p>

Arham_Khan: <p>Thank you, I figured that this was the problem but based on my print debugging it would seem that the tensors were all of size [32], what could account for this sort of error?</p>

ptrblck: <p>The shapes are also <code>[32]</code> in my example, but the number of <code>True</code> values in <code>idx</code> is <code>31</code>.<br>
Indexing <code>x</code> with <code>31</code> <code>True</code> values will return a tensor of the shape <code>[31]</code>:</p>
<pre><code class="lang-python">x = torch.randn(32)
idx = torch.ones(32).bool()
idx[0] = False
print(x[idx].shape)
# &gt; torch.Size([31])
</code></pre>
<p>which will then create the error since you are trying to assing <code>32</code> values to it.</p>