Simple feed forward neural network on Tensorflow question
I'm building a simple feedforward neural network in Tensorflow and something seems to be broken. I have adopted the basic structure from http://joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/ and have tried to adapt it to spit out 1 output unit instead. The input is 10 numerically represented players ex:(5, 17, 19, 54, 110, 3, 112, 78, 60, 63), 5 on the 'North' team and 5 on the 'South' team, and the output is a 1 or 0 (if the 'North' team won or not). 458
3 hidden layers (200 parameters each). With how this is set up, the algorithm trains to constantly predict 1 (North victory). What can I fix that allows this to train to predict which team is going to have a higher chance of winning? My expected outcome is ex: [.54] (slight edge to the north team) or [.40] (edge to the south team) etc.
import numpy as np
import tensorflow as tf
import sqlite3
def get_matches(start, stop):
connection = sqlite3.connect('5v5.db')
cursor = connection.cursor()
result = cursor.execute('''SELECT * FROM matches WHERE rowid>=? AND rowid<=?''', (start, stop))
return result.fetchall()
def init_weights(shape):
return tf.Variable(tf.random_normal(shape, stddev=0.01))
def model(X, w_h1, w_h2, w_h3, w_o):
h1 = tf.nn.sigmoid(tf.matmul(X, w_h1))
h2 = tf.nn.sigmoid(tf.matmul(h1, w_h2))
h3 = tf.nn.sigmoid(tf.matmul(h2, w_h3))
return tf.nn.sigmoid(tf.matmul(h3, w_o))
if __name__ == "__main__":
TRAINING_GAMES = 2500 #number of games to train on
TEST_GAMES = 500 #number of games to test the results on
NUM_DIGITS = 10 #input units
FIRST_HIDDEN_LAYER = 200
SECOND_HIDDEN_LAYER = 200
THIRD_HIDDEN_LAYER = 200
BATCH_SIZE = 150
training_matches = get_matches(1, TRAINING_GAMES)
trX = np.array([match[1:11] for match in training_matches])
trY = np.array([[match[11]] for match in training_matches])
X = tf.placeholder("float")
Y = tf.placeholder("float")
w_h1 = init_weights([NUM_DIGITS, FIRST_HIDDEN_LAYER])
w_h2 = init_weights([FIRST_HIDDEN_LAYER, SECOND_HIDDEN_LAYER])
w_h3 = init_weights([SECOND_HIDDEN_LAYER, THIRD_HIDDEN_LAYER])
w_o = init_weights([THIRD_HIDDEN_LAYER, 1])
py_x = model(X, w_h1, w_h2, w_h3, w_o)
cost = tf.square(Y - py_x)
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost)
predict_op = py_x
with tf.Session() as sess:
tf.initialize_all_variables().run()
for epoch in range(10000):
p = np.random.permutation(range(len(trX)))
trX, trY = trX[p], trY[p]
for start in range(0, len(trX), BATCH_SIZE):
end = start + BATCH_SIZE
sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})
print(epoch, sess. 695