-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_lstm.py
428 lines (370 loc) · 13.9 KB
/
my_lstm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# coding:utf-8
import pandas as pd
import gensim, logging
import numpy as np
from collections import defaultdict
from gensim import corpora
from gensim.corpora.dictionary import Dictionary
from gensim.models.doc2vec import Doc2Vec
from gensim.models.word2vec import LineSentence
from gensim.models.word2vec import Word2Vec
from gensim.models.lsimodel import LsiModel
from gensim.models.tfidfmodel import TfidfModel
from gensim import corpora
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import SVC
from sklearn.decomposition import IncrementalPCA
import time
import re
import jieba
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import SVC
import numpy as np
import xgboost as xgb
import yaml
from scipy.sparse import hstack
from matplotlib import pyplot
from keras.wrappers.scikit_learn import KerasClassifier
from keras.layers.core import *
from keras.layers.embeddings import Embedding
from keras.layers.recurrent import LSTM
from keras.layers.core import Dense, Dropout,Activation
from keras.models import model_from_yaml
from keras.models import Sequential
from keras.optimizers import *
from keras.preprocessing.sequence import pad_sequences
from sklearn.cross_validation import StratifiedKFold
from sklearn.cross_validation import cross_val_score
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import OneHotEncoder
from sklearn.cross_validation import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from scipy import sparse
from data_preprocess import *
#-- Log function
def LogInfo(stri):
print(str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))+' '+stri)
def word2vec(documents, topicNum):
texts = [[word for word in document.split(' ')] for document in documents]
frequency = defaultdict(int)
for text in texts:
for token in text:
#token = int(token)
#print token
frequency[token] += 1
texts = [[token for token in text if frequency[token] >= 20] for text in texts]
print('train Model...')
model = Word2Vec(texts, size=topicNum, window=5, iter = 15, min_count=20, workers=12, seed = 12)#, sample = 1e-5, iter = 10,seed = 1)
path = '../feature/'+str(topicNum)+'w2vModel.m'
model.save(path)
w2vFeature = np.zeros((len(texts), topicNum))
w2vFeatureAvg = np.zeros((len(texts), topicNum))
i = 0
for line in texts:
num = 0
for word in line:
num += 1
vec = model[word]
w2vFeature[i, :] += vec
w2vFeatureAvg[i,:] = w2vFeature[i,:]/num
i += 1
if i%5000 == 0:
print(i)
return w2vFeature, w2vFeatureAvg
def get_pretrained_w2vmodel():
with open('../feature/sgns.weibo.bigram-char') as file:
lines = file.read().split('\n')
# print(lines[0])
w2vmodel = {}
for line in lines[1:]:
vec = line.split()
key = ''.join(vec[:-300])
w2vmodel[key]=list(map(float,vec[-300:]))
print(list(w2vmodel.keys())[:100])
return w2vmodel
def lstm_test():
dim = 100
# preprocess for lstm
print('preprocess data...')
x_train, y_train = load_data()
# print(x_train.shape,x_test.shape)
x_train = preprocess_data(x_train)
texts = [[word for word in document.split(' ')] for document in x_train.astype(str).values]
# texts, label = select_data(texts,label)
frequency = defaultdict(int)
for text in texts:
for token in text:
#token = int(token)
#print token
frequency[token] += 1
texts = [[token for token in text if frequency[token] >= 20] for text in texts]
vocab = set([word for doc in texts for word in doc])
vocab_size = len(vocab)
print('generate embedding_matrix...')
embedding_matrix = np.zeros((vocab_size,dim))
word2index = {}
model = Word2Vec.load('../feature/test/100w2vModel.m')
# model = get_pretrained_w2vmodel()
for i, word in enumerate(vocab):
word2index[word] = i
# if model is selftrained
embedding_matrix[i] = model[word]
# if model is pretrained
# if word in model.keys():
# try:
# embedding_matrix[i] = model[word]
# except:
# print('error: ',word)
# else:
# embedding_matrix[i] = np.zeros(dim)
print('generate encoded_texts...')
encoded_texts = []
for doc in texts:
encoded_doc = []
for word in doc:
encoded_doc.append(word2index[word])
encoded_texts.append(encoded_doc)
# print(encoded_texts[:100])
# max_length = max([len(doc) for doc in texts])
max_length=config['max_length']
print('generate padded_texts...')
padded_texts = pad_sequences(encoded_texts, maxlen=max_length, padding='post')
# x_train = padded_texts[:len(x_train)]
# x_test = padded_texts[len(x_train):]
x_train, x_test, y_train, y_test = train_test_split(padded_texts, y_train, test_size=0.2,random_state=42)
print(len(x_train),len(x_test),len(y_train))
# lstm model structure
print('Construct lstm model...')
model = Sequential()
embedding = Embedding(
input_dim=vocab_size,
output_dim=dim,
mask_zero=True,
weights=[embedding_matrix],
input_length=max_length,
trainable=False)
model.add(embedding)
model.add(LSTM(units=50, activation='sigmoid', recurrent_activation='hard_sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
# Compile and train the model
print('Compiling the Model...')
model.compile(loss='binary_crossentropy',
optimizer='adam',metrics=['accuracy'])
print("Train...")
skf = StratifiedKFold(y_train, n_folds=3, shuffle=True)
new_train = np.zeros((len(x_train),1))
new_test = np.zeros((len(x_test),1))
for i,(trainid,valid) in enumerate(skf):
print('fold' + str(i))
train_x = x_train[trainid]
train_y = y_train[trainid]
val_x = x_train[valid]
model.fit(train_x, train_y,
batch_size=config['batch_size'],
epochs=config['n_epoch'],verbose=1)
new_train[valid] = model.predict_proba(val_x)
new_test += model.predict_proba(x_test)
new_test /= 3
stacks = []
stacks_name = []
stack = np.vstack([new_train,new_test])
stacks.append(stack)
stacks = np.hstack(stacks)
clf_stacks = pd.DataFrame(data=stacks,columns=['lstm'])
clf_stacks.to_csv('../feature/test/lstm_prob1.csv',index=0)
#创建词语字典,并返回每个词语的索引,词向量,以及每个句子所对应的词语索引
def create_dictionaries(document):
data,label = load_data()
data = preprocess_data(data)
texts = [[word for word in document.split(' ')] for document in data.values]
frequency = defaultdict(int)
for text in texts:
for token in text:
#token = int(token)
#print token
frequency[token] += 1
texts = [[token for token in text if frequency[token] >= 20] for text in texts]
vocab = set([word for doc in texts for word in doc])
vocab_size = len(vocab)
embedding_matrix = np.zeros((vocab_size,300))
word2index = {}
model = Word2Vec.load('../feature/100w2vModel.m')
for i, word in enumerate(vocab):
embedding_matrix[i] = model[word]
word2index[word] = i
encoded_texts = []
for doc in document:
encoded_doc = []
for word in doc:
if word in vocab:
encoded_doc.append(word2index[word])
else:
encoded_doc.append(0)
encoded_texts.append(encoded_doc)
# print(encoded_texts[:100])
# max_length = max([len(doc) for doc in texts])
max_length = config['max_length']
padded_texts = pad_sequences(encoded_texts, maxlen=max_length, padding='post')
return padded_texts
def input_transform(strings):
texts = []
for string in strings:
string = re.sub(u"[^\u4E00-\u9FFF]", "", string)
punctuation = """!?。"#$%&'()*+-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘'‛“”„‟…‧﹏"""
string = re.sub(punctuation, "",string)
string =' '.join(jieba.cut(string))
string = re.sub('\s{2,}',' ',string)
string = string.split()
texts.append(string)
# print(texts)
texts = create_dictionaries(texts)
# print(texts)
return texts
def lstm_predict(strings):
print('loading model......')
max_length=config['max_length']
path1 = '../lstm_data/lstm_len'+str(max_length)+'_epoch'+str(config['n_epoch'])+'.yml'
path2 = '../lstm_data/lstm_len'+str(max_length)+'_epoch'+str(config['n_epoch'])+'.h5'
with open(path1, 'r') as f:
yaml_string = yaml.load(f)
model = model_from_yaml(yaml_string)
print('loading weights......')
model.load_weights(path2)
model.compile(loss='binary_crossentropy',
optimizer='adam',metrics=['accuracy'])
data=input_transform(strings)
for i,string in enumerate(strings):
predict_data = data[i].reshape(1,-1)
pro = model.predict(predict_data)[0][0]
result=model.predict_classes(predict_data)
print(string)
if result[0][0]==1:
print('probability:',pro,' positive')
else:
print('probability:',pro,' negative')
def lstm_predict():
dim = 100
# preprocess for lstm
print('preprocess data...')
x_train, y_train = load_data()
x_test,test_id = load_testdata()
# print(x_train.shape,x_test.shape)
x_train = preprocess_data(x_train)
x_test = preprocess_data(x_test)
data = pd.concat([x_train,x_test],axis=0).astype(str)
texts = [[word for word in document.split(' ')] for document in data.values]
# texts, label = select_data(texts,label)
frequency = defaultdict(int)
for text in texts:
for token in text:
#token = int(token)
#print token
frequency[token] += 1
texts = [[token for token in text if frequency[token] >= 20] for text in texts]
vocab = set([word for doc in texts for word in doc])
vocab_size = len(vocab)
print('generate embedding_matrix...')
embedding_matrix = np.zeros((vocab_size,dim))
word2index = {}
model = Word2Vec.load('../feature/predict/100w2vModel.m')
# model = get_pretrained_w2vmodel()
for i, word in enumerate(vocab):
word2index[word] = i
# if model is selftrained
embedding_matrix[i] = model[word]
# if model is pretrained
# if word in model.keys():
# try:
# embedding_matrix[i] = model[word]
# except:
# print('error: ',word)
# else:
# embedding_matrix[i] = np.zeros(dim)
print('generate encoded_texts...')
encoded_texts = []
for doc in texts:
encoded_doc = []
for word in doc:
encoded_doc.append(word2index[word])
encoded_texts.append(encoded_doc)
# print(encoded_texts[:100])
# max_length = max([len(doc) for doc in texts])
max_length=config['max_length']
print('generate padded_texts...')
padded_texts = pad_sequences(encoded_texts, maxlen=max_length, padding='post')
x_train = padded_texts[:len(x_train)]
x_test = padded_texts[len(x_train):]
# x_train, x_test, y_train, y_test = train_test_split(padded_texts, label, test_size=0.2,random_state=42)
print(len(x_train),len(x_test),len(y_train))
# lstm model structure
print('Construct lstm model...')
model = Sequential()
embedding = Embedding(
input_dim=vocab_size,
output_dim=dim,
mask_zero=True,
weights=[embedding_matrix],
input_length=max_length,
trainable=False)
model.add(embedding)
model.add(LSTM(units=50, activation='sigmoid', recurrent_activation='hard_sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
# Compile and train the model
print('Compiling the Model...')
model.compile(loss='binary_crossentropy',
optimizer='adam',metrics=['accuracy'])
print("Train...")
skf = StratifiedKFold(y_train, n_folds=3, shuffle=True)
new_train = np.zeros((len(x_train),1))
new_test = np.zeros((len(x_test),1))
for i,(trainid,valid) in enumerate(skf):
print('fold' + str(i))
train_x = x_train[trainid]
train_y = y_train[trainid]
val_x = x_train[valid]
model.fit(train_x, train_y,
batch_size=config['batch_size'],
epochs=config['n_epoch'],verbose=1)
new_train[valid] = model.predict_proba(val_x)
new_test += model.predict_proba(x_test)
new_test /= 3
stacks = []
stacks_name = []
stack = np.vstack([new_train,new_test])
stacks.append(stack)
stacks = np.hstack(stacks)
clf_stacks = pd.DataFrame(data=stacks,columns=['lstm'])
clf_stacks.to_csv('../feature/predict/lstm_prob2.csv',index=0)
config={
'max_length':50,
'batch_size':32,
'n_epoch':10
}
if __name__=='__main__':
# data,label = load_data()
# data = preprocess_data(data)
# LogInfo('w2v starts!')
# getWord2vecFeature(data,100)
# LogInfo('w2v finishes!')
# LogInfo('d2v starts!')
# getDoc2vecFeature(data,100)
# LogInfo('d2v finishes!')
# LogInfo('lsi starts!')
# getLsiFeature(data,100)
# LogInfo('lsi finishes!')
# LogInfo('lda starts!')
# getLdaFeature(data,10)
# LogInfo('lda finishes!')
# get_allData()
# lstm_test()
lstm_predict()
# lstm_predict(string)