使用Paddle内置的单向LSTM进行文本分类
时间: 2023-07-11 20:21:06 浏览: 133
基于LSTM的文本分类.zip
好的,您可以参考以下代码实现使用Paddle内置的单向LSTM进行文本分类:
```python
import paddle
import paddle.nn as nn
class TextLSTM(nn.Layer):
def __init__(self, vocab_size, num_classes, emb_dim=128, hidden_size=128, num_layers=1):
super(TextLSTM, self).__init__()
self.embedding = nn.Embedding(vocab_size, emb_dim, padding_idx=0)
self.lstm = nn.LSTM(input_size=emb_dim, hidden_size=hidden_size, num_layers=num_layers, direction='forward')
self.fc = nn.Linear(hidden_size, num_classes)
def forward(self, inputs):
x = self.embedding(inputs)
x, _ = self.lstm(x)
x = self.fc(x[:, -1, :])
return x
```
其中,TextLSTM是一个继承自paddle.nn.Layer的类,其中包含了Embedding层、单向LSTM层和全连接层。在forward()函数中,我们首先将输入的整数序列进行embedding,然后输入到LSTM中进行序列建模,最终取出最后一个时间步的输出进行分类。
使用该模型进行训练和评估的代码可以参考以下示例:
```python
import numpy as np
import paddle.optimizer as optim
# 定义超参数
batch_size = 64
learning_rate = 0.001
epochs = 10
# 加载数据
train_data = ...
dev_data = ...
test_data = ...
# 初始化模型
model = TextLSTM(len(word_dict), len(label_dict))
# 定义优化器和损失函数
optimizer = optim.Adam(parameters=model.parameters(), learning_rate=learning_rate)
criterion = nn.CrossEntropyLoss()
# 训练模型
for epoch in range(epochs):
train_loss = 0.0
train_acc = 0.0
train_steps = 0
model.train()
for batch in get_batch(train_data, batch_size):
inputs, labels = batch
inputs = paddle.to_tensor(inputs)
labels = paddle.to_tensor(labels)
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
optimizer.clear_grad()
train_loss += loss.numpy()[0]
train_acc += np.sum(np.argmax(outputs.numpy(), axis=1) == labels.numpy())
train_steps += 1
train_loss /= train_steps
train_acc /= len(train_data)
# 在开发集上评估模型
dev_loss = 0.0
dev_acc = 0.0
dev_steps = 0
model.eval()
for batch in get_batch(dev_data, batch_size):
inputs, labels = batch
inputs = paddle.to_tensor(inputs)
labels = paddle.to_tensor(labels)
outputs = model(inputs)
loss = criterion(outputs, labels)
dev_loss += loss.numpy()[0]
dev_acc += np.sum(np.argmax(outputs.numpy(), axis=1) == labels.numpy())
dev_steps += 1
dev_loss /= dev_steps
dev_acc /= len(dev_data)
print("Epoch %d, Train Loss: %.4f, Train Acc: %.4f, Dev Loss: %.4f, Dev Acc: %.4f" % (
epoch+1, train_loss, train_acc, dev_loss, dev_acc))
# 在测试集上评估模型
test_loss = 0.0
test_acc = 0.0
test_steps = 0
model.eval()
for batch in get_batch(test_data, batch_size):
inputs, labels = batch
inputs = paddle.to_tensor(inputs)
labels = paddle.to_tensor(labels)
outputs = model(inputs)
loss = criterion(outputs, labels)
test_loss += loss.numpy()[0]
test_acc += np.sum(np.argmax(outputs.numpy(), axis=1) == labels.numpy())
test_steps += 1
test_loss /= test_steps
test_acc /= len(test_data)
print("Test Loss: %.4f, Test Acc: %.4f" % (test_loss, test_acc))
```
其中,get_batch()函数是一个用于将数据划分成batch的函数,具体实现可以根据自己的需求进行编写。在训练过程中,我们首先将数据传递给模型,得到模型的预测结果和损失值,然后根据损失值更新模型参数。在评估过程中,我们不需要更新模型参数,只需要得到模型的预测结果和损失值即可。最终,我们可以在测试集上评估模型的性能。
阅读全文