import numpy as np import torch import torch.nn as nn import torch.optim as optim class RNN(nn.Module): def init(self, input_size, hidden_size, output_size): super(RNN, self).init() self.hidden_size = hidden_size self.i2h = nn.Linear(input_size + hidden_size, hidden_size) self.i2o = nn.Linear(input_size + hidden_size, output_size) self.softmax = nn.LogSoftmax(dim=1) def forward(self, input, hidden): combined = torch.cat((input, hidden), 1) hidden = self.i2h(combined) output = self.i2o(combined) output = self.softmax(output) return output, hidden def begin_state(self, batch_size): return torch.zeros(batch_size, self.hidden_size) #定义数据集 data = """he quick brown fox jumps over the lazy dog's back""" #定义字符表 tokens = list(set(data)) tokens.sort() token2idx = {t: i for i, t in enumerate(tokens)} idx2token = {i: t for i, t in enumerate(tokens)} #将字符表转化成独热向量 one_hot_matrix = np.eye(len(tokens)) #定义模型参数 input_size = len(tokens) hidden_size = 128 output_size = len(tokens) learning_rate = 0.01 #初始化模型和优化器 model = RNN(input_size, hidden_size, output_size) optimizer = optim.Adam(model.parameters(), lr=learning_rate) criterion = nn.NLLLoss() #训练模型 for epoch in range(1000): model.train() state = model.begin_state(1) loss = 0 for ii in range(len(data) - 1): x_input = one_hot_matrix[token2idx[data[ii]]] y_target = torch.tensor([token2idx[data[ii + 1]]]) x_input = x_input.reshape(1, 1, -1) y_target = y_target.reshape(1) pred, state = model(torch.from_numpy(x_input), state) loss += criterion(pred, y_target) optimizer.zero_grad() loss.backward() optimizer.step() if epoch % 100 == 0: print(f"Epoch {epoch}, Loss: {loss.item()}")代码运行报错,请修改
时间: 2024-01-15 07:04:55 浏览: 164
在类定义中,`__init__` 方法的名称拼写错误了,应该是两个下划线。将 `init` 修改为 `__init__` 即可。
修改后的代码如下:
```python
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(RNN, self).__init__()
self.hidden_size = hidden_size
self.i2h = nn.Linear(input_size + hidden_size, hidden_size)
self.i2o = nn.Linear(input_size + hidden_size, output_size)
self.softmax = nn.LogSoftmax(dim=1)
def forward(self, input, hidden):
combined = torch.cat((input, hidden), 1)
hidden = self.i2h(combined)
output = self.i2o(combined)
output = self.softmax(output)
return output, hidden
def begin_state(self, batch_size):
return torch.zeros(batch_size, self.hidden_size)
#定义数据集
data = """he quick brown fox jumps over the lazy dog's back"""
#定义字符表
tokens = list(set(data))
tokens.sort()
token2idx = {t: i for i, t in enumerate(tokens)}
idx2token = {i: t for i, t in enumerate(tokens)}
#将字符表转化成独热向量
one_hot_matrix = np.eye(len(tokens))
#定义模型参数
input_size = len(tokens)
hidden_size = 128
output_size = len(tokens)
learning_rate = 0.01
#初始化模型和优化器
model = RNN(input_size, hidden_size, output_size)
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
criterion = nn.NLLLoss()
#训练模型
for epoch in range(1000):
model.train()
state = model.begin_state(1)
loss = 0
for ii in range(len(data) - 1):
x_input = one_hot_matrix[token2idx[data[ii]]]
y_target = torch.tensor([token2idx[data[ii + 1]]])
x_input = x_input.reshape(1, 1, -1)
y_target = y_target.reshape(1)
pred, state = model(torch.from_numpy(x_input), state)
loss += criterion(pred, y_target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if epoch % 100 == 0:
print(f"Epoch {epoch}, Loss: {loss.item()}")
```
阅读全文