Python nn.Module的Pytorch子类没有属性“parameters”
Python版本:Python 3.8.5 Pytorch版本:“1.6.0” 我正在定义LSTM,nn.Module的一个子类。我正在尝试创建优化器,但出现以下错误:torch.nn.modules.ModuleAttributeError:“LSTM”对象没有属性“parameters” 我有两个代码文件,train.py和lstm_class.py包含lstm类。我会尽量提供一个最低限度的工作例子,让我知道如果任何其他信息是有用的 lstm_class.py中的代码:Python nn.Module的Pytorch子类没有属性“parameters”,python,python-3.x,neural-network,pytorch,Python,Python 3.x,Neural Network,Pytorch,Python版本:Python 3.8.5 Pytorch版本:“1.6.0” 我正在定义LSTM,nn.Module的一个子类。我正在尝试创建优化器,但出现以下错误:torch.nn.modules.ModuleAttributeError:“LSTM”对象没有属性“parameters” 我有两个代码文件,train.py和lstm_class.py包含lstm类。我会尽量提供一个最低限度的工作例子,让我知道如果任何其他信息是有用的 lstm_class.py中的代码: import to
import torch.nn as nn
class LSTM(nn.Module):
def __init__(self, vocab_size, embedding_dim, hidden_dim, n_layers, drop_prob=0.2):
super(LSTM, self).__init__()
# network size parameters
self.n_layers = n_layers
self.hidden_dim = hidden_dim
self.vocab_size = vocab_size
self.embedding_dim = embedding_dim
# the layers of the network
self.embedding = nn.Embedding(self.vocab_size, self.embedding_dim)
self.lstm = nn.LSTM(self.embedding_dim, self.hidden_dim, self.n_layers, dropout=drop_prob, batch_first=True)
self.dropout = nn.Dropout(drop_prob)
self.fc = nn.Linear(self.hidden_dim, self.vocab_size)
def forward(self, input, hidden):
# Defines forward pass, probably isn't relevant
def init_hidden(self, batch_size):
#Initializes hidden state, probably isn't relevant
train.py中的代码
import torch
import torch.optim
import torch.nn as nn
import lstm_class
vocab_size = 1000
embedding_dim = 256
hidden_dim = 256
n_layers = 2
net = lstm_class.LSTM(vocab_size, embedding_dim, hidden_dim, n_layers)
optimizer = torch.optim.Adam(net.paramters(), lr=learning_rate)
我得到了上面最后一行的错误。完整错误消息:
Traceback (most recent call last):
File "train.py", line 58, in <module>
optimizer = torch.optim.Adam(net.paramters(), lr=learning_rate)
File "/usr/local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 771, in __getattr__
raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
torch.nn.modules.module.ModuleAttributeError: 'LSTM' object has no attribute 'paramters'
如有任何关于如何解决此问题的提示,将不胜感激。同样如上所述,如果还有其他相关内容,请告诉我。谢谢这不是net.parameters,这是net.parameters:这不是net.parameters,这是net.parameters:谢谢!不知道我写这篇文章的时候怎么没有意识到。谢谢!不知道我写这篇文章的时候怎么没有意识到。