-
Notifications
You must be signed in to change notification settings - Fork 33
/
main_keras.py
150 lines (117 loc) · 4.63 KB
/
main_keras.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
# coding: utf-8
import os
import re
import jieba
import time
import numpy as np
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
from keras.models import Sequential, Model
from keras.layers import Dense, Flatten, Embedding, Input
from keras.layers import Conv1D, MaxPooling1D, Flatten
from load_data import labels_index, load_raw_datasets, load_pre_trained
from utils import plot_history
texts, labels = load_raw_datasets()
embeddings_index = load_pre_trained()
MAX_SEQUENCE_LEN = 1000 # sequence length
MAX_WORDS_NUM = 20000 # max words
VAL_SPLIT_RATIO = 0.2 # ratio for validation
EMBEDDING_DIM = 300 # embedding dimension
# process datasets by keras API
tokenizer = Tokenizer(num_words=MAX_WORDS_NUM)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)
word_index = tokenizer.word_index
print(len(word_index)) # all token found
dict_swaped = lambda _dict: {val:key for (key, val) in _dict.items()}
word_dict = dict_swaped(word_index) # swap key-value
data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LEN)
labels_categorical = to_categorical(np.asarray(labels))
print('Shape of data tensor:', data.shape)
print('Shape of label tensor:', labels_categorical.shape)
indices = np.arange(data.shape[0])
np.random.shuffle(indices)
data = data[indices]
labels_categorical = labels_categorical[indices]
# split data by ratio
val_samples_num = int(VAL_SPLIT_RATIO * data.shape[0])
x_train = data[:-val_samples_num]
y_train = labels_categorical[:-val_samples_num]
x_val = data[-val_samples_num:]
y_val = labels_categorical[-val_samples_num:]
# generate embedding matrix
embedding_matrix = np.zeros((MAX_WORDS_NUM+1, EMBEDDING_DIM)) # row 0 for 0
for word, i in word_index.items():
embedding_vector = embeddings_index.get(word)
if i < MAX_WORDS_NUM:
if embedding_vector is not None:
# Words not found in embedding index will be all-zeros.
embedding_matrix[i] = embedding_vector
# build models
# model 1 without pre-trained embedding
input_dim = x_train.shape[1]
model1 = Sequential()
model1.add(Embedding(input_dim=MAX_WORDS_NUM+1,
output_dim=EMBEDDING_DIM,
input_length=MAX_SEQUENCE_LEN))
model1.add(Flatten())
model1.add(Dense(64, activation='relu', input_shape=(input_dim,)))
model1.add(Dense(64, activation='relu'))
model1.add(Dense(len(labels_index), activation='softmax'))
model1.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
history1 = model1.fit(x_train,
y_train,
epochs=30,
batch_size=128,
validation_data=(x_val, y_val))
plot_history(history1)
# model 2 with pre-trained embedding
model2 = Sequential()
model2.add(Embedding(input_dim=MAX_WORDS_NUM+1,
output_dim=EMBEDDING_DIM,
weights=[embedding_matrix],
input_length=MAX_SEQUENCE_LEN,
trainable=False))
model2.add(Flatten())
model2.add(Dense(64, activation='relu', input_shape=(input_dim,)))
model2.add(Dense(64, activation='relu'))
model2.add(Dense(len(labels_index), activation='softmax'))
model2.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
history2 = model2.fit(x_train,
y_train,
epochs=10,
batch_size=128,
validation_data=(x_val, y_val))
plot_history(history2)
# model 3 with CNN
embedding_layer = Embedding(input_dim=MAX_WORDS_NUM+1,
output_dim=EMBEDDING_DIM,
weights=[embedding_matrix],
input_length=MAX_SEQUENCE_LEN,
trainable=False)
sequence_input = Input(shape=(MAX_SEQUENCE_LEN,), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)
x = Conv1D(128, 5, activation='relu')(embedded_sequences)
x = MaxPooling1D(5)(x)
x = Conv1D(128, 5, activation='relu')(x)
x = MaxPooling1D(5)(x)
x = Conv1D(128, 5, activation='relu')(x)
x = MaxPooling1D(35)(x) # global max pooling
x = Flatten()(x)
x = Dense(128, activation='relu')(x)
preds = Dense(len(labels_index), activation='softmax')(x)
model3 = Model(sequence_input, preds)
model3.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['acc'])
history3 = model3.fit(x_train,
y_train,
epochs=6,
batch_size=128,
validation_data=(x_val, y_val))
plot_history(history3)