-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathsentence_cnn_save.py
170 lines (122 loc) · 4.62 KB
/
sentence_cnn_save.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
'''
Written by Austin Walters
Last Edit: January 2, 2019
For use on austingwalters.com
A CNN to classify a sentence as one
of the common sentance types:
Question, Statement, Command, Exclamation
Heavily Inspired by Keras Examples:
https://github.com/keras-team/keras
'''
from __future__ import print_function
import os
import sys
import numpy as np
import keras
from sentence_types import load_encoded_data
from sentence_types import encode_data, import_embedding
from sentence_types import get_custom_test_comments
from keras.preprocessing import sequence
from keras.models import Sequential, model_from_json
from keras.layers import Dense, Dropout, Activation, Embedding
from keras.layers import Conv1D, GlobalMaxPooling1D
from keras.preprocessing.text import Tokenizer
# Use can load a different model if desired
model_name = "models/cnn"
embedding_name = "data/default"
load_model_flag = False
arguments = sys.argv[1:len(sys.argv)]
if len(arguments) == 1:
model_name = arguments[0]
load_model_flag = os.path.isfile(model_name+".json")
print(model_name)
print("Load Model?", (load_model_flag))
# Model configuration
maxlen = 500
batch_size = 64
embedding_dims = 75
filters = 100
kernel_size = 5
hidden_dims = 350
epochs = 2
# Add parts-of-speech to data
pos_tags_flag = True
# Export & load embeddings
x_train, x_test, y_train, y_test = load_encoded_data(data_split=0.8,
embedding_name=embedding_name,
pos_tags=pos_tags_flag)
word_encoding, category_encoding = import_embedding(embedding_name)
max_words = len(word_encoding) + 1
num_classes = np.max(y_train) + 1
print(max_words, 'words')
print(num_classes, 'classes')
print('Pad sequences (samples x time)')
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print('Convert class vector to binary class matrix '
'(for use with categorical_crossentropy)')
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
if not load_model_flag:
print('Constructing model!')
model = Sequential()
model.add(Embedding(max_words, embedding_dims,
input_length=maxlen))
model.add(Dropout(0.2))
model.add(Conv1D(filters, kernel_size, padding='valid',
activation='relu', strides=1))
model.add(GlobalMaxPooling1D())
model.add(Dense(hidden_dims))
model.add(Dropout(0.2))
model.add(Activation('relu'))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=batch_size,
epochs=epochs, validation_data=(x_test, y_test))
model_json = model.to_json()
with open(model_name + ".json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights(model_name + ".h5")
print("Saved model to disk")
else:
print('Loading model!')
# load json and create model
json_file = open(model_name + '.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
# load weights into new model
model.load_weights(model_name + ".h5")
print("Loaded model from disk")
# evaluate loaded model on test data
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
score = model.evaluate(x_test, y_test, batch_size=batch_size, verbose=1)
print('Test accuracy:', score[1])
test_comments, test_comments_category = get_custom_test_comments()
x_test, _, y_test, _ = encode_data(test_comments, test_comments_category,
data_split=1.0,
embedding_name=embedding_name,
add_pos_tags_flag=pos_tags_flag)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
y_test = keras.utils.to_categorical(y_test, num_classes)
score = model.evaluate(x_test, y_test,
batch_size=batch_size, verbose=1)
print('Manual test')
print('Test accuracy:', score[1])
# Show predictions
print(len(x_test))
predictions = model.predict(x_test, batch_size=batch_size, verbose=1)
real = []
test = []
for i in range(0, len(predictions)):
real.append(y_test[i].argmax(axis=0))
test.append(predictions[i].argmax(axis=0))
print("Predictions")
print("Real", real)
print("Test", test)