|
| 1 | + |
| 2 | +import pandas as pd |
| 3 | +import tensorflow as tf |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from tensorflow.contrib import rnn |
| 7 | + |
| 8 | + |
| 9 | +class LSTMNet: |
| 10 | + start = 0 |
| 11 | + model_file = "./author_lstm.model" |
| 12 | + |
| 13 | + def __init__(self, nfeatures, max_length, vocab_size, embedding_matrix): |
| 14 | + self.nfeatures = nfeatures |
| 15 | + self.n_hidden = nfeatures / 2 |
| 16 | + self.n_steps = max_length |
| 17 | + self.n_layers = 1 |
| 18 | + self.batch_size = 200 |
| 19 | + self.dropout = 0.7 |
| 20 | + self.max_length = max_length |
| 21 | + self.embedding_matrix = embedding_matrix |
| 22 | + self.vocab_size = vocab_size |
| 23 | + self.threshold = 0.7 |
| 24 | + self.learning_rate = 0.08 |
| 25 | + self.epsilon = 1e-3 |
| 26 | + self.istraining = True |
| 27 | + |
| 28 | + def __createBatch(self, input1=None, labels=None, batch_size=None): |
| 29 | + |
| 30 | + self.end = self.start + batch_size |
| 31 | + |
| 32 | + batch_x1 = input1[self.start:self.end] |
| 33 | + print(len(batch_x1)) |
| 34 | + |
| 35 | + batch_y = labels[self.start:self.end] |
| 36 | + |
| 37 | + self.start = self.end |
| 38 | + |
| 39 | + if (self.end >= len(input1)): |
| 40 | + self.start = 0 |
| 41 | + |
| 42 | + return batch_x1, batch_y |
| 43 | + |
| 44 | + def __createTestBatch(self, input1=None, batch_size=None): |
| 45 | + |
| 46 | + self.end = self.start + batch_size |
| 47 | + |
| 48 | + batch_x1 = input1[self.start:self.end] |
| 49 | + print(len(batch_x1)) |
| 50 | + |
| 51 | + self.start = self.end |
| 52 | + |
| 53 | + if (self.end >= len(input1)): |
| 54 | + self.start = 0 |
| 55 | + |
| 56 | + return batch_x1 |
| 57 | + |
| 58 | + def convertLabelsToOneHotVectors(self, labels): |
| 59 | + |
| 60 | + one_hot_label = [] |
| 61 | + |
| 62 | + for label in labels: |
| 63 | + if label == 0: |
| 64 | + one_hot_label.append([1, 0, 0]) |
| 65 | + elif label == 1: |
| 66 | + one_hot_label.append([0, 1, 0]) |
| 67 | + else: |
| 68 | + one_hot_label.append([0, 0, 1]) |
| 69 | + |
| 70 | + return one_hot_label |
| 71 | + |
| 72 | + def reshape(self, input1, labels=None): |
| 73 | + input1 = np.reshape(input1, (-1, self.max_length)) |
| 74 | + labels = np.reshape(labels, (-1, 1)) |
| 75 | + |
| 76 | + return input1, labels |
| 77 | + |
| 78 | + def insertBatchNNLayer(self, mat_rel, axes, dimension_size): |
| 79 | + mean = None |
| 80 | + var = None |
| 81 | + batch_mean, batch_var = tf.nn.moments(mat_rel, axes) |
| 82 | + ema = tf.train.ExponentialMovingAverage(decay=0.5) |
| 83 | + |
| 84 | + if self.istraining: |
| 85 | + print("is training in BN") |
| 86 | + mean = batch_mean |
| 87 | + var = batch_var |
| 88 | + ema_apply_op = ema.apply([batch_mean, batch_var]) |
| 89 | + else: |
| 90 | + print("is testing in BN") |
| 91 | + mean = ema.average(batch_mean) |
| 92 | + var = ema.average(batch_var) |
| 93 | + |
| 94 | + scale2 = tf.Variable(tf.ones(dimension_size, dtype=tf.float64), dtype=tf.float64) |
| 95 | + beta2 = tf.Variable(tf.zeros(dimension_size, dtype=tf.float64), dtype=tf.float64) |
| 96 | + bn_layer = tf.nn.batch_normalization(mat_rel, mean, var, beta2, scale2, self.epsilon) |
| 97 | + |
| 98 | + return bn_layer |
| 99 | + |
| 100 | + #Build 6 softmax layers to predict the sentiments |
| 101 | + def buildRNN(self, x, scope): |
| 102 | + print(x) |
| 103 | + x = tf.transpose(x, [1, 0, 2]) |
| 104 | + |
| 105 | + |
| 106 | + with tf.name_scope("fw" + scope), tf.variable_scope("fw" + scope): |
| 107 | + fw_cell_array = [] |
| 108 | + print(tf.get_variable_scope().name) |
| 109 | + for _ in range(self.n_layers): |
| 110 | + fw_cell = rnn.GRUCell(self.n_hidden, activation=tf.nn.relu) |
| 111 | + fw_cell = rnn.DropoutWrapper(fw_cell,input_keep_prob=self.dropout,output_keep_prob=self.dropout) |
| 112 | + fw_cell_array.append(fw_cell) |
| 113 | + fw_cell = rnn.MultiRNNCell(fw_cell_array, state_is_tuple=True) |
| 114 | + with tf.name_scope("bw" + scope), tf.variable_scope("bw" + scope): |
| 115 | + bw_cell_array = [] |
| 116 | + print(tf.get_variable_scope().name) |
| 117 | + for _ in range(self.n_layers): |
| 118 | + bw_cell = rnn.GRUCell(self.n_hidden, activation=tf.nn.relu) |
| 119 | + bw_cell = rnn.DropoutWrapper(bw_cell,input_keep_prob=self.dropout,output_keep_prob=self.dropout) |
| 120 | + bw_cell_array.append(bw_cell) |
| 121 | + bw_cell = rnn.MultiRNNCell(bw_cell_array, state_is_tuple=True) |
| 122 | + |
| 123 | + |
| 124 | + outputs = tf.nn.bidirectional_dynamic_rnn(fw_cell, bw_cell, x, dtype=tf.float64, time_major=True) |
| 125 | + print("output-->" + str(outputs)) |
| 126 | + outputs = tf.concat(outputs[0], 2) |
| 127 | + outputs = tf.reshape(outputs, [-1, self.nfeatures]) |
| 128 | + outputs = tf.split(outputs, self.n_steps, 0) |
| 129 | + print("output-->"+str(outputs)) |
| 130 | + outputs = outputs[-1] |
| 131 | + print("output-->" + str(outputs)) |
| 132 | + |
| 133 | + nn_layer1 = tf.layers.dense(outputs,1024,activation=tf.nn.relu) |
| 134 | + nn_layer1 = tf.layers.dropout(nn_layer1,rate=0.8) |
| 135 | + nn_layer2 = tf.layers.dense(nn_layer1, 1024, activation=tf.nn.relu) |
| 136 | + nn_layer2 = tf.layers.dropout(nn_layer2, rate=0.8) |
| 137 | + result = tf.layers.dense(nn_layer2, 3, activation=tf.nn.softmax) |
| 138 | + |
| 139 | + print("final result11-->"+str(result)) |
| 140 | + |
| 141 | + return result |
| 142 | + |
| 143 | + def optimizeWeights(self, pred): |
| 144 | + #cost = tf.reduce_mean(-tf.reduce_sum(self.y * tf.log(pred), reduction_indices=1)) |
| 145 | + print("predicted-->"+str(pred)) |
| 146 | + cost = tf.losses.log_loss(self.y, pred) |
| 147 | + #global_step = tf.Variable(0, trainable=False) |
| 148 | + #learning_rate = tf.train.exponential_decay(self.learning_rate, global_step, |
| 149 | + # 1000, 0.5, staircase=False) |
| 150 | + optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(cost) |
| 151 | + |
| 152 | + return optimizer, cost |
| 153 | + |
| 154 | + |
| 155 | + def prepareFeatures(self): |
| 156 | + x1 = tf.placeholder(tf.int32, [None, self.max_length]) # batch_size x sentence_length |
| 157 | + y = tf.placeholder(tf.float64, [None, 3], "labels") |
| 158 | + |
| 159 | + return x1, y |
| 160 | + |
| 161 | + |
| 162 | + #connect to fully connected layer of 3 target classes |
| 163 | + def trainModel(self, input1, labels, one_hot_encoding=False): |
| 164 | + # Parameters |
| 165 | + |
| 166 | + training_epochs = 10 |
| 167 | + display_step = 1 |
| 168 | + record_size = len(input1) |
| 169 | + labels = self.convertLabelsToOneHotVectors(labels) |
| 170 | + |
| 171 | + |
| 172 | + self.x1, self.y = self.prepareFeatures() |
| 173 | + self.embedded_chars1 = tf.nn.embedding_lookup(self.embedding_matrix, self.x1, |
| 174 | + name="lookup1") # batch_size x sent_length x embedding_size |
| 175 | + |
| 176 | + print("Embedding-->" + str(self.embedded_chars1)) |
| 177 | + print("Embedding-->" + str(self.x1)) |
| 178 | + |
| 179 | + |
| 180 | + self.pred = self.buildRNN(self.embedded_chars1, "nn1_side") |
| 181 | + |
| 182 | + |
| 183 | + # Initializing the variables |
| 184 | + optimizer, cost = self.optimizeWeights(self.pred) |
| 185 | + init = tf.global_variables_initializer() |
| 186 | + |
| 187 | + with tf.Session() as sess: |
| 188 | + sess.run(init) |
| 189 | + count = 0 |
| 190 | + labels = np.reshape(labels, (-1, 3)) |
| 191 | + |
| 192 | + # Training cycle |
| 193 | + # Change code accordingly |
| 194 | + for epoch in range(training_epochs): |
| 195 | + print("Epoch--->" + str(epoch)) |
| 196 | + avg_cost = 0. |
| 197 | + total_batch = int(record_size / self.batch_size) |
| 198 | + # Loop over all batches |
| 199 | + for i in range(total_batch): |
| 200 | + print("batch--->" + str(i)) |
| 201 | + batch_x1, batch_ys = self.__createBatch(input1, labels, self.batch_size) |
| 202 | + |
| 203 | + |
| 204 | + # Run optimization op (backprop) and cost op (to get loss value) |
| 205 | + _, c = sess.run([optimizer, cost], feed_dict={self.x1: batch_x1, self.y: batch_ys}) |
| 206 | + print("cost per step-->"+str(c)) |
| 207 | + # Compute average loss |
| 208 | + avg_cost += c / total_batch |
| 209 | + count = count + self.batch_size |
| 210 | + # Display logs per epoch step |
| 211 | + if (epoch + 1) % display_step == 0: |
| 212 | + # -1304 cost :0 |
| 213 | + print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost)) |
| 214 | + |
| 215 | + #saver.save(sess, self.model_file) |
| 216 | + |
| 217 | + print("Optimization Finished!") |
| 218 | + |
| 219 | + def validateModel(self, test_input1, test_labels, one_hot_encoding=False): |
| 220 | + |
| 221 | + self.istraining = False |
| 222 | + test_labels = self.convertLabelsToOneHotVectors(test_labels) |
| 223 | + |
| 224 | + test_input1 = np.asarray(test_input1) |
| 225 | + test_labels = np.asarray(test_labels) |
| 226 | + |
| 227 | + print("Test1--->" + str(len(test_input1))) |
| 228 | + |
| 229 | + #test_input1, test_labels = self.reshape(test_input1, test_labels) |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | + print(len(test_input1)) |
| 234 | + print(len(test_labels)) |
| 235 | + record_size = len(test_input1) |
| 236 | + |
| 237 | + init_op = tf.global_variables_initializer() |
| 238 | + #saver = tf.train.Saver() |
| 239 | + |
| 240 | + with tf.Session() as sess: |
| 241 | + sess.run(init_op) |
| 242 | + #saver.restore(sess, self.model_file) |
| 243 | + overall_accuracy = 0 |
| 244 | + |
| 245 | + total_batch = int(record_size / self.batch_size) |
| 246 | + for i in range(total_batch): |
| 247 | + batch_x1, batch_ys = self.__createBatch(test_input1, test_labels, |
| 248 | + self.batch_size) |
| 249 | + print(len(batch_x1)) |
| 250 | + predictions = sess.run([self.pred], feed_dict={self.x1: batch_x1}) |
| 251 | + # Compute Accuracy |
| 252 | + batch_log_loss = tf.losses.log_loss(predictions[0], batch_ys) |
| 253 | + print("Log Loss:", batch_log_loss.eval()) |
| 254 | + |
| 255 | + |
| 256 | + def evaluateResults(self, predictions, actual): |
| 257 | + print(predictions) |
| 258 | + predictions = predictions[0] |
| 259 | + predicted = tf.equal(tf.argmax(predictions, 1), tf.argmax(actual, 1)) |
| 260 | + batch_accuracy = tf.reduce_mean(tf.cast(predicted, "float"), name="accuracy") |
| 261 | + batch_accuracy = batch_accuracy.eval() |
| 262 | + |
| 263 | + return batch_accuracy |
| 264 | + |
| 265 | + def predict(self, test_input1): |
| 266 | + # Test model |
| 267 | + self.istraining = False |
| 268 | + result = [] |
| 269 | + test_input1 = np.asarray(test_input1) |
| 270 | + record_size = len(test_input1) |
| 271 | + |
| 272 | + |
| 273 | + init = tf.global_variables_initializer() |
| 274 | + with tf.Session() as sess: |
| 275 | + sess.run(init) |
| 276 | + |
| 277 | + total_batch = int(record_size / self.batch_size) + 1 |
| 278 | + |
| 279 | + for i in range(total_batch): |
| 280 | + batch_x1 = self.__createTestBatch(test_input1, batch_size=self.batch_size) |
| 281 | + |
| 282 | + print(len(batch_x1)) |
| 283 | + predictions = sess.run([self.pred], feed_dict={self.x1: batch_x1}) |
| 284 | + print(predictions) |
| 285 | + result.extend(predictions[0]) |
| 286 | + # print(result) |
| 287 | + |
| 288 | + return result |
| 289 | + |
| 290 | + def generatePrediction(self,predictions): |
| 291 | + predicted = tf.argmax(predictions[0],1) |
| 292 | + predicted = predicted.eval() |
| 293 | + return predicted |
0 commit comments