TypeError: empty(): argument 'size' must be tuple of ints, but found element of type torch.Size at pos 2

1amarvind: <p>Hi i need help regrading this issues <strong>TypeError: empty(): argument ‘size’ must be tuple of ints, but found element of type torch.Size at pos 2</strong></p>

Sairam954: <p>Can you share the code snippet, you are passing torch.Size to empty whereas it expects tuple. If you are trying to create an empty tensor with certain dimensioned tensor x. Call torch.empty(x.shape) instead of torch.empty(x.size()).</p>

1amarvind: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/a/3ad7407e5c8c9a18df46ac9d0cb7cfe2029dfc31.png" data-download-href="https://discuss.pytorch.org/uploads/default/3ad7407e5c8c9a18df46ac9d0cb7cfe2029dfc31" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/a/3ad7407e5c8c9a18df46ac9d0cb7cfe2029dfc31_2_690x428.png" alt="image" data-base62-sha1="8owRQbDNwdBYOvwWWUAjr1gv08p" width="690" height="428" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/a/3ad7407e5c8c9a18df46ac9d0cb7cfe2029dfc31_2_690x428.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/a/3ad7407e5c8c9a18df46ac9d0cb7cfe2029dfc31_2_1035x642.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/a/3ad7407e5c8c9a18df46ac9d0cb7cfe2029dfc31.png 2x" data-dominant-color="E9ECEA"><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">image</span><span class="informations">1186×736 88.4 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>

Matias_Vasquez: <p><code>Variable()</code> has been deprecated.</p>
<p>You can simply set the flag <code>requires_grad</code> to <code>True</code> when creating a tensor.</p>
<pre><code class="lang-python">h_0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size, requires_grad=True)
c_0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size, requires_grad=True)
</code></pre>

1amarvind: <p>its not working still got same error</p>

Matias_Vasquez: <p>It would really help if you posted <strong>where</strong> the error is occurring. You could, for example, post the error stack trace or a reproducible code to get this error.</p>
<p>As <a class="mention" href="/u/sairam954">@Sairam954</a> has already said, it appears that you are trying to create an empty tensor by passing a tuple and he has given some solutions. Another solution would be to use <code>empt_like</code> (<a href="https://pytorch.org/docs/stable/generated/torch.empty_like.html" rel="noopener nofollow ugc">docs</a>).</p>
<p>However, in the code that you posted there is no empty tensor created.</p>

1amarvind: <pre><code class="lang-auto">
* for epoch in range(num_epochs):
  outputs = lstm1.forward(X_train_tensors_final) #forward pass
  optimizer.zero_grad() #caluclate the gradient, manually setting to 0
 
  # obtain the loss function
  loss = criterion(outputs, X_test_tensors_final)
 
  loss.backward() #calculates the loss of the loss function
 
  optimizer.step() #improve from loss, i.e backprop
  if epoch % 100 == 0:
    print("Epoch: %d, loss: %1.5f" % (epoch, loss.item()))
[details="Summary"]
This text will be hidden
[/details]


</code></pre>

1amarvind: <p>*<em>RuntimeError: The size of tensor a (6) must match the size of tensor b (9) at non-singleton dimension 2</em></p>
<p><a class="mention" href="/u/matias_vasquez">@Matias_Vasquez</a></p>

Matias_Vasquez: <p>Hi,</p>
<p>can you print the entire stacktrace?</p>
<p>This way it will be easier to determine where the error is happening and why.</p>

1amarvind: <pre><code class="lang-auto">/usr/local/lib/python3.7/dist-packages/torch/nn/modules/loss.py:529: UserWarning: Using a target size (torch.Size([128, 2947, 9])) that is different to the input size (torch.Size([384, 6])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
  return F.mse_loss(input, target, reduction=self.reduction)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-16-d3c6c29eeb10&gt; in &lt;module&gt;()
      4 
      5   # obtain the loss function
----&gt; 6   loss = criterion(outputs, X_test_tensors_final)
      7 
      8   loss.backward() #calculates the loss of the loss function

3 frames
/usr/local/lib/python3.7/dist-packages/torch/functional.py in broadcast_tensors(*tensors)
     73     if has_torch_function(tensors):
     74         return handle_torch_function(broadcast_tensors, tensors, *tensors)
---&gt; 75     return _VF.broadcast_tensors(tensors)  # type: ignore[attr-defined]
     76 
     77 

RuntimeError: The size of tensor a (6) must match the size of tensor b (9) at non-singleton dimension 2```</code></pre>

Matias_Vasquez: <p><a href="https://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html" rel="noopener nofollow ugc">Here</a> is the documentation for <code>MSELoss</code>. As you can see on the bottom, it says that the <code>input</code> shape and <code>target</code> shape have to be the same.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/e/0ef9a7f933285a954a66410a1e618adcc6d26ffa.png" alt="image" data-base62-sha1="28tzmFkdtvY6RC0ZMa3p5I4InTI" width="408" height="125"></p>
<p>It seems that you are using the output of your model as the <code>input</code> for your criterion (this is correct, the prediction should go here). However, the <code>target</code> is <code>X_test_tensors_final</code>. This should be the ground truth labels to the input data you gave your model to classify.</p>
<p>Maybe you can take a look at that.</p>
<aside class="quote no-group quote-modified" data-username="1amarvind" data-post="9" data-topic="151094">
<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/1amarvind/48/49383_2.png" class="avatar"> 1amarvind:</div>
<blockquote>
<pre><code class="lang-auto">outputs = lstm1.forward(X_train_tensors_final) #forward pass
 
# obtain the loss function
loss = criterion(outputs, X_test_tensors_final)
</code></pre>
</blockquote>
</aside>