-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlstm.py
More file actions
106 lines (86 loc) · 3.29 KB
/
lstm.py
File metadata and controls
106 lines (86 loc) · 3.29 KB
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
#coding:utf-8
import sys
import keras
import pydot
VECTOR_DIR = 'category_7_3.model'
MAX_SEQUENCE_LENGTH = 100
EMBEDDING_DIM = 250
VALIDATION_SPLIT = 0.16
TEST_SPLIT = 0.2
rootdir = 'D:/Topic/code/segment/data/lstm_train/7_2'
print ('(1) load texts...')
train_texts = open(rootdir + '/train.txt','r',encoding='utf-8').read().split('\n')
train_labels = open(rootdir + '/train_label2.txt','r',encoding='utf-8').read().split('\n')
test_texts = open(rootdir + '/test.txt','r',encoding='utf-8').read().split('\n')
test_labels = open(rootdir + '/test_label2.txt','r',encoding='utf-8').read().split('\n')
all_texts = train_texts + test_texts
all_labels = train_labels + test_labels
print ('(2) doc to var...')
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
import numpy as np
tokenizer = Tokenizer()
tokenizer.fit_on_texts(all_texts)
sequences = tokenizer.texts_to_sequences(all_texts)
word_index = tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))
data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH)
labels = to_categorical(np.asarray(all_labels))
print('Shape of data tensor:', data.shape)
print('Shape of label tensor:', labels.shape)
print ('(3) split data set...')
p1 = int(len(data)*(1-VALIDATION_SPLIT-TEST_SPLIT))
p2 = int(len(data)*(1-TEST_SPLIT))
x_train = data[:p1]
y_train = labels[:p1]
x_val = data[p1:p2]
y_val = labels[p1:p2]
x_test = data[p2:]
y_test = labels[p2:]
print ('train docs: '+str(len(x_train)))
print ('val docs: '+str(len(x_val)))
print ('test docs: '+str(len(x_test)))
print ('(4) load word2vec as embedding...')
import gensim
from keras.utils import plot_model
#w2v_model = gensim.models.KeyedVectors.load_word2vec_format(VECTOR_DIR, binary=True)
w2v_model = gensim.models.KeyedVectors.load(VECTOR_DIR)
embedding_matrix = np.zeros((len(word_index) + 1, EMBEDDING_DIM))
not_in_model = 0
in_model = 0
for word, i in word_index.items():
if str(word) in w2v_model:
in_model += 1
embedding_matrix[i] = np.asarray(w2v_model[str(word)], dtype='float32')
else:
not_in_model += 1
print (str(not_in_model)+' words not in w2v model')
from keras.layers import Embedding
embedding_layer = Embedding(len(word_index) + 1,
EMBEDDING_DIM,
weights=[embedding_matrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
print ('(5) training model...')
from keras.layers import Dense, Input, Flatten, Dropout
from keras.layers import LSTM, Embedding
from keras.models import Sequential
model = Sequential()
model.add(embedding_layer)
model.add(LSTM(200, dropout=0.3, recurrent_dropout=0.1 , return_sequences=True))
#model.add(LSTM(200, return_sequences=True))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(labels.shape[1], activation='softmax'))
model.summary()
#plot_model(model, to_file='model.png',show_shapes=True)
#exit(0)
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['acc'])
print (model.metrics_names)
model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=2, batch_size=128)
model.save('word_vector_lstm.h5')
print ('(6) testing model...')
print (model.evaluate(x_test, y_test))