Created
October 9, 2016 13:05
-
-
Save HasseIona/4bcaf9f95ae828e056d5210a2ea07f88 to your computer and use it in GitHub Desktop.
simple neural network with tensorflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import pickle as p | |
import numpy as np | |
#first_file is a file containing 30000 lists. Each list consists of 2 sublist, | |
#one containing champion (input) data and the second containing item (correct output) data | |
#both the champion and item data take the form of a list filled with 1´s and 0´s | |
first_file = p.load(open("data/0-30000.pickle", "rb")) | |
first_file = np.array(first_file) #necessary for simplifying the next two lines, which would else have to be handled by a for loop | |
training_inputs = list(first_file[:,0][:]) | |
training_outputs = list(first_file[:,1][:]) | |
nr_input_neurons = len(first_file[0][0]) | |
nr_hidden_neurons = 2000 | |
nr_output_neurons = len(first_file[0][1]) | |
nr_training_examples = len(first_file) | |
batch_size = 100 | |
#the input data for my network, later to be filled with batches of training_inputs | |
champion_data = tf.placeholder('float', [None, nr_input_neurons]) | |
#the output data for my network, later to be filled with batches of training_outputs | |
item_data = tf.placeholder('float', [None, nr_output_neurons]) | |
#The neural network | |
def neural_network_model(input_data): | |
#define the weights and the biases of the hidden and output layer | |
hidden_layer_param = {'weights': tf.Variable(tf.random_normal([nr_input_neurons, nr_hidden_neurons])), | |
'biases': tf.Variable(tf.random_normal([nr_hidden_neurons]))} | |
output_layer_param = {'weights': tf.Variable(tf.random_normal([nr_hidden_neurons, nr_output_neurons])), | |
'biases': tf.Variable(tf.random_normal([nr_output_neurons]))} | |
#run the input data through the network by multiplying by the weights and adding to the biases | |
hidden_layer_output = tf.add(tf.matmul(input_data, hidden_layer_param['weights']), hidden_layer_param['biases']) | |
hidden_layer_output = tf.nn.relu(hidden_layer_output) | |
output_layer_output = tf.add(tf.matmul(hidden_layer_output, output_layer_param['weights']), output_layer_param['biases']) | |
#return the output of the network (the network´s ¨prediction¨) | |
return output_layer_output | |
#The code necessary to run the neural network | |
def train_neural_network(champion_data): | |
#put the data into the network and calculate the predicted output | |
prediction = neural_network_model(champion_data) | |
#calculate the cost based on the prediction and the actually correct item data | |
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction, item_data)) | |
#optimize the network by minimizing the cost | |
optimizer = tf.train.AdamOptimizer().minimize(cost) | |
nr_epochs = 10 | |
with tf.Session() as tf_session: | |
tf_session.run(tf.initialize_all_variables()) | |
#run the data through the network 10 times (epochs) | |
for epoch in range(nr_epochs): | |
batch_offset = 0 | |
#create the batches and run the network one batch at a time | |
while batch_offset < len(training_inputs): | |
batch_input = np.array(training_inputs[batch_offset:batch_offset + batch_size]) | |
batch_output = np.array(training_outputs[batch_offset:batch_offset + batch_size]) | |
_, cost = tf_session.run([optimizer, cost], feed_dict = {champion_data: batch_input, item_data: batch_output}) | |
batch_offset += batch_size | |
train_neural_network(champion_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment