torch.cuda.FloatTensor changed to 'DataLoader'

Hi,i am new to pytorch from tensorflow.I get a problem when using pytorch.
Below is the init(self, n_visible,n_hidden,dropout_) function.An Auto Encoder

self.fc1 = nn.Linear(n_visible,n_hidden)
self.fc2 = nn.Linear(n_hidden,n_visible)
self.dropout = nn.Dropout(dropout_)

Below is the forward(self,x) function

print("type:", x.type())
Encoder = F.sigmoid(self.fc1(x))
dropout_hidden = self.dropout(Encoder)
Decoder = F.sigmoid(self.fc2(dropout_hidden))

Below is the trace

type: torch.cuda.FloatTensor
Traceback (most recent call last):
  File "/home/zhanafeng/PycharmProjects/CollaborativeDeepLearning/main.py", line 147, in <module>
    num_items,num_voca,itr,dipen_activation,pretrain_input,args)
  File "/home/zhanafeng/PycharmProjects/CollaborativeDeepLearning/SDAE.py", line 101, in pretrain
    Encoder,Decoder = model(train_loader)
  File "/home/zhanafeng/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/zhanafeng/PycharmProjects/CollaborativeDeepLearning/SDAE.py", line 35, in forward
    print("type:", x.type())
AttributeError: 'DataLoader' object has no attribute 'type'

I don’t know why a torch.cuda.FloatTensor changed to a ‘DataLoader’ object .And when i train the net,the trace is:

Traceback (most recent call last):
  File "/home/zhanafeng/PycharmProjects/CollaborativeDeepLearning/main.py", line 147, in <module>
    num_items,num_voca,itr,dipen_activation,pretrain_input,args)
  File "/home/zhanafeng/PycharmProjects/CollaborativeDeepLearning/SDAE.py", line 101, in pretrain
    Encoder,Decoder = model(train_loader)
  File "/home/zhanafeng/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/zhanafeng/PycharmProjects/CollaborativeDeepLearning/SDAE.py", line 36, in forward
    Encoder = F.sigmoid(self.fc1(x))
  File "/home/zhanafeng/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/zhanafeng/anaconda3/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 55, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/zhanafeng/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 1022, in linear
    if input.dim() == 2 and bias is not None:
AttributeError: 'DataLoader' object has no attribute 'dim'

the train code is:

    for epoch in range(train_epoch):
        model.train()
        for step,(x,y) in enumerate(train_loader):

            x = x.cuda()
            y = y.cuda()
            Encoder,Decoder = model(x)

            loss = loss_func(Decoder,y)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

Hi,

I notice that in your second trackback, you have Encoder,Decoder = model(train_loader). It is different from the code you posted below.

1 Like

thanks.i find the bug.Encoder,Decoder = model(train_loader) is the below code of train code.i want to test the whole data.But i find a new bug now,my model did’t train.:sweat:
thanks