In [104]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from get_pong_data import get_objects_locations
#must compute current speed of the ball to check where it is going to
In [105]:
#get_objects_locations(train_data[1][0])
In [106]:
#480 #270 #1
In [107]:
train_data_inputs = np.load(file="train_data_inputs.npy", encoding='bytes')
train_data_labels = np.load(file="train_data_labels.npy", encoding='bytes')
In [108]:
extra_data_inputs = list()
extra_data_labels = list()
for d_input, d_label in zip(train_data_inputs, train_data_labels):
if d_label[1] == 1 or d_label[2] == 1:
for _ in range(5):
extra_data_inputs.append(d_input.copy())
extra_data_labels.append(d_label.copy())
extra_data_inputs = np.array(extra_data_inputs)
extra_data_labels = np.array(extra_data_labels)
print(extra_data_inputs.shape)
print(extra_data_labels.shape)
enhanced_train_data_inputs = np.concatenate((train_data_inputs, extra_data_inputs), axis=0)
enhanced_train_data_labels = np.concatenate((train_data_labels, extra_data_labels), axis=0)
print(enhanced_train_data_inputs.shape)
print(enhanced_train_data_labels.shape)
In [111]:
data_mean = np.mean(enhanced_train_data_inputs, axis=0)
data_std = np.std(enhanced_train_data_inputs, axis=0)
norm_enhanced_train_data_inputs = (enhanced_train_data_inputs - data_mean) / data_std
In [112]:
#data_std
In [121]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(norm_enhanced_train_data_inputs, enhanced_train_data_labels, test_size=0.4, random_state=42)
X_valid, X_test, y_valid, y_test = train_test_split(X_test, y_test, test_size=0.5, random_state=42)
In [122]:
list1 = list(range(0,10))
list2 = list(range(0,10))
train_test_split(list1, list2, test_size=0.2, random_state=5)
Out[122]:
In [123]:
X_train.shape
Out[123]:
In [128]:
def pong_dense_model():
inputs = tf.placeholder(dtype=tf.float32, shape=[None, 8])
y = tf.placeholder(dtype=tf.float32, shape=[None, 3])
h1 = tf.layers.dense(inputs, 20, activation=tf.nn.relu)
h2 = tf.layers.dense(h1, 20, activation=tf.nn.relu)
h3 = tf.layers.dense(h2, 10, activation=tf.nn.relu)
logits = tf.layers.dense(h3, 3, activation=None)
sc = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y)
cost = tf.reduce_mean(sc)
# Accuracy
correct_pred = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy')
return inputs, y, logits, cost, accuracy
In [129]:
#from pong_model import pong_model
# Remove previous weights, bias, inputs, etc..
tf.reset_default_graph()
inputs, y, logits, cost, accuracy = pong_dense_model()
optimizer = tf.train.AdamOptimizer(0.01).minimize(cost)
In [131]:
from random import shuffle
import sklearn
from sklearn.model_selection import train_test_split
import matplotlib
epochs = 10000
#atch_size = 1000
cost_list = []
acc_list = []
with tf.Session() as sess:
# Initializing the variables
sess.run(tf.global_variables_initializer())
for e in range(epochs):
#for b in range(0, 800, batch_size):
cost_value, _ = sess.run([cost, optimizer], feed_dict={
inputs: X_train,
y: y_train
})
if e % 500 == 0:
print("Epoch: {} Cost: {}".format(e, cost_value))
cost_list.append(cost_value)
acc_value = sess.run(accuracy, feed_dict={
inputs: X_valid,
y: y_valid
})
acc_list.append(acc_value)
if e % 500 == 0:
print("Accuracy: {}".format(acc_value))
print("")
#Calculate final accuracy
final_acc = sess.run(accuracy, feed_dict={
inputs: X_test,
y: y_test
})
plt.plot(cost_list)
plt.show()
plt.plot(acc_list)
plt.show()
print("Final accuracy: {}".format(final_acc))
saver = tf.train.Saver()
save_path = saver.save(sess, "./model/model2.ckpt")
In [ ]: