|
1 | 1 | from tf_models.lstm import LSTMModel
|
| 2 | +from tf_models.convlstm import ConvLSTMModel |
2 | 3 | import tensorflow as tf
|
3 | 4 | import numpy as np
|
4 |
| -from tqdm import tqdm |
| 5 | +from tqdm import tqdm_notebook, tqdm |
| 6 | +import os |
5 | 7 |
|
6 | 8 | from ReadData import ReadData
|
7 | 9 |
|
|
11 | 13 | parser.add_argument('--model', '-m', help='Name of Model to use [lstm, cnn, cnnlstm]', required=True)
|
12 | 14 | parser.add_argument('--training_csv', '-csv', help='Path to Training CSV file', required=True)
|
13 | 15 | 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') |
15 | 16 | parser.add_argument('--n_classes', '-n', help='No of classes to predict | Default: 2', default=2, type=int)
|
16 | 17 | parser.add_argument('--optimizer', '-o', help='which Optimizer to use? | Default: "Adam"', default='adam')
|
17 | 18 | parser.add_argument('--batch_size', '-b', help='What should be the batch size? | Default: 32', default=32, type=int)
|
18 | 19 | parser.add_argument('--epochs', '-ep', help='How many epochs to Train? | Default: 100', default=100, type=int)
|
19 | 20 | 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)
|
20 | 21 | parser.add_argument('--no_samples', '-ns', help='How many samples to train on? | Default: 1000', default=1000, type=int)
|
21 | 22 | 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') |
22 | 24 |
|
23 | 25 | args = parser.parse_args()
|
24 | 26 |
|
|
27 | 29 | timesteps = 75
|
28 | 30 | embed_size = 101
|
29 | 31 |
|
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') |
32 | 35 |
|
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() |
34 | 41 |
|
35 |
| -reader = ReadData(args.training_csv, args.embedding, args.pos_model, |
| 42 | +reader = ReadData(args.training_csv, args.embedding, |
36 | 43 | batch_size=args.batch_size, no_samples=args.no_samples,
|
37 | 44 | train_val_split=args.train_val_split)
|
38 | 45 |
|
39 |
| -'''print('Reading Training data.') |
40 |
| -train_x, train_y = reader.read_all_train()''' |
41 | 46 | print('Reading Validation data.')
|
42 | 47 | val_x, val_y = reader.read_all_val()
|
43 | 48 |
|
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) |
47 | 51 |
|
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) |
50 | 64 |
|
51 | 65 | 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) |
52 | 73 |
|
| 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') |
53 | 82 | with tf.Session() as sess:
|
54 | 83 | sess.run(tf.global_variables_initializer())
|
55 | 84 |
|
| 85 | + train_summary_writer = tf.summary.FileWriter(train_log, graph=sess.graph) |
| 86 | + val_summary_writer = tf.summary.FileWriter(val_log) |
| 87 | + |
56 | 88 | for epoch in range(args.epochs):
|
57 | 89 | i = 0
|
58 | 90 | epoch_loss = 0
|
59 | 91 | 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 = [] |
63 | 95 | 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): |
65 | 97 | start = i
|
66 | 98 | end = i + args.batch_size
|
67 | 99 | i = end
|
68 | 100 |
|
69 | 101 | 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 | + |
73 | 106 | 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)))) |
84 | 111 | pbar.update(1)
|
85 | 112 |
|
| 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}) |
86 | 115 |
|
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) |
89 | 117 |
|
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)) |
93 | 121 | print('------------------------------------------------------------')
|
94 | 122 |
|
| 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 | + |
95 | 128 | print("Accuracy: {}".format(accuracy.eval({x: val_x, y: val_y})))
|
0 commit comments