Skip to content

Commit 5f56e5f

Browse files
committed
new architecture added and tensorboard
1 parent 244c092 commit 5f56e5f

File tree

10 files changed

+145
-100
lines changed

10 files changed

+145
-100
lines changed

Diff for: ReadData.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import numpy as np
66

77
class ReadData:
8-
def __init__(self, path_csv, embedding_model, pos_model, batch_size=32, no_samples=10000, train_val_split=0.1):
9-
self.text2vec = Text2Vector(embedding_model, pos_model, size=(75, 101))
8+
def __init__(self, path_csv, embedding_model, batch_size=32, no_samples=10000, train_val_split=0.1):
9+
self.text2vec = Text2Vector(embedding_model, size=(75, 101))
1010
self.data = pd.read_csv(path_csv, sep="|")
1111
self.data = self.data.sample(frac=1).reset_index(drop=True)
1212
self.data = self.data.sample(frac=1).reset_index(drop=True).head(no_samples)

Diff for: __pycache__/ReadData.cpython-36.pyc

-4.38 KB
Binary file not shown.

Diff for: __pycache__/text2vector.cpython-36.pyc

-2.46 KB
Binary file not shown.

Diff for: keras_models/cnnlstm.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ def CNNLSTMModel(input_shape, output_shape):
1010
inp = Input(shape=input_shape)
1111

1212
x = Conv1D(32, kernel_size=5, padding='same', activation='relu')(inp)
13-
x = Conv1D(64, kernel_size=7, padding='same', activation='relu')(x)
14-
x = Conv1D(128, kernel_size=7, padding='same', activation='relu')(x)
15-
x = MaxPooling1D(pool_size=5, strides=2)(x)
13+
#x = Conv1D(64, kernel_size=7, padding='same', activation='relu')(x)
14+
#x = Conv1D(128, kernel_size=7, padding='same', activation='relu')(x)
15+
#x = MaxPooling1D(pool_size=5, strides=2)(x)
1616

17-
x = LSTM(256, return_sequences=True)(x)
17+
#x = LSTM(256, return_sequences=True)(x)
1818

1919
x = Flatten()(x)
20-
x = Dense(512, activation='relu')(x)
20+
'''x = Dense(512, activation='relu')(x)
2121
x = Dropout(0.2)(x)
2222
x = Dense(512, activation='relu')(x)
2323
x = Dropout(0.2)(x)
24-
24+
'''
2525
out = Dense(output_shape, activation='softmax')(x)
2626

2727
model = Model(inputs=inp, outputs=out)
2828
return model
2929

3030
if __name__ == "__main__":
31-
model = CNNLSTMModel((100,1), 6)
31+
model = CNNLSTMModel((75,101), 2)
3232
model.summary()

Diff for: ner_pos.py

-44
This file was deleted.

Diff for: text2vector.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77
import pandas as pd
88

99
class Text2Vector:
10-
def __init__(self, embed_path, pos_model, size):
10+
def __init__(self, embed_path, size):
1111
print('Loading embedding model {}'.format(embed_path))
1212
self.embed_model = fasttext.load_model(embed_path)
13-
14-
#print('Loading POS model {}'.format(pos_model))
15-
#self.pos_model = fasttext.load_model(pos_model)
16-
13+
1714
self.sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')
1815
self.size = size
1916

@@ -54,8 +51,6 @@ def convert(self, text):
5451
for i, word in enumerate(df['word']):
5552
try:
5653
embed_vector = np.array(self.embed_model[word])
57-
#pos_vector = np.array(self.pos_model[pos_tag([word])[0][1]])
58-
#vectors.append(list(embed_vector) + [df['TF_IDF'][i]] + list(pos_vector))
5954
vectors.append(list(embed_vector) + [df['TF_IDF'][i]])
6055
except Exception as e:
6156
print('In text2vector.py: {}'.format(e))

Diff for: tf_models/__pycache__/convlstm.cpython-36.pyc

2.21 KB
Binary file not shown.

Diff for: tf_models/convlstm.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import tensorflow as tf
2+
3+
class ConvLSTMModel:
4+
def __init__(self):
5+
pass
6+
7+
def conv2d(self, x, filter, strides, padding='SAME'):
8+
return tf.nn.conv2d(x, filter=filter, strides=strides, padding=padding)
9+
10+
def max_pool2d(self, x, ksize, strides, padding='SAME'):
11+
return tf.nn.max_pool(x, ksize=ksize, strides=strides, padding=padding)
12+
13+
def dropout(self, x, keep_rate):
14+
return tf.nn.dropout(x, keep_rate)
15+
16+
def relu(self, x):
17+
return tf.nn.relu(x)
18+
19+
def batch_normalization(self, x):
20+
return tf.nn.batch_normalization(x)
21+
22+
def model(self, x):
23+
weight1 = tf.Variable(tf.random_normal([3, 3, 1, 64]))
24+
bias1 = tf.Variable(tf.random_normal([64]))
25+
conv1 = self.conv2d(x, filter=weight1, strides=2)
26+
relu1 = self.relu(tf.add(conv1, bias1))
27+
max_pool1 = self.max_pool2d(relu1, ksize=2, strides=2)
28+
#max_pool1 = self.batch_normalization(max_pool1)
29+
30+
weight2 = tf.Variable(tf.random_normal([3, 3, 64, 128]))
31+
bias2 = tf.Variable(tf.random_normal([128]))
32+
conv2 = self.conv2d(max_pool1, filter=weight2, strides=2)
33+
relu2 = self.relu(tf.add(conv2, bias2))
34+
max_pool2 = self.max_pool2d(relu2, ksize=2, strides=2)
35+
#max_pool2 = self.batch_normalization(max_pool2)
36+
37+
weight3 = tf.Variable(tf.random_normal([3, 3, 128, 256]))
38+
bias3 = tf.Variable(tf.random_normal([256]))
39+
conv3 = self.conv2d(max_pool2, filter=weight3, strides=2)
40+
relu3 = self.relu(tf.add(conv3, bias3))
41+
max_pool3 = self.max_pool2d(relu3, ksize=2, strides=2)
42+
#max_pool3 = self.batch_normalization(max_pool3)
43+
44+
flatten = tf.reshape(max_pool3, [-1, 2*2*256])
45+
46+
weight4 = tf.Variable(tf.random_normal([2*2*256, 1024]))
47+
bias4 = tf.Variable(tf.random_normal([1024]))
48+
dense1 = tf.add(tf.matmul(flatten, weight4), bias4)
49+
50+
dropout1 = self.dropout(dense1, 0.8)
51+
52+
weight5 = tf.Variable(tf.random_normal([1024, 2]))
53+
bias5 = tf.Variable(tf.random_normal([2]))
54+
dense2 = tf.add(tf.matmul(dropout1, weight5), bias5)
55+
56+
return dense2

Diff for: tf_models/lstm.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@ def model(self, x):
1111
x = tf.unstack(x, self.timesteps, 1)
1212

1313
lstmcells = []
14-
for _ in range(3):
14+
for _ in range(2):
1515
lstmcells.append(rnn.BasicLSTMCell(self.hidden_states))
1616

1717
multilstm= rnn.MultiRNNCell(lstmcells)
1818
rnn_output, states = tf.nn.static_rnn(multilstm, x, dtype=tf.float32)
1919

20-
weights = tf.Variable(tf.random_normal([self.hidden_states, self.no_classes]))
21-
biases = tf.Variable(tf.random_normal([self.no_classes]))
20+
weights1 = tf.Variable(tf.random_normal([self.hidden_states, 1024]))
21+
biases1 = tf.Variable(tf.random_normal([1024]))
22+
output1 = tf.add(tf.matmul(rnn_output[-1], weights1), biases1)
2223

23-
output = tf.add(tf.matmul(rnn_output[-1], weights), biases)
24+
output1 = tf.nn.relu(output1, 0.75)
2425

25-
return output
26+
weights2 = tf.Variable(tf.random_normal([1024, self.no_classes]))
27+
biases2 = tf.Variable(tf.random_normal([self.no_classes]))
28+
output2 = tf.add(tf.matmul(output1, weights2), biases2)
29+
30+
return output2
2631

2732
if __name__ == "__main__":
2833
hidden_states = 512

Diff for: tf_train.py

+68-35
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from tf_models.lstm import LSTMModel
2+
from tf_models.convlstm import ConvLSTMModel
23
import tensorflow as tf
34
import numpy as np
4-
from tqdm import tqdm
5+
from tqdm import tqdm_notebook, tqdm
6+
import os
57

68
from ReadData import ReadData
79

@@ -11,14 +13,14 @@
1113
parser.add_argument('--model', '-m', help='Name of Model to use [lstm, cnn, cnnlstm]', required=True)
1214
parser.add_argument('--training_csv', '-csv', help='Path to Training CSV file', required=True)
1315
parser.add_argument('--embedding', '-e', help='Path to word embedding model | Default: "embeddings/skipgram-100/skipgram.bin"', default='embeddings/skipgram-100/skipgram.bin')
14-
parser.add_argument('--pos_model', '-pos', help='Path to POS embedding model | Default: "embeddings/skipgram-pos-100/skipgram_pos.bin"', default='embeddings/skipgram-pos-100/skipgram_pos.bin')
1516
parser.add_argument('--n_classes', '-n', help='No of classes to predict | Default: 2', default=2, type=int)
1617
parser.add_argument('--optimizer', '-o', help='which Optimizer to use? | Default: "Adam"', default='adam')
1718
parser.add_argument('--batch_size', '-b', help='What should be the batch size? | Default: 32', default=32, type=int)
1819
parser.add_argument('--epochs', '-ep', help='How many epochs to Train? | Default: 100', default=100, type=int)
1920
parser.add_argument('--train_val_split', '-s', help='What should be the train vs val split fraction? | Default: 0.1', default=0.1, type=float)
2021
parser.add_argument('--no_samples', '-ns', help='How many samples to train on? | Default: 1000', default=1000, type=int)
2122
parser.add_argument('--learning_rate', '-lr', help='What should be the learning rate? | Default: 0.001', default=0.001, type=float)
23+
parser.add_argument('--logs', '-l', help="Where should the trained model be saved? | Default: logs", default='logs')
2224

2325
args = parser.parse_args()
2426

@@ -27,69 +29,100 @@
2729
timesteps = 75
2830
embed_size = 101
2931

30-
x = tf.placeholder("float", [None, timesteps, embed_size])
31-
y = tf.placeholder("float", [None, classes])
32+
if args.model == 'lstm':
33+
x = tf.placeholder("float", [None, timesteps, embed_size], name='InputData')
34+
y = tf.placeholder("float", [None, classes], name='Label')
3235

33-
model = LSTMModel(hidden_states=hidden_states, no_classes=classes, timesteps=timesteps)
36+
model = LSTMModel(hidden_states=hidden_states, no_classes=classes, timesteps=timesteps)
37+
elif args.model.startswith('cnn'):
38+
x = tf.placeholder("float", [None, timesteps, embed_size, 1], name='InputData')
39+
y = tf.placeholder("float", [None, classes], name='Label')
40+
model = ConvLSTMModel()
3441

35-
reader = ReadData(args.training_csv, args.embedding, args.pos_model,
42+
reader = ReadData(args.training_csv, args.embedding,
3643
batch_size=args.batch_size, no_samples=args.no_samples,
3744
train_val_split=args.train_val_split)
3845

39-
'''print('Reading Training data.')
40-
train_x, train_y = reader.read_all_train()'''
4146
print('Reading Validation data.')
4247
val_x, val_y = reader.read_all_val()
4348

44-
prediction = model.model(x)
45-
cost_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y))
46-
optimizer = tf.train.AdamOptimizer(learning_rate=args.learning_rate).minimize(cost_func)
49+
with tf.name_scope('Model'):
50+
prediction = model.model(x)
4751

48-
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
49-
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
52+
with tf.name_scope('Loss'):
53+
cost_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y))
54+
55+
with tf.name_scope('Optimizer'):
56+
optimizer = tf.train.AdamOptimizer(learning_rate=args.learning_rate).minimize(cost_func)
57+
58+
with tf.name_scope('Accuracy'):
59+
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
60+
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
61+
62+
if not os.path.exists(args.logs):
63+
os.mkdir(args.logs)
5064

5165
saver = tf.train.Saver()
66+
weights_path = os.path.join(args.logs, 'weights')
67+
if not os.path.exists(weights_path):
68+
os.mkdir(weights_path)
69+
70+
tensorboard_path = os.path.join(args.logs, 'tensorboard')
71+
if not os.path.exists(tensorboard_path):
72+
os.mkdir(tensorboard_path)
5273

74+
train_log = os.path.join(tensorboard_path, 'training')
75+
val_log = os.path.join(tensorboard_path, 'validation')
76+
77+
tf.summary.scalar('loss', cost_func)
78+
tf.summary.scalar('accuracy', accuracy)
79+
merged_summary_op = tf.summary.merge_all()
80+
81+
prev_val_loss = float('inf')
5382
with tf.Session() as sess:
5483
sess.run(tf.global_variables_initializer())
5584

85+
train_summary_writer = tf.summary.FileWriter(train_log, graph=sess.graph)
86+
val_summary_writer = tf.summary.FileWriter(val_log)
87+
5688
for epoch in range(args.epochs):
5789
i = 0
5890
epoch_loss = 0
5991
no_batches = int(reader.train_size/args.batch_size)
60-
#while i < reader.train_size:
61-
loss = 0
62-
acc = 0
92+
93+
loss = []
94+
acc = []
6395
with tqdm(total=no_batches, desc="Epoch {}/{}: loss: {} acc: {}".format(epoch + 1, args.epochs, loss, acc)) as pbar:
64-
for _ in range(no_batches):
96+
for batch_num in range(no_batches):
6597
start = i
6698
end = i + args.batch_size
6799
i = end
68100

69101
epoch_x, epoch_y = reader.get_next_batch(start, end)
70-
#epoch_x, epoch_y = train_x[start:end], train_y[start:end]
71-
#epoch_x = np.reshape(epoch_x, [args.batch_size, len(epoch_x[0]), len(epoch_x[0][0])])
72-
_, c = sess.run([optimizer, cost_func], feed_dict={x: epoch_x, y:epoch_y})
102+
epoch_x = np.reshape(epoch_x, (epoch_x.shape[0], timesteps, embed_size, 1))
103+
_, c, summary = sess.run([optimizer, cost_func, merged_summary_op], feed_dict={x: epoch_x, y:epoch_y})
104+
train_summary_writer.add_summary(summary, epoch*no_batches+batch_num)
105+
73106
a = accuracy.eval({x: epoch_x, y: epoch_y})
74-
if loss == 0 and acc == 0:
75-
loss = c
76-
acc = a
77-
else:
78-
loss += c
79-
loss /= 2
80-
acc += a
81-
acc /= 2
82-
83-
pbar.set_description(desc=("Epoch {}/{}: loss: {:03f}".format(epoch + 1, args.epochs, loss) + " acc: {:03f}".format(acc)))
107+
loss.append(c)
108+
acc.append(a)
109+
110+
pbar.set_description(desc=("Epoch {}/{}: loss: {:.03f}".format(epoch + 1, args.epochs, np.average(loss)) + " acc: {:.03f}".format(np.average(acc))))
84111
pbar.update(1)
85112

113+
print('------------------------------------------------------------')
114+
val_loss, val_acc, val_summary = sess.run([cost_func, accuracy, merged_summary_op], feed_dict={x: epoch_x, y:epoch_y})
86115

87-
#print("Loss: {}. Accuracy: {}".format(c, accuracy.eval({x: epoch_x, y: epoch_y})))
88-
#epoch_loss += c
116+
val_summary_writer.add_summary(val_summary, epoch)
89117

90-
#print("Epoch {} of {}. Loss: {}. Accuracy: {}".format(epoch + 1, args.epochs, epoch_loss, accuracy.eval({x: train_x, y: train_y})))
91-
print('------------------------------------------------------------')
92-
print("Val Loss: {} Val Accuracy: {}".format(cost_func.eval({x: val_x, y: val_y}), accuracy.eval({x: val_x, y: val_y})))
118+
val_loss = cost_func.eval({x: np.reshape(val_x, (val_x.shape[0], timesteps, embed_size, 1)), y: val_y})
119+
val_acc = accuracy.eval({x: np.reshape(val_x, (val_x.shape[0], timesteps, embed_size, 1)), y: val_y})
120+
print("Val Loss: {} Val Accuracy: {}".format(val_loss, val_acc))
93121
print('------------------------------------------------------------')
94122

123+
if val_loss < prev_val_loss:
124+
prev_val_loss = val_loss
125+
model_name = 'ep{:03d}'.format(epoch+1) + '-loss{:.03f}'.format( np.average(loss)) + '-val_loss{:.03f}.ckpt'.format(val_loss)
126+
saver.save(sess, os.path.join(weights_path, model_name))
127+
95128
print("Accuracy: {}".format(accuracy.eval({x: val_x, y: val_y})))

0 commit comments

Comments
 (0)