def train_convolutional_neural_network(train, train_labels, test, test_labels, configuration, shuffle): tf.reset_default_graph() x, y, prediction = create_model(configuration) cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y)) optimizer = tf.train.AdamOptimizer(config.LEARNING_RATE).minimize(cost) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # Repeat training for N_EPOCHS for epoch in range(config.N_EPOCHS): epoch_loss = 0 i = 0 train_np = np.array(train) train_labels_np = np.array(train_labels) if (shuffle is True): # shuffle data in each epoch train_np, train_labels_np = parse.shuffle_data( train_np, train_labels_np) while i < len(train): start = i end = i + config.N_BATCH batch_x = np.array(train_np[start:end], dtype=object) batch_y = np.array(train_labels_np[start:end]) _, c = sess.run([optimizer, cost], feed_dict={ x: batch_x, y: batch_y }) epoch_loss += c i += config.N_BATCH # After each epoch, calculate loss s = 'Epoch ' + str(epoch) + ', loss: ' + str(epoch_loss) print(s) correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct, 'float')) accuracy_value = accuracy.eval({x: test, y: test_labels}) return accuracy_value
def train_neural_network(train, train_labels, test, test_labels, configuration, shuffle): tf.reset_default_graph() # Prepare tensor shapes, hidden layers, weights and biases x, y, prediction = create_model(configuration) cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y)) optimizer = tf.train.AdamOptimizer(config.LEARNING_RATE).minimize(cost) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for epoch in range(config.N_EPOCHS): epoch_loss = 0 i = 0 # Shuffle data in each epoch if (shuffle is True): train, train_labels = parse.shuffle_data(train, train_labels) # Present whole training set to neural network in batches while i < len(train): start = i end = i + config.N_BATCH batch_x = np.array(train[start:end]) batch_y = np.array(train_labels[start:end]) _, c = sess.run([optimizer, cost], feed_dict={ x: batch_x, y: batch_y }) # Count lost epoch_loss += c i += config.N_BATCH # After each epoch, calculate loss s = 'Epoch ' + str(epoch) + ', loss: ' + str(epoch_loss) print(s) correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct, 'float')) accuracy_value = accuracy.eval({x: test, y: test_labels}) return accuracy_value