-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural-net-keras.py
76 lines (60 loc) · 2.48 KB
/
neural-net-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
from getEmbeddings import getEmbeddings
import matplotlib.pyplot as plt
import numpy as np
import keras, pickle
from keras import backend as K
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM, Embedding, Input, RepeatVector
from keras.optimizers import SGD
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
import scikitplot.plotters as skplt
def save_classifier(classifier, classifier_fname):
classifier_file = open(classifier_fname, 'wb')
pickle.dump(classifier, classifier_file)
classifier_file.close()
def plot_cmat(yte, ypred):
'''Plotting confusion matrix'''
skplt.plot_confusion_matrix(yte, ypred)
plt.show()
xtr,xte,ytr,yte = getEmbeddings("datasets/train.csv")
np.save('./xtr', xtr)
np.save('./xte', xte)
np.save('./ytr', ytr)
np.save('./yte', yte)
xtr = np.load('./xtr.npy')
xte = np.load('./xte.npy')
ytr = np.load('./ytr.npy')
yte = np.load('./yte.npy')
def baseline_model():
'''Neural network with 3 hidden layers'''
model = Sequential()
model.add(Dense(256, input_dim=300, activation='relu', kernel_initializer='normal'))
model.add(Dropout(0.3))
model.add(Dense(256, activation='relu', kernel_initializer='normal'))
model.add(Dropout(0.5))
model.add(Dense(80, activation='relu', kernel_initializer='normal'))
model.add(Dense(2, activation="softmax", kernel_initializer='normal'))
# gradient descent
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
# configure the learning process of the model
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
return model
model = baseline_model()
model.summary()
x_train, x_test, y_train, y_test = train_test_split(xtr, ytr, test_size=0.2, random_state=42)
label_encoder = LabelEncoder()
label_encoder.fit(y_train)
encoded_y = np_utils.to_categorical((label_encoder.transform(y_train)))
label_encoder.fit(y_test)
encoded_y_test = np_utils.to_categorical((label_encoder.transform(y_test)))
estimator = model.fit(x_train, encoded_y, epochs=20, batch_size=64)
print("Model Trained!")
score = model.evaluate(x_test, encoded_y_test)
print("")
print("Accuracy = " + format(score[1]*100, '.2f') + "%") # 92.69%
probabs = model.predict_proba(x_test)
y_pred = np.argmax(probabs, axis=1)
plot_cmat(y_test, y_pred)
save_classifier(model, "neural-net-keras_model.pickle")