# -*- coding:utf-8 -*-''' word embedding测试 在GTX960上,18s一轮 经过30轮迭代,训练集准确率为98.41%,测试集准确率为89.03% Dropout不能用太多,否则信息损失太严重 '''import numpy as npimport pandas as pdimport jieba pos = pd.read_excel('pos.xls', header=None) pos['label'] = 1neg = pd.read_excel('neg.xls', header=None) neg['label'] = 0all_ = pos.append(neg, ignore_index=True) all_['words'] = all_[0].apply(lambda s: list(jieba.cut(s))) #调用结巴分词maxlen = 100 #截断词数min_count = 5 #出现次数少于该值的词扔掉。这是最简单的降维方法content = []for i in all_['words']: content.extend(i) abc = pd.Series(content).value_counts() abc = abc[abc >= min_count] abc[:] = range(1, len(abc)+1) abc[''] = 0 #添加空字符串用来补全word_set = set(abc.index)def doc2num(s, maxlen): s = [i for i in s if i in word_set] s = s[:maxlen] + ['']*max(0, maxlen-len(s)) return list(abc[s]) all_['doc2num'] = all_['words'].apply(lambda s: doc2num(s, maxlen))#手动打乱数据idx = range(len(all_)) np.random.shuffle(idx) all_ = all_.loc[idx]#按keras的输入要求来生成数据x = np.array(list(all_['doc2num'])) y = np.array(list(all_['label'])) y = y.reshape((-1,1)) #调整标签形状from keras.models import Sequentialfrom keras.layers import Dense, Activation, Dropout, Embeddingfrom keras.layers import LSTM#建立模型model = Sequential() model.add(Embedding(len(abc), 256, input_length=maxlen)) model.add(LSTM(128)) model.add(Dropout(0.5)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) batch_size = 128train_num = 15000model.fit(x[:train_num], y[:train_num], batch_size = batch_size, nb_epoch=30) model.evaluate(x[train_num:], y[train_num:], batch_size = batch_size)def predict_one(s): #单个句子的预测函数 s = np.array(doc2num(list(jieba.cut(s)), maxlen)) s = s.reshape((1, s.shape[0])) return model.predict_classes(s, verbose=0)[0][0]