前言
大家好,我是阿光。
本专栏整理了《PyTorch深度学习项目实战100例》,内包含了各种不同的深度学习项目,包含项目原理以及源码,每一个项目实例都附带有完整的代码+数据集。
正在更新中~ ✨
🚨 我的项目环境:
- 平台:Windows10
- 语言环境:python3.7
- 编译器:PyCharm
- PyTorch版本:1.8.1
💥 项目专栏:【PyTorch深度学习项目实战100例】
一、使用LSTM进行文本情感分析
本项目是使用LSTM进行文本情感分析,针对数据为购物评价信息,可以判断出语料所含情感的积极性,实现思路就是针对评价进行二分类,也就是简单的指出评价是正面的还是负面的。
二、数据集介绍
本项目中使用的数据是电商网站中某个商品的评论,该数据集的下载网址为:下载链接 ,该数据集一共有4310条评论数据,文本的情感分为两类:“正面”和“反面”,该数据集的前几行如下:
三、项目实现思路
本项目使用的是LSTM,如果使用普通MLP网络依然可以实现二分类,但是对于文本信息,他不同词之间是存在语义关系的,并不是独立的,每个词会受语境及上下文
所影响,如果使用传统模型是捕捉不到这层关系。
所以考虑使用循环神经网络,可以将不同时间步的数据向下进行传递,将上文语境信息向后传入进行分析。
四、加载文本数据
由于数据集中是每一个样本为一句话,这样是不可以进行训练的,所以需要将其转化为数值信息。
- 首先需要获得
词汇表
,就是训练样本中所有出现的字 - 然后将其转化为
字典
,键为字,值为对应的编号,用于进行映射 - 将每一句话进行映射形成对应的数值编号
- 如果每一行的字数不够
input_shape
,那么就使用0进行填补
,保持送入网络模型中的数据的维度是一致的
# 加载文本数据
def load_data(file_path, input_shape=20):
df = pd.read_csv(file_path)
# 标签及词汇表
labels, vocabulary = list(df['label'].unique()), list(df['evaluation'].unique())
# 构造字符级别的特征
string = ''
for word in vocabulary:
string += word
# 所有的词汇表
vocabulary = set(string)
# word2idx 将字映射为索引
word_dictionary = {word: i + 1 for i, word in enumerate(vocabulary)}
with open('word_dict.pk', 'wb') as f:
pickle.dump(word_dictionary, f)
# idx2word 将索引映射为字
inverse_word_dictionary = {i + 1: word for i, word in enumerate(vocabulary)}
# label2idx 将正反面映射为0和1
label_dictionary = {label: i for i, label in enumerate(labels)}
with open('label_dict.pk', 'wb') as f:
pickle.dump(label_dictionary, f)
# idx2label 将0和1映射为正反面
output_dictionary = {i: labels for i, labels in enumerate(labels)}
# 训练数据中所有词的个数
vocab_size = len(word_dictionary.keys()) # 词汇表大小
# 标签类别,分别为正、反面
label_size = len(label_dictionary.keys()) # 标签类别数量
# 序列填充,按input_shape填充,长度不足的按0补充
# 将一句话映射成对应的索引 [0,24,63...]
x = [[word_dictionary[word] for word in sent] for sent in df['evaluation']]
# 如果长度不够input_shape,使用0进行填充
x = pad_sequences(maxlen=input_shape, sequences=x, padding='post', value=0)
# 形成标签0和1
y = [[label_dictionary[sent]] for sent in df['label']]
# y = [np_utils.to_categorical(label, num_classes=label_size) for label in y]
y = np.array(y)
return x, y, output_dictionary, vocab_size, label_size, inverse_word_dictionary
五、定义网络结构
项目中使用的模型是LSTM,在模型中我们定义了三个组件,分别是embedding层
,lstm层
和全连接层
。
- Embedding层:将每个词生成对应的
嵌入向量
,就是利用一个连续型向量来表示每个词 - Lstm层:提取语句中的语义信息
- Linear层:将结果映射成2大小用于二分类,即正反面的概率
注意:在LSTM网络中返回的值为最后一个时间片
的输出,而不是将整个output全部输出,因为我们是需要捕捉整个语句的语义信息,并不是获得特定时间片的数据。
六、模型训练
# 6.模型训练
model = LSTM(vocab_size=len(inverse_word_dictionary), hidden_dim=hidden_dim, num_layers=num_layers,
embedding_dim=embedding_dim, output_dim=output_dim)
Configimizer = optim.Adam(model.parameters(), lr=lr) # 优化器
criterion = nn.CrossEntropyLoss() # 多分类损失函数
model.to(device)
loss_meter = meter.AverageValueMeter()
best_acc = 0 # 保存最好准确率
best_model = None # 保存对应最好准确率的模型参数
for epoch in range(epochs):
model.train() # 开启训练模式
epoch_acc = 0 # 每个epoch的准确率
epoch_acc_count = 0 # 每个epoch训练的样本数
train_count = 0 # 用于计算总的样本数,方便求准确率
loss_meter.reset()
train_bar = tqdm(train_loader) # 形成进度条
for data in train_bar:
x_train, y_train = data # 解包迭代器中的X和Y
x_input = x_train.long().transpose(1, 0).contiguous()
x_input = x_input.to(device)
Configimizer.zero_grad()
# 形成预测结果
output_ = model(x_input)
# 计算损失
loss = criterion(output_, y_train.long().view(-1))
loss.backward()
Configimizer.step()
loss_meter.add(loss.item())
# 计算每个epoch正确的个数
epoch_acc_count += (output_.argmax(axis=1) == y_train.view(-1)).sum()
train_count += len(x_train)
# 每个epoch对应的准确率
epoch_acc = epoch_acc_count / train_count
# 打印信息
print("【EPOCH: 】%s" % str(epoch + 1))
print("训练损失为%s" % (str(loss_meter.mean)))
print("训练精度为%s" % (str(epoch_acc.item() * 100)[:5]) + '%')
# 保存模型及相关信息
if epoch_acc > best_acc:
best_acc = epoch_acc
best_model = model.state_dict()
# 在训练结束保存最优的模型参数
if epoch == epochs - 1:
# 保存模型
torch.save(best_model, './best_model.pkl')
七、语句测试
实现规定好input_shape
,如果不够使用0进行填补,方便送入网络当中。
读取模型以及相应的词序号映射信息
,然后将我们待测试的话转成相应的tensor,送入网络中,然后的tensor为2维
,即对应正面和反面的概率,然后使用argmax函数获得最大值对应的索引。
完整源码
【PyTorch深度学习项目实战100例】—— 基于pytorch使用LSTM进行文本情感分析 | 第7例_咕 嘟的博客-CSDN博客_pytorchlstm中文文本情感分析