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

I’m working on a DDPG implementation and getting AttributeError: 'numpy.ndarray' object has no attribute 'dim' from my Actor class.
Based on answer here https://discuss.pytorch.org/t/attributeerror-numpy-ndarray-object-has-no-attribute-dim/16026/2, I tried using Variable(), however, the args are Tensors already and I get the TypeError: expected np.ndarray (got Tensor) error.

Code

    def forward(self, state):
        """
        Build the policy/actor network that maps states to actions.
        Using Tanh activation as per DDPG paper
        """
        # print(type(state)) # <class 'torch.Tensor'>
#         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

Error:

<ipython-input-11-d3087ef19a20> in forward(self, state)
     71         # print(type(state)) # <class 'torch.Tensor'>
     72 #         state = Variable(T.from_numpy(state))
---> 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:
--> 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):
---> 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     """
-> 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'

Based on the error message it looks like state is a numpy array.
Could you check the type again, please, and make sure it’s a tensor?

Don’t use Variables, as they are deprecated since PyTorch 0.4.0.

<class 'torch.Tensor'> was the output of print(type(state))

Is this error raised directly after the print statement and did you check all state inputs?
Could some of them be numpy arrays somehow?

Thanks for the suggestion to check all state inputs. Looks like a bug in my code when I sample from ReplayBuffer. :exploding_head:
I was able to move forward now :+1:

Great to hear it’s working! :slight_smile: