欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

NLP文本分类问题-电影评论的情感分析

程序员文章站 2022-06-15 12:25:45
预训练词嵌入数据集是kaggle上的竞赛数据文本分类问题-电影评论的情感分析。https://www.kaggle.com/columbine/imdb-dataset-sentiment-analysis-in-csv-format。从头开始学习嵌入import pandas as pdimport numpy as np#读取csv文件train = pd.read_csv('/kaggle/input/imdb-dataset-sentiment-analysis-in-csv-....

NLP文本分类问题-电影评论的情感分析

预训练词嵌入
数据集是kaggle上的竞赛数据
文本分类问题-电影评论的情感分析。
https://www.kaggle.com/columbine/imdb-dataset-sentiment-analysis-in-csv-format

从头开始学习嵌入

import pandas as pd
import numpy as np

#读取csv文件
train = pd.read_csv('/kaggle/input/imdb-dataset-sentiment-analysis-in-csv-format/Train.csv')
valid = pd.read_csv('/kaggle/input/imdb-dataset-sentiment-analysis-in-csv-format/Valid.csv')             

#训练测试集分离
x_tr, y_tr = train['text'].values, train['label'].values
x_val, y_val = valid['text'].values, valid['label'].values
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences

tokenizer = Tokenizer()

#准备词汇表
tokenizer.fit_on_texts(list(x_tr))

#将文本转换为整数序列
x_tr_seq  = tokenizer.texts_to_sequences(x_tr) 
x_val_seq = tokenizer.texts_to_sequences(x_val)

#填充以准备相同长度的序列
x_tr_seq  = pad_sequences(x_tr_seq, maxlen=100)
x_val_seq = pad_sequences(x_val_seq, maxlen=100)

【text.Tokenizer类】
函数:
fit_on_text(texts) 使用一系列文档来生成token词典,texts为list类,每个元素为一个文档。
texts_to_sequences(texts) 将多个文档转换为word下标的向量形式,shape为[len(texts),len(text)] – (文档数,每条文档的长度)
texts_to_matrix(texts) 将多个文档转换为矩阵表示,shape为[len(texts),num_words]
成员变量
document_count 处理的文档数量
word_index 一个dict,保存所有word对应的编号id,从1开始
word_counts 一个dict,保存每个word在所有文档中出现的次数
word_docs 一个dict,保存每个word出现的文档的数量
index_docs 一个dict,保存word的id出现的文档的数量
【sequence模块】
pad_sequences(sequences, maxlen, padding=’pre’, truncating=’pre’, value=0.) 将序列填充到maxlen长度,padding取值有pre|post,value指定用何值填充的值
表示,将长度不足 maxlen的新闻用 value填充,pre在前面填充,post在后面填充。

print(x_tr_seq)

数字表示,每个句子在token词典下的下标。比如477表示对应与token词典里第477的单词。
NLP文本分类问题-电影评论的情感分析

size_of_vocabulary=len(tokenizer.word_index) + 1 #+1用于填充
print(size_of_vocabulary)
#112204
#深度学习库
from keras.models import *
from keras.layers import *
from keras.callbacks import *

model=Sequential()

#嵌入层
model.add(Embedding(size_of_vocabulary,300,input_length=100,trainable=True)) 

#lstm层
model.add(LSTM(128,return_sequences=True,dropout=0.2))

#Global Max池化
model.add(GlobalMaxPooling1D())

#Dense层
model.add(Dense(64,activation='relu')) 
model.add(Dense(1,activation='sigmoid')) 

#添加损失函数、度量、优化器
model.compile(optimizer='adam', loss='binary_crossentropy',metrics=["acc"]) 

#添加回调
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1,patience=3)  
mc=ModelCheckpoint('best_model.h5', monitor='val_acc', mode='max', save_best_only=True,verbose=1)  

#输出模型
print(model.summary())

NLP文本分类问题-电影评论的情感分析

history = model.fit(np.array(x_tr_seq),np.array(y_tr),batch_size=128,epochs=10,validation_data=(np.array(x_val_seq),np.array(y_val)),verbose=1,callbacks=[es,mc])

NLP文本分类问题-电影评论的情感分析

from keras.models import load_model
model = load_model('best_model.h5')

#评估
_,val_acc = model.evaluate(x_val_seq,y_val, batch_size=128)
print(val_acc)

NLP文本分类问题-电影评论的情感分析

用GLoVE预训练的词嵌入

glove.6B.300d是一个词向量的txt文档。里面有很多单词,每个单词有一个向量。

# 将整个嵌入加载到内存中
embeddings_index = dict()
f = open('../input/glove6b/glove.6B.300d.txt')

for line in f:
    values = line.split()
    word = values[0]
    coefs = np.asarray(values[1:], dtype='float32')
    embeddings_index[word] = coefs

f.close()
print('Loaded %s word vectors.' % len(embeddings_index))
# 为文档中的单词创建权重矩阵
embedding_matrix = np.zeros((size_of_vocabulary, 300))

for word, i in tokenizer.word_index.items():
    embedding_vector = embeddings_index.get(word)
    if embedding_vector is not None:
        embedding_matrix[i] = embedding_vector

也就是嵌入层使用的是Glove的权重,不需要再训练更新。其他层就需要训练

model=Sequential()

#嵌入层
model.add(Embedding(size_of_vocabulary,300,weights=[embedding_matrix],input_length=100,trainable=False)) 

#lstm层
model.add(LSTM(128,return_sequences=True,dropout=0.2))

#Global Max池化
model.add(GlobalMaxPooling1D())

#Dense层
model.add(Dense(64,activation='relu')) 
model.add(Dense(1,activation='sigmoid')) 

#添加损失函数、度量、优化器
model.compile(optimizer='adam', loss='binary_crossentropy',metrics=["acc"]) 

#添加回调
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1,patience=3)  
mc=ModelCheckpoint('best_model.h5', monitor='val_acc', mode='max', save_best_only=True,verbose=1)  

#输出模型
print(model.summary())
history = model.fit(np.array(x_tr_seq),np.array(y_tr),batch_size=128,epochs=10,validation_data=(np.array(x_val_seq),np.array(y_val)),verbose=1,callbacks=[es,mc])
#加载最佳模型
from keras.models import load_model
model = load_model('best_model.h5')

#评估
_,val_acc = model.evaluate(x_val_seq,y_val, batch_size=128)
print(val_acc)

本文地址:https://blog.csdn.net/weixin_44940421/article/details/107635471