In [208]:
import tensorflow as tf
import numpy as np
import pandas as pd
import scipy
import random
import string

learningrate=0.01
iterations=2000
num_inputs=2
num_hidden=4
num_output=2
#import the data from the csv file
tran = pd.read_csv("intro_to_ann.csv")
print (tran.head())
X_data, Y_data = np.array(train.ix[:,0:2]), np.array(train.ix[:,2])
#split into training and test data labels
#reshape the array into one hot vector
Y_reshape = (np.arange(2) == Y_data[:, None]).astype(np.float32)
#keep placeholder values for x and y
X = tf.placeholder("float", shape=[None, 2])
Y = tf.placeholder("float", shape=[None,2])
#Assign weights as variables since these values keep changing
W1 = tf.Variable(tf.random_normal([num_inputs, num_hidden]))
b1 = tf.Variable(tf.zeros([4]))
y1 = tf.matmul(X, W1) + b1
W2 = tf.Variable(tf.random_normal([num_hidden, num_output]))
b2 = tf.Variable(tf.zeros([num_output]))
y2 = tf.nn.sigmoid(tf.matmul(y1, W2) + b2)
#loss function is a cross entropy function which tries to reduce the losss
cross_entropy = tf.reduce_mean(-tf.reduce_sum(Y* tf.log(y2), reduction_indices=[1]))
#We use a GradientDescent Optimizer which uses learning rate of 0.01 
optimizer = tf.train.GradientDescentOptimizer(learningrate).minimize(cross_entropy)
#It initializes all the variables
init = tf.initialize_all_variables()
#This is where actual computation of graph happens
with tf.Session() as sess:
    sess.run(init)
    #compare the actual value with the predicted value
    correct_prediction = tf.equal(tf.argmax(y2,1), tf.argmax(Y,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    for i in range(iterations):
        
        sess.run(optimizer, feed_dict={X: X_data, Y: Y_reshape})
        accuracy_value = sess.run(accuracy, feed_dict={X: X_data, Y: Y_reshape})
        errors.append(1 - accuracy_value)
    #We append the errors in an array and print the last minimized error
    print(sess.run(W2))
    print("Error: ", errors[-1])


   Feature1  Feature2  Target
0  2.067788  0.258133       1
1  0.993994 -0.609145       1
2 -0.690315  0.749921       0
3  1.023582  0.529003       0
4  0.700747 -0.496724       1

[5 rows x 3 columns]
[[ 2.23285508  0.98599917]
 [-1.26625991 -0.54991925]
 [-0.68401438  0.45249093]
 [-1.10392606  1.56885862]]
Error:  0.240000009537

In [ ]: