lstm pytorch代码
时间: 2023-09-23 10:10:03 浏览: 141
lstm+crf、bilstm+crf 、LSTM CRF pytorch 命名实体识别代码
5星 · 资源好评率100%
以下是一个简单的 LSTM 模型的 PyTorch 代码示例:
```
import torch
import torch.nn as nn
# 定义 LSTM 模型类
class LSTMModel(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(LSTMModel, self).__init__()
self.hidden_dim = hidden_dim
self.lstm = nn.LSTM(input_dim, hidden_dim, batch_first=True)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
h0 = torch.zeros(1, x.size(0), self.hidden_dim).requires_grad_()
c0 = torch.zeros(1, x.size(0), self.hidden_dim).requires_grad_()
out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))
out = self.fc(out[:, -1, :])
return out
# 定义训练函数
def train(model, train_loader, learning_rate, num_epochs):
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
for epoch in range(num_epochs):
for i, (inputs, labels) in enumerate(train_loader):
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, len(train_loader), loss.item()))
# 定义测试函数
def test(model, test_loader):
with torch.no_grad():
correct = 0
total = 0
for inputs, labels in test_loader:
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Test Accuracy of the model on the test images: {} %'.format(100 * correct / total))
# 主函数
if __name__ == '__main__':
# 定义模型参数
input_dim = 28
hidden_dim = 100
output_dim = 10
num_epochs = 5
learning_rate = 0.001
# 加载数据
train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=transforms.ToTensor(), download=True)
test_dataset = torchvision.datasets.MNIST(root='./data', train=False, transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=100, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=100, shuffle=False)
# 实例化模型并训练
model = LSTMModel(input_dim, hidden_dim, output_dim)
train(model, train_loader, learning_rate, num_epochs)
# 测试模型
test(model, test_loader)
```
以上代码中实现了一个简单的 LSTM 模型,并使用 PyTorch 中的 DataLoader 加载了 MNIST 数据集进行训练和测试。
阅读全文