In [30]:
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pylab import *
from tensorflow.examples.tutorials.mnist import input_data
%matplotlib inline
In [31]:
#read the datasets
train = pd.read_csv("intro_to_ann.csv")
trX, trY, teX, teY = np.array(train.ix[1:400,0:2]), np.array(train.ix[1:400,2:3]),np.array(train.ix[401:500,0:2]), np.array(train.ix[401:500,2:3])
In [32]:
def init_weights(shape):
return tf.Variable(tf.random_normal(shape, stddev=0.01))
In [33]:
def model(X, w_h, w_h2, w_o): # this network is the same as the previous one except with an extra hidden layer + dropout
h = tf.sigmoid(tf.matmul(X, w_h))
h2 = tf.sigmoid(tf.matmul(h, w_h2))
return tf.matmul(h2, w_o)
In [34]:
X = tf.placeholder("float", [None, 2])
Y = tf.placeholder("float", [None, 1])
In [35]:
w_h = init_weights([2, 256])
w_h2 = init_weights([256, 256])
w_o = init_weights([256, 1])
In [36]:
py_x = model(X, w_h, w_h2, w_o)
In [37]:
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y))
train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)
predict_op = tf.argmax(py_x, 1)
In [38]:
with tf.Session() as sess:
# you need to initialize all variables
tf.initialize_all_variables().run()
for i in range(100):
sess.run(train_op, feed_dict={X: trX, Y: trY})
print(i, np.mean(np.argmax(teY, axis=1) ==
sess.run(predict_op, feed_dict={X: teX, Y: teY})))
In [ ]:
In [ ]: