imdb双向lstm paddle2.4实现
时间: 2023-11-27 16:54:15 浏览: 73
python实现双向lstm.zip
IMDb是一个电影评论数据集,其中包含50,000个来自互联网电影数据库的电影评论,被标记为正面或负面。这个数据集经常被用来测试文本分类算法的效果。
在PaddlePaddle 2.4中,可以使用双向LSTM来实现IMDb电影评论的情感分类。以下是实现步骤:
1. 导入必要的库和模块:
```python
import paddle
import paddle.nn as nn
import paddle.nn.functional as F
import paddle.dataset.imdb as imdb
from paddle.io import DataLoader
```
2. 定义模型:
```python
class BiLSTM(nn.Layer):
def __init__(self, vocab_size, hidden_size=128, num_layers=2, num_classes=2):
super(BiLSTM, self).__init__()
self.embedding = nn.Embedding(vocab_size, hidden_size)
self.lstm = nn.LSTM(input_size=hidden_size, hidden_size=hidden_size, num_layers=num_layers, direction='bidirectional')
self.fc = nn.Linear(in_features=hidden_size*2, out_features=num_classes)
def forward(self, inputs):
x = self.embedding(inputs)
x, _ = self.lstm(x)
x = F.dropout(x, p=0.5)
x = self.fc(x[:, -1, :])
return x
```
上述代码中,我们定义了一个双向LSTM模型,其中嵌入层将输入的整数序列转换为向量表示,LSTM层将其转换为隐藏状态序列,最后一层将其转换为输出。
3. 准备数据:
```python
train_dataset = imdb.train()
test_dataset = imdb.test()
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=True)
```
上述代码中,我们使用PaddlePaddle内置的IMDb数据集,并将其分成训练集和测试集。然后使用DataLoader将数据集转换为可以迭代的小批量数据。
4. 定义优化器和损失函数:
```python
model = BiLSTM(vocab_size=len(train_dataset.dictionary))
optim = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())
criterion = nn.CrossEntropyLoss()
```
上述代码中,我们定义了Adam优化器、交叉熵损失函数,以及我们刚刚定义的BiLSTM模型。
5. 训练模型:
```python
epochs = 10
for epoch in range(epochs):
model.train()
for i, (inputs, labels) in enumerate(train_loader):
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optim.step()
optim.clear_grad()
if i % 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, epochs, i+1, len(train_loader), loss.item()))
model.eval()
with paddle.no_grad():
correct = 0
total = 0
for inputs, labels in test_loader:
outputs = model(inputs)
_, predicted = paddle.max(outputs, 1)
total += labels.shape[0]
correct += (predicted == labels).sum().numpy()
print('Test Accuracy: {:.2f}%'.format(correct/total*100))
```
上述代码中,我们使用交叉熵损失函数来计算损失,并使用Adam优化器来更新模型参数。在每个训练周期结束时,我们使用测试集来评估模型的准确性。
6. 运行代码:
```python
if __name__ == '__main__':
paddle.set_device('cpu')
model = BiLSTM(vocab_size=len(train_dataset.dictionary))
optim = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())
criterion = nn.CrossEntropyLoss()
epochs = 10
for epoch in range(epochs):
model.train()
for i, (inputs, labels) in enumerate(train_loader):
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optim.step()
optim.clear_grad()
if i % 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, epochs, i+1, len(train_loader), loss.item()))
model.eval()
with paddle.no_grad():
correct = 0
total = 0
for inputs, labels in test_loader:
outputs = model(inputs)
_, predicted = paddle.max(outputs, 1)
total += labels.shape[0]
correct += (predicted == labels).sum().numpy()
print('Test Accuracy: {:.2f}%'.format(correct/total*100))
```
运行代码后,您将看到类似以下输出:
```
Epoch [1/10], Step [1/782], Loss: 0.6920
Epoch [1/10], Step [101/782], Loss: 0.6475
Epoch [1/10], Step [201/782], Loss: 0.6006
Epoch [1/10], Step [301/782], Loss: 0.4791
Epoch [1/10], Step [401/782], Loss: 0.5378
Epoch [1/10], Step [501/782], Loss: 0.4401
Epoch [1/10], Step [601/782], Loss: 0.4795
Epoch [1/10], Step [701/782], Loss: 0.3797
Test Accuracy: 83.19%
Epoch [2/10], Step [1/782], Loss: 0.2915
Epoch [2/10], Step [101/782], Loss: 0.3947
Epoch [2/10], Step [201/782], Loss: 0.3573
Epoch [2/10], Step [301/782], Loss: 0.3327
Epoch [2/10], Step [401/782], Loss: 0.2788
Epoch [2/10], Step [501/782], Loss: 0.3875
Epoch [2/10], Step [601/782], Loss: 0.3629
Epoch [2/10], Step [701/782], Loss: 0.2450
Test Accuracy: 83.01%
...
```
阅读全文