LSTM 文本情感分析/序列分类 Keras
2017-04-24 13:12
549 查看
LSTM 文本情感分析/序列分类 Keras
请参考 http://spaces.ac.cn/archives/3414/neg.xls是这样的
pos.xls是这样的
neg=pd.read_excel(‘neg.xls’,header=None,index=None)
pos=pd.read_excel(‘pos.xls’,header=None,index=None) #读取训练语料完毕
pos[‘mark’]=1
neg[‘mark’]=0 #给训练语料贴上标签
pn=pd.concat([pos,neg],ignore_index=True) #合并语料
neglen=len(neg)
poslen=len(pos) #计算语料数目
cw = lambda x: list(jieba.cut(x)) #定义分词函数
pn[‘words’] = pn[0].apply(cw)
comment = pd.read_excel(‘sum.xls’) #读入评论内容
#comment = pd.read_csv(‘a.csv’, encoding=’utf-8′)
comment = comment[comment[‘rateContent’].notnull()] #仅读取非空评论
comment[‘words’] = comment[‘rateContent’].apply(cw) #评论分词
d2v_train = pd.concat([pn[‘words’], comment[‘words’]], ignore_index = True)
w = [] #将所有词语整合在一起
for i in d2v_train:
w.extend(i)
dict = pd.DataFrame(pd.Series(w).value_counts()) #统计词的出现次数
del w,d2v_train
dict[‘id’]=list(range(1,len(dict)+1))
get_sent = lambda x: list(dict[‘id’][x])
pn[‘sent’] = pn[‘words’].apply(get_sent)
maxlen = 50
print “Pad sequences (samples x time)”
pn[‘sent’] = list(sequence.pad_sequences(pn[‘sent’], maxlen=maxlen))
x = np.array(list(pn[‘sent’]))[::2] #训练集
y = np.array(list(pn[‘mark’]))[::2]
xt = np.array(list(pn[‘sent’]))[1::2] #测试集
yt = np.array(list(pn[‘mark’]))[1::2]
xa = np.array(list(pn[‘sent’])) #全集
ya = np.array(list(pn[‘mark’]))
print ‘Build model…’
model = Sequential()
model.add(Embedding(len(dict)+1, 256))
model.add(LSTM(256, 128)) # try using a GRU instead, for fun
model.add(Dropout(0.5))
model.add(Dense(128, 1))
model.add(Activation(‘sigmoid’))
model.compile(loss=’binary_crossentropy’, optimizer=’adam’, class_mode=”binary”)
print ‘Fit model…’
model.fit(xa, ya, batch_size=32, nb_epoch=4) #训练时间为若干个小时
classes = model.predict_classes(xa)
acc = np_utils.accuracy(classes, ya)
print ‘Test accuracy:’, acc
可以试一试
w = [] #将所有词语整合在一起
for i in d2v_train:
w.extend(i)
newList = list(set(w))
print “newlist len is”
print len(newList)
dict = pd.DataFrame(pd.Series(w).value_counts()) #统计词的出现次数
print type(dict)
print len(dict)
可以发现print len(newList)结果和print len(dict) 也就是说dict的长度就是所有不重复词语的distinct的长度。
主要有一个这个函数 sequence.pad_sequences https://keras.io/preprocessing/sequence/#pad_sequences http://www.360doc.com/content/16/0714/10/1317564_575385964.shtml
如果指定了参数maxlen,比如这里maxlen为50,那么意思就是这里每句话只截50个单词,后面就不要了,如果一句话不足50个单词,则用0补齐。
首先,Word2Vec 将词语对应一个多维向量,
model.add(Embedding(len(dict)+1, 256))
参数参考 http://www.360doc.com/content/16/0714/09/1317564_575385061.shtml http://blog.csdn.net/niuwei22007/article/details/49406355
然后
model.add(LSTM(256, 128)) # try using a GRU instead, for fun
model.add(Dropout(0.5))
model.add(Dense(128, 1))
model.add(Activation(‘sigmoid’))
整个流程对应下图
结果
再看一看keras自带的例子:imdb_lstm
maxlen = 100
print(“Pad sequences (samples x time)”)
X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
X_test = sequence.pad_sequences(X_test, maxlen=maxlen)
print(‘X_train shape:’, X_train.shape)
print(‘X_test shape:’, X_test.shape)
print(‘Build model…’)
model = Sequential()
model.add(Embedding(max_features, 128))
model.add(LSTM(128, 128)) # try using a GRU instead, for fun
model.add(Dropout(0.5))
model.add(Dense(128, 1))
model.add(Activation(‘sigmoid’))
同样的道理
如果训练样本较少,为了防止模型过拟合,Dropout可以作为一种trikc供选择。在每个训练批次中,通过忽略一半的特征检测器(让一半的隐层节点值为0),可以明显地减少过拟合现象。这种方式可以减少特征检测器间的相互作用,检测器相互作用是指某些检测器依赖其他检测器才能发挥作用。
相关文章推荐
- Keras + LSTM + 词向量 情感分类/情感分析实验
- Keras实现的文本情感分类例子
- 利用 Keras 下的 LSTM 进行情感分析
- 基于LSTM搭建一个文本情感分类的深度学习模型:准确率往往有95%以上
- 文本分类之情感分析– 去除低信息量的特征
- 文本分类之情感分析– 停用词和惯用语
- 用keras实现lstm 利用Keras下的LSTM进行情感分析
- 情感分析的新方法,使用word2vec对微博文本进行情感分析和分类
- 文本情感分类---搭建LSTM(深度学习模型)做文本情感分类的代码
- 【keras】序贯Sequential模型实例之用于序列分类的栈式LSTM
- 文本分类之情感分析– 准确率和召回率
- 【keras】序贯Sequential模型实例之使用LSTM的序列分类
- keras实现aspect level中文文本情感分类-源自EMNLP2016
- lstm 做 文本的情感分析
- Tensorflow学习笔记--使用keras完成文本情感分类问题
- 文本分类之情感分析 – 朴素贝叶斯分类器
- 基于Attention Model的Aspect level文本情感分类---用Python+Keras实现
- 自然语言处理课程作业 中文文本情感分类
- 基于文本挖掘和情感分析的股票价格预测系统(一)
- 基于斯坦福CoreNLP的情感分析(文本分析)