用Pyorch写一段训练LSTM网络的代码
时间: 2023-07-20 12:11:05 浏览: 111
下面是一个使用 PyTorch 训练 LSTM 网络的示例代码:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, Dataset
# 定义 LSTM 模型
class LSTM(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, output_size):
super(LSTM, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
out, _ = self.lstm(x, (h0, c0))
out = self.fc(out[:, -1, :])
return out
# 定义训练数据集
class MyDataset(Dataset):
def __init__(self, data):
self.data = data
def __getitem__(self, index):
x = torch.Tensor(self.data[index][0])
y = torch.Tensor(self.data[index][1])
return x, y
def __len__(self):
return len(self.data)
# 定义超参数
input_size = 1
hidden_size = 32
num_layers = 2
output_size = 1
learning_rate = 0.001
num_epochs = 100
# 准备数据
data = [(torch.randn(10, input_size), torch.randn(1)) for _ in range(100)]
train_dataset = MyDataset(data)
train_loader = DataLoader(train_dataset, batch_size=16, shuffle=True)
# 初始化模型和优化器
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = LSTM(input_size, hidden_size, num_layers, output_size).to(device)
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# 训练模型
for epoch in range(num_epochs):
for i, (inputs, labels) in enumerate(train_loader):
inputs = inputs.to(device)
labels = labels.to(device)
# 前向传播
outputs = model(inputs)
loss = criterion(outputs, labels)
# 反向传播和优化
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 10 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, len(train_loader), loss.item()))
# 预测新数据
test_inputs = torch.randn(10, input_size).to(device)
test_outputs = model(test_inputs)
print('Test Input: {}'.format(test_inputs))
print('Test Output: {}'.format(test_outputs))
```
在这个示例代码中,我们定义了一个简单的 LSTM 模型,使用随机数据进行训练,并使用均方误差作为损失函数,使用 Adam 优化器进行优化。在训练过程中,我们使用 DataLoader 加载数据,并将数据传递给模型进行训练。最后,我们使用训练好的模型对新的数据进行预测。
阅读全文