AttributeError: 'torch.DoubleTensor' object has no attribute 'data'

import torch.nn as nn
from torch.autograd import Variable
import unicodedata
import string
import torch,ipdb
import torch.nn.functional as F
from envs.nn_env import NnEnv

class RNN(nn.Module):
    def __init__(self, num_hyperparams=4, hidden_size = 20, num_layers=3):
        super(RNN, self).__init__()
        self.hidden_size=hidden_size
        self.num_layers=num_layers
        self.rnn = nn.LSTM(input_size=num_hyperparams, hidden_size=hidden_size, num_layers=num_layers)
        self.affine1 = nn.Linear(hidden_size, num_hyperparams)

    
    def forward(self, input):
        output, self.h = self.rnn(input, (self.h, self.c))
        output = output.view(-1, output.size(2))
        output = affine1(output)
        return F.softmax(output)
    
    def initHidden(self):
        batch = 1
        self.h = Variable(torch.randn(self.num_layers, batch, self.hidden_size)) # (num_layers, batch, hidden_size)
        self.c = Variable(torch.randn(self.num_layers, batch, self.hidden_size))

def select_action(state):
    state = torch.from_numpy(state)
    probs = rnn(state.resize_(1,1,4))
    action = probs.multinomial()
    policy.saved_actions.append(action)
    return action.data

if __name__ == '__main__':
    # hyperparameters for rnn 
    n_hidden = 128
    num_hyperparams=4
    rnn = RNN(num_hyperparams, n_hidden, num_hyperparams)


    # values for the architecture of cnn sampled
    num_layers=2
    num_hyperparams_per_layer=4
    num_episodes=8
    
    for i in range(num_episodes):
        # create an environment
        env = NnEnv()
        rnn.initHidden()
        observation = env.reset()
        done=False
        
        while not done:
            # forward through the rnn
            action = select_action(observation)
            ipdb.set_trace()

            observation, reward, done, info = env.step(action)
            policy.rewards.append(reward)

Error:

   Traceback (most recent call last):
  File "rnn.py", line 57, in <module>
    action = select_action(observation)
  File "rnn.py", line 31, in select_action
    probs = rnn(state.resize_(1,1,4))
  File "/Users/abhishek/.virtualenvs/nn_search/lib/python2.7/site-packages/torch/_
    result = self.forward(*input, **kwargs)
  File "rnn.py", line 19, in forward
    output, self.h = self.rnn(input, (self.h, self.c))
  File "/Users/abhishek/.virtualenvs/nn_search/lib/python2.7/site-packages/torch/_
    result = self.forward(*input, **kwargs)
  File "/Users/abhishek/.virtualenvs/nn_search/lib/python2.7/site-packages/torch/d
    output, hidden = func(input, self.all_weights, hx)
  File "/Users/abhishek/.virtualenvs/nn_search/lib/python2.7/site-packages/torch/d
    if cudnn.is_acceptable(input.data):
AttributeError: 'torch.DoubleTensor' object has no attribute 'data'

Not sure if the way I resize it is correct.

No attribute data means, its expecting an autograd.Variable, and youre feeding it a torch.Tensor, so just wrap your torch.Tensor in an autograd.Variable, and should work ok.

(thinkgin about this, it seems like the modules themselves could handle this conversion themsevles. but, for now, they dont…)

I agree with you. Sould work okay.

Note to maintainers: might be interseting to add a dummy property to Tensor, that simply does:

@property
def data(self):
    print('data is a property of autograd.Variable. Please wrap your Tensor in autograd.Varaible, and try again')
    # throw exception or something here

PR for interpretable error message here: https://github.com/pytorch/pytorch/pull/2058

1 Like