/
training.py
144 lines (124 loc) · 4.28 KB
/
training.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
import os
import keras
import io
import csv
import model
import dataset
import datetime
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
import numpy as np
from keras.utils import np_utils
from keras.utils import to_categorical
from keras.layers import Dropout, SimpleRNN, LSTM, Bidirectional
from keras.layers import BatchNormalization
from keras.optimizers import Adam
import keras.backend as K
from keras.callbacks import TensorBoard
import keras_metrics
from keras.utils.vis_utils import plot_model
from keras.models import load_model
from keras.callbacks import TensorBoard, ModelCheckpoint, EarlyStopping
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import Pipeline
from keras.models import Sequential
from keras.layers import Embedding, Flatten, Dense, Conv1D, MaxPooling1D, GlobalMaxPooling1D
import plotly.offline as py
import plotly.graph_objs as go
py.init_notebook_mode(connected=True)
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
import time
'''
Model Parameters and script settings:
'''
# Set params for the model
tensorBoard_logs_dir = '.\\logs\\'
model_dir = '.\\models\\'
glove_dir = '.\\models\\glove'
train_dir = '.\\data\\train'
train_data = 'messages_2.csv'
label_data = 'labels_2.csv'
'''
Hyper parameters for the model, training
'''
maxlen = 200 # Maximum length of the message/ tweet to be considered
training_samples = 2500
testing_samples = 3189 - training_samples
max_words = 10000 # vocab length
num_filters = 64
embedding_dim = 100
num_classes = 3
num_hidden_lstm = 64
num_hidden_rnn_final = 32
num_dense_fc = 64
recurrent_dropout = 0.2
fc_dropout = 0.5
cnn_dropout = 0.5
learning_rate = 0.01
lr_decay = 1e-6
patience = 100
lr_plateau_factor = 0.1
epochs = 500
batch_size = 1024
validation_split = 0.2
'''
model storage and name
'''
model_name = "CodeMixed-Emb-BiLSTM_32x2_64x1_DO-Dense_64x2-{}".format(int(time.time()))
def save_model(model_qualifier, model, location):
name = model_qualifier + "-{}".format(datetime.date.today()) + "-{}".format(time.time())
try:
model.save(os.path.join(location, name))
except IOError:
print("Exception occured while saving the model to disc.")
def set_keras_callbacks():
es = EarlyStopping(monitor='val_acc', mode='min', verbose=1, patience=patience, restore_best_weights=True)
mc = ModelCheckpoint(model_dir + 'Best' + model_name + '.h5', monitor='val_acc', mode='min', verbose=1)
lrp = keras.callbacks.callbacks.ReduceLROnPlateau(monitor='val_acc', factor=lr_plateau_factor, patience=patience,
verbose=0, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0)
return es, mc, lrp
x_train, y_train, x_test, y_test, embedding_matrix = dataset.dataset()
K.clear_session()
model = model.model(max_words, embedding_dim, maxlen)
model.layers[0].set_weights([embedding_matrix])
model.layers[0].trainable = True
model.summary()
optimizer = Adam(lr=learning_rate, decay=lr_decay)
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['acc'])
'''
Callbacks from Keras
'''
early_stopping, reduce_lr_plateau, model_checkpoint = set_keras_callbacks()
'''
Tensor board setup
'''
tensor_board = TensorBoard(log_dir=tensorBoard_logs_dir + '{}'.format(model_name), histogram_freq=1)
'''
Model training
'''
history = model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_split= validation_split,
callbacks=[tensor_board, early_stopping, reduce_lr_plateau, model_checkpoint])
save_model(model_name, model, ".\\models")
'''
Visualize the performance of the model
'''
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
run_epochs = range(1, len(acc)+1)
plt.plot(run_epochs, acc, 'bo', label='Training acc')
plt.plot(run_epochs, val_acc, 'b', label='Validation acc')
plt.title("Training and validation accuracy")
plt.legend()
plt.figure()
plt.plot(run_epochs, loss, 'bo', label='Training Loss')
plt.plot(run_epochs, val_loss, 'b', label='Validation Loss')
plt.title("Training and validation Losses")
plt.legend()
plt.figure()
plt.show()