AttributeError: 'numpy.ndarray' object has no attribute 'dim' from torch/nn/functional.py

jintolonappan: <p>I’m working on a DDPG implementation and getting <code>AttributeError: 'numpy.ndarray' object has no attribute 'dim'</code> from my Actor class.<br>
Based on answer here <a>https://discuss.pytorch.org/t/attributeerror-numpy-ndarray-object-has-no-attribute-dim/16026/2</a>, I tried using <code>Variable()</code>, however, the args are Tensors already and I get the <code>TypeError: expected np.ndarray (got Tensor)</code> error.</p>
<p><strong>Code</strong></p>
<pre><code class="lang-auto">    def forward(self, state):
        """
        Build the policy/actor network that maps states to actions.
        Using Tanh activation as per DDPG paper
        """
        # print(type(state)) # &lt;class 'torch.Tensor'&gt;
#         state = Variable(T.from_numpy(state))
        x = self.inp(state)
        l1_states = F.relu(x)
        l2_states = F.relu(self.h1(l1_states))
        l3_actions = T.tanh(self.out(l2_states))

        return l3_actions
</code></pre>
<p><strong>Error:</strong></p>
<pre><code class="lang-auto">&lt;ipython-input-11-d3087ef19a20&gt; in forward(self, state)
     71         # print(type(state)) # &lt;class 'torch.Tensor'&gt;
     72 #         state = Variable(T.from_numpy(state))
---&gt; 73         x = self.inp(state)
     74         l1_states = F.relu(x)
     75         l2_states = F.relu(self.h1(l1_states))

~/anaconda3/envs/drlnd/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    539             result = self._slow_forward(*input, **kwargs)
    540         else:
--&gt; 541             result = self.forward(*input, **kwargs)
    542         for hook in self._forward_hooks.values():
    543             hook_result = hook(self, input, result)

~/anaconda3/envs/drlnd/lib/python3.6/site-packages/torch/nn/modules/linear.py in forward(self, input)
     85 
     86     def forward(self, input):
---&gt; 87         return F.linear(input, self.weight, self.bias)
     88 
     89     def extra_repr(self):

~/anaconda3/envs/drlnd/lib/python3.6/site-packages/torch/nn/functional.py in linear(input, weight, bias)
   1366         - Output: :math:`(N, *, out\_features)`
   1367     """
-&gt; 1368     if input.dim() == 2 and bias is not None:
   1369         # fused op is marginally faster
   1370         ret = torch.addmm(bias, input, weight.t())

AttributeError: 'numpy.ndarray' object has no attribute 'dim'
</code></pre>

ptrblck: <p>Based on the error message it looks like <code>state</code> is a numpy array.<br>
Could you check the type again, please, and make sure it’s a tensor?</p>
<p>Don’t use <code>Variables</code>, as they are deprecated since PyTorch <code>0.4.0</code>.</p>

jintolonappan: <aside class="quote no-group quote-modified" data-username="jintolonappan" data-post="1" data-topic="66265">
<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/jintolonappan/48/19517_2.png" class="avatar"> jintolonappan:</div>
<blockquote>
<p>print(type(state)) # &lt;class ‘torch.Tensor’&gt;</p>
</blockquote>
</aside>
<p><code>&lt;class 'torch.Tensor'&gt;</code> was the output of <code>print(type(state)) </code></p>

ptrblck: <p>Is this error raised directly after the print statement and did you check all <code>state</code> inputs?<br>
Could some of them be numpy arrays somehow?</p>

jintolonappan: <p>Thanks for the suggestion to check <em>all</em> <code>state</code> inputs. Looks like a bug in my code when I sample from ReplayBuffer. <img src="https://discuss.pytorch.org/images/emoji/apple/exploding_head.png?v=9" title=":exploding_head:" class="emoji" alt=":exploding_head:"><br>
I was able to move forward now <img src="https://discuss.pytorch.org/images/emoji/apple/+1.png?v=9" title=":+1:" class="emoji" alt=":+1:"></p>

ptrblck: <p>Great to hear it’s working! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>