Module 'torch.nn' has no attribute 'Module'

what shoud i do?
my code is as follows:
import torch
import torch.nn as nn
class LSTM(nn.Module):

def __init__(self, num_classes, input_size, hidden_size, num_layers):
    super(LSTM, self).__init__()
    self.num_classes = num_classes
    self.num_layers = num_layers
    self.input_size = input_size
    self.hidden_size = hidden_size
    #self.seq_length = seq_length
    self.dropout = nn.Dropout(p=0.2)
    self.lstm = nn.LSTM(input_size=input_size, hidden_size=hidden_size,
                        num_layers=num_layers, batch_first=True,dropout = 0.25)
    self.fc = nn.Linear(hidden_size, num_classes)

def forward(self, x):
    h_0 = Variable(torch.zeros(
        self.num_layers, x.size(0), self.hidden_size).to(device))
    c_0 = Variable(torch.zeros(
        self.num_layers, x.size(0), self.hidden_size).to(device))
    # Propagate input through LSTM
    ula, (h_out, _) = self.lstm(x, (h_0, c_0))
    h_out = h_out.view(-1, self.hidden_size)
    out = self.fc(h_out)
    out = self.dropout(out)
    return out

AttributeError Traceback (most recent call last)
Cell In[32], line 3
1 import torch
2 import torch.nn as nn
----> 3 class LSTM(nn.Module):
5 def init(self, num_classes, input_size, hidden_size, num_layers):
6 super(LSTM, self).init()

AttributeError: module ‘torch.nn’ has no attribute ‘Module’

You can try listing the attributes of the torch.nn namespace with torch.nn.__dict__.

Module should be defined in this namespace.

thank u for ur advice, I reinstall the pytorch, now the problem has been solved, i guess it may be some mistakes when first setting the environment.

1 Like