AttributeError: 'list' object has no attribute 'dim'

xz6014: <p>Hi there,</p>
<p>Could anyone help me resolve this issue of mine?</p>
<pre><code class="lang-auto">from __future__ import division

import igraph as ig
import os
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as Data

from demo.graphcc import *


class SDNE(nn.Module):
    def __init__(self, encode_dim, N, batch_size=128):
        super(SDNE, self).__init__()
        self.encode_dim = encode_dim
        self.vectors = {}
        self.N = N
        self.batch_size = batch_size
        if encode_dim == 4:
            self.linear1 = nn.Linear(self.N, 5000)
            self.linear2 = nn.Linear(5000, 1000)
            self.linear3 = nn.Linear(1000, 100)
            self.linear4 = nn.Linear(100, 1000)
            self.linear5 = nn.Linear(1000, 5000)
            self.linear6 = nn.Linear(5000, self.N)
        if encode_dim == 3:
            self.linear1 = nn.Linear(self.N, 1000)
            self.linear2 = nn.Linear(1000, 100)
            self.linear3 = nn.Linear(100, 1000)
            self.linear4 = nn.Linear(1000, self.N)

    def forward(self, x):
        if self.encode_dim == 3:
            emb = nn.Sigmoid(self.linear1(x))
            emb = nn.Sigmoid(self.linear2(emb))
            recon = nn.Sigmoid(self.linear3(emb))
            recon = nn.Sigmoid(self.linear4(recon))
        else:
            emb = nn.Sigmoid(self.linear1(x))
            emb = nn.Sigmoid(self.linear2(emb))
            emb = nn.Sigmoid(self.linear3(emb))
            recon = nn.Sigmoid(self.linear4(emb))
            recon = nn.Sigmoid(self.linear5(recon))
            recon = nn.Sigmoid(self.linear6(recon))

        # returning embedding embedding layer and reconstruction output
        return emb, recon

</code></pre>
<p>This is the error that obtained<br>
‘’’<br>
Traceback (most recent call last):<br>
File “/anaconda/envs/py35/lib/python3.5/site-packages/IPython/core/interactiveshell.py”, line 2963, in run_code<br>
exec(code_obj, self.user_global_ns, self.user_ns)<br>
File “”, line 1, in <br>
embedding_final = train(model, optimizer, weighted_adj)<br>
File “”, line 98, in train<br>
emb_batch, recon_batch = m(data)<br>
File “/anaconda/envs/py35/lib/python3.5/site-packages/torch/nn/modules/module.py”, line 489, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “”, line 52, in forward<br>
emb = nn.Sigmoid(self.linear1(x))<br>
File “/anaconda/envs/py35/lib/python3.5/site-packages/torch/nn/modules/module.py”, line 489, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “/anaconda/envs/py35/lib/python3.5/site-packages/torch/nn/modules/linear.py”, line 67, in forward<br>
return F.linear(input, self.weight, self.bias)<br>
File “/anaconda/envs/py35/lib/python3.5/site-packages/torch/nn/functional.py”, line 1350, in linear<br>
if input.dim() == 2 and bias is not None:<br>
AttributeError: ‘list’ object has no attribute ‘dim’<br>
‘’’</p>
<p>Cheers,</p>
<p>XZ</p>

alex.veuthey: <p>You should not instantiate new <code>nn.Sigmoid</code> classes every time, but instead create one object, e.g. <code>self.sigmoid = nn.Sigmoid()</code> and then call it as <code>emb = self.sigmoid(self.linear1(x))</code>, and so on for each layer in your network.</p>
<p>This will call the <code>forward()</code> function of this <code>sigmoid</code> object rather than the constructor of the class <code>nn.Sigmoid</code>.</p>

xz6014: <p>Thank you for your kind reply. I have changed my code to the following as instructed, but still obtained the same error.</p>
<pre><code class="lang-auto">class SDNE(nn.Module):
    def __init__(self, encode_dim, N, batch_size=128):
        super(SDNE, self).__init__()
        self.encode_dim = encode_dim
        self.vectors = {}
        self.N = N
        self.batch_size = batch_size
        self.sigmoid = nn.Sigmoid()
        self.linear1 = nn.Linear(self.N, 5000)
        self.linear2 = nn.Linear(5000, 1000)

        self.linear3 = nn.Linear(self.N, 1000)
        self.linear4 = nn.Linear(1000, 100)
        self.linear5 = nn.Linear(100, 1000)
        self.linear6 = nn.Linear(1000, self.N)

        self.linear7 = nn.Linear(1000, 5000)
        self.linear8 = nn.Linear(5000, self.N)

    def forward(self, x):
        if self.encode_dim == 3:
            emb = self.sigmoid(self.linear3(x))
            emb = self.sigmoid(self.linear4(emb))
            recon = self.sigmoid(self.linear5(emb))
            recon = self.sigmoid(self.linear6(recon))
        else:
            emb = self.sigmoid(self.linear1(x))
            emb = self.sigmoid(self.linear2(emb))
            emb = self.sigmoid(self.linear4(emb))
            recon = self.sigmoid(self.linear5(emb))
            recon = self.sigmoid(self.linear7(recon))
            recon = self.sigmoid(self.linear8(recon))

        # returning embedding embedding layer and reconstruction output
        return emb, recon

</code></pre>

ptrblck: <p>Your code seems to work on my machine after you’ve fixed the issue as suggested by <a class="mention" href="/u/alex.veuthey">@alex.veuthey</a>.</p>
<pre><code class="lang-python">model = SDNE(1, 1, 1)
model(torch.randn(1, 1))
</code></pre>
<p>yields two valid tensors.</p>

xz6014: <p>It works now. Thank you very much!</p>
<p>Best,</p>
<p>XZ</p>

Elvern_Eng_Hui: <p>I got AttributeError: ‘list’ object has no attribute ‘dim’ from this. My input for the LSTM is a list because the input supposed to be a time series input. But that creates a problem which I still I can’t seem to figure it out.</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

import numpy as np
from tqdm import tqdm

inputs = [torch.randn(1,1,3) for _ in range(1)]



batch_size = 1
hidden = (torch.randn(1, 1, 3), torch.randn(1, 1, 3))


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        
        self.lstm = nn.LSTM(3, 3, 1) #input_dim,output_dim and number of layer/depth 
        
        self.fc1 = nn.Linear(3,5)
        self.fc2 = nn.Linear(5,1)
        self.fc3 = nn.Linear(len(inputs),2)
        
    def RNN(self, inputs, hidden):
        
        for i in inputs:
            out, hidden = lstm(i.view(1, 1, -1), hidden)
            
        #out, hidden = lstm(inputs, hidden)
        
        #print(out,hidden)
        return out, hidden
        
    def forward(self, out):

        x = torch.tanh(self.fc1(out))
        x = torch.tanh(self.fc2(x))
        x = self.fc3(x.view(-1,len(inputs)))

        return x
        
    
        
net = Net()

output = net(inputs)
print(output)
</code></pre>

ptrblck: <p><code>nn.LSTM</code> expects the input in the shape <code>[seq_len, batch_size, features]</code>, so you could concatenate the tensors in the <code>seq_len</code> dimension before passing it to the model.<br>
<code>inputs = torch.cat(inputs, dim=0)</code> should work.</p>