forked from jcjview/cail2018_repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keras_relu_valid.py
238 lines (206 loc) · 8.36 KB
/
keras_relu_valid.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
# input_file = "../input/process_10k.csv"
from keras import Input
from keras.layers import Embedding, SpatialDropout1D, Conv1D, GlobalAveragePooling1D, LSTM, BatchNormalization, merge, \
Dense, PReLU, Dropout
input_file = "../process.csv"
# SEP = "\t"
SEP = ","
w2vpath = '../baike.128.no_truncate.glove.txt'
embedding_matrix_path = './matrix_glove.npy'
kernel_name = "bgru_cnn"
word_index_path = "worddict.pkl"
TRAIN_HDF5 = "train_hdf5.h5"
import h5py
import pandas as pd
import numpy as np
import keras
from keras.callbacks import EarlyStopping, ModelCheckpoint, Callback
from sklearn.metrics import f1_score
MAX_TEXT_LENGTH = 300
MAX_FEATURES = 100000
embedding_dims = 128
dr = 0.2
dropout_p = 0.1
fit_batch_size = 256
fit_epoch = 30
class_num = 202
class F1ScoreCallback(Callback):
def __init__(self, predict_batch_size=1024, include_on_batch=False):
super(F1ScoreCallback, self).__init__()
self.predict_batch_size = predict_batch_size
self.include_on_batch = include_on_batch
def on_batch_begin(self, batch, logs={}):
pass
def on_train_begin(self, logs={}):
if not ('avg_f1_score_val' in self.params['metrics']):
self.params['metrics'].append('avg_f1_score_val')
def on_batch_end(self, batch, logs={}):
if (self.include_on_batch):
logs['avg_f1_score_val'] = float('-inf')
def on_epoch_end(self, epoch, logs={}):
logs['avg_f1_score_val'] = float('-inf')
if (self.validation_data):
y_predict = self.model.predict(self.validation_data[0],
batch_size=self.predict_batch_size)
y_predict[y_predict >= 0.5] = 1
y_predict[y_predict < 0.5] = 0
f1 = f1_score(self.validation_data[1], y_predict, average='macro')
print("macro f1_score %.4f " % f1)
f2 = f1_score(self.validation_data[1], y_predict, average='micro')
print("micro f1_score %.4f " % f2)
avgf1=(f1 + f2) / 2
print("avg_f1_score %.4f " % (avgf1))
logs['avg_f1_score_val'] =avgf1
def get_model(embedding_matrix, nb_words):
input_tensor = Input(shape=(MAX_TEXT_LENGTH,), dtype='int32')
embedding_layer = Embedding(MAX_FEATURES,
embedding_dims,
weights=[embedding_matrix],
input_length=MAX_TEXT_LENGTH,
trainable=False)
embedded_sequences = embedding_layer(input_tensor)
x = SpatialDropout1D(0.2)(embedded_sequences)
xconv = Conv1D(filters=128,
kernel_size=3,
padding='same',
activation='relu')(x)
xconv = GlobalAveragePooling1D()(xconv)
xlstm = LSTM(350, dropout_W=0.2, dropout_U=0.2)(x)
xlstm = BatchNormalization()(xlstm)
x = merge([xconv, xlstm], mode='concat')
x = Dense(500)(x)
x = PReLU()(x)
x1 = BatchNormalization()(x)
x = Dense(500)(x1)
x = PReLU()(x)
x2 = BatchNormalization()(x)
x = merge([x1, x2], mode='sum')
x = Dense(500)(x)
x = PReLU()(x)
x3 = BatchNormalization()(x)
x = merge([x1, x2, x3], mode='sum')
x = Dense(500)(x)
x = PReLU()(x)
x4 = BatchNormalization()(x)
x = merge([x1, x2, x3, x4], mode='sum')
x = Dense(500)(x)
x = PReLU()(x)
x5 = BatchNormalization()(x)
x = merge([x1, x2, x3, x4, x5], mode='sum')
# x = concatenate([x1, x2, x3, x4], axis=1)
x = Dropout(0.2)(x)
output_layer = keras.layers.Dense(class_num, activation="softmax")(x)
model = keras.models.Model(input_tensor, output_layer)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["accuracy"])
model.summary()
return model
from tqdm import tqdm
import mmap
import os
def get_num_lines(file_path):
fp = open(file_path, "r+")
buf = mmap.mmap(fp.fileno(), 0)
lines = 0
while buf.readline():
lines += 1
return lines
def get_embedding_matrix(word_index, Emed_path, Embed_npy):
if (os.path.exists(Embed_npy)):
return np.load(Embed_npy)
print('Indexing word vectors')
embeddings_index = {}
file_line = get_num_lines(Emed_path)
print('lines ', file_line)
with open(Emed_path, encoding='utf-8') as f:
for line in tqdm(f, total=file_line):
values = line.split()
if (len(values) < embedding_dims):
print(values)
continue
word = ' '.join(values[:-embedding_dims])
coefs = np.asarray(values[-embedding_dims:], dtype='float32')
embeddings_index[word] = coefs
f.close()
print('Total %s word vectors.' % len(embeddings_index))
print('Preparing embedding matrix')
nb_words = MAX_FEATURES # min(MAX_FEATURES, len(word_index))
all_embs = np.stack(embeddings_index.values())
print(all_embs.shape)
emb_mean, emb_std = all_embs.mean(), all_embs.std()
embedding_matrix = np.random.normal(loc=emb_mean, scale=emb_std, size=(nb_words, embedding_dims))
# embedding_matrix = np.zeros((nb_words, embedding_dims))
count = 0
for word, i in tqdm(word_index.items()):
if i >= MAX_FEATURES:
continue
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
# words not found in embedding index will be all-zeros.
embedding_matrix[i] = embedding_vector
count += 1
np.save(Embed_npy, embedding_matrix)
print('Null word embeddings: %d' % (nb_words - count))
print('not Null word embeddings: %d' % count)
print('embedding_matrix shape', embedding_matrix.shape)
# print('Null word embeddings: %d' % np.sum(np.sum(embedding_matrix, axis=1) == 0))
return embedding_matrix
# df = pd.read_csv(input_file, encoding="utf-8")
# text = df['text'].values
# label = df['accu_label'].values
# from sklearn.preprocessing import LabelEncoder
# # encode class values as integers
# encoder = LabelEncoder()
# encoded_Y = encoder.fit_transform(label)
# # convert integers to dummy variables (one hot encoding)
# y = keras.utils.to_categorical(encoded_Y,num_classes=class_num)
# print('y shape',y.shape)
# from keras.preprocessing.sequence import pad_sequences
# from keras.preprocessing.text import Tokenizer
# tokenizer = Tokenizer(num_words=MAX_FEATURES)
# tokenizer.fit_on_texts(list(text))
# list_tokenized_text = tokenizer.texts_to_sequences(text)
# X_train = pad_sequences(list_tokenized_text, maxlen=MAX_TEXT_LENGTH)
# print('x shape',X_train.shape)
# nb_words = min(MAX_FEATURES, len(tokenizer.word_index))
# print("nb_words", nb_words)
# outh5file = h5py.File(TRAIN_HDF5, 'w')
# outh5file.create_dataset('train_token', data=X_train)
# outh5file.create_dataset('train_label', data=y)
outh5file = h5py.File(TRAIN_HDF5, 'r')
X_train = outh5file['train_token']
y = outh5file['train_label']
X_train=np.array(X_train,copy=True)
y=np.array(y,copy=True)
# embedding_matrix1 = get_embedding_matrix(tokenizer.word_index, w2vpath, embedding_matrix_path)
embedding_matrix1 = np.load(embedding_matrix_path)
nb_words=MAX_FEATURES
import time
timeStr = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime())
x_train = X_train[:155000]
x_val = X_train[155000:]
y_train = y[:155000]
y_val = y[155000:]
print('x_train shape', x_train.shape)
print('x_val shape', x_val.shape)
print('y_train shape', y_train.shape)
print('y_val shape', y_val.shape)
model = get_model(embedding_matrix1, nb_words)
early_stopping = EarlyStopping(monitor='val_loss', patience=12, verbose=1)
bst_model_path = kernel_name + '_weight_valid_%s.h5' % timeStr
csv_logger = keras.callbacks.CSVLogger('./log/' + bst_model_path + '_log.csv', append=True, separator=';')
model_checkpoint = ModelCheckpoint(bst_model_path, monitor='avg_f1_score_val',mode='max',
save_best_only=True, verbose=1, save_weights_only=True)
hist = model.fit(x_train, y_train,
validation_data=(x_val, y_val),
epochs=fit_epoch, batch_size=fit_batch_size, shuffle=True,
verbose=1,
callbacks=[F1ScoreCallback(),early_stopping, model_checkpoint ]
)
predict = model.predict(x_val, batch_size=1024)
predict[predict > 0.5] = 1
predict[predict < 0.5] = 0
macro_f1 = f1_score(y_val, predict, average="macro")
micro_f1 = f1_score(y_val, predict, average="micro")
print("macro_f1", macro_f1)
print("micro_f1", micro_f1)
print(macro_f1 / 2 + micro_f1 / 2)