In [4]:
# Import the required packages
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import scipy
import math
import random
import string
import tensorflow as tf

random.seed(123)
# Display plots inline 
%matplotlib inline
# Define plot's default figure size
matplotlib.rcParams['figure.figsize'] = (10.0, 8.0)

In [90]:
# Read the datasets
train = pd.read_csv("../data/intro_to_ann.csv")
print (train.head())
X, Y = np.array(train.ix[:,0:2]), np.array(train.ix[:,2])
print(X.shape, Y.shape)

# Get sample and feature size from training data
size, features = X.shape
print("Sample Size =",size,"\nNumber of Features =",features)

plt.scatter(X[:,0], X[:,1], s=40, c=Y, cmap=plt.cm.BuGn)


   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]
(500, 2) (500,)
Sample Size = 500 
Number of Features = 2
Out[90]:
<matplotlib.collections.PathCollection at 0x7efebeb5ec18>
/usr/lib/python3/dist-packages/matplotlib/collections.py:549: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if self._edgecolors == 'face':

In [83]:
# Parameters
iterations = 2500
labels = 2
hiddenNodes = 4
learningRate = 0.01

labelsArray = (np.arange(labels) == Y[:,None]).astype(np.float32)

print(labelsArray[0:10])


Sample Size = 500 
Number of Features = 2
[[ 0.  1.]
 [ 0.  1.]
 [ 1.  0.]
 [ 1.  0.]
 [ 0.  1.]
 [ 1.  0.]
 [ 0.  1.]
 [ 0.  1.]
 [ 1.  0.]
 [ 1.  0.]]

In [95]:
# Tensorflow Graph Placeholders
x = tf.placeholder(tf.float32, shape=[None, features])
y = tf.placeholder(tf.float32, shape=[None, labels])

# Weights for NN Random Initially
wHid = tf.Variable(tf.random_normal([features, hiddenNodes]))
bHid = tf.Variable(tf.zeros([hiddenNodes]))
W = tf.Variable(tf.random_normal([hiddenNodes, labels]))
b = tf.Variable(tf.zeros([labels]))

# Create Model
hiddenLayer = tf.nn.softmax(tf.add(tf.matmul(x,wHid), bHid))
model = tf.nn.softmax(tf.add(tf.matmul(hiddenLayer,W),b))

# Error calculation, Mean Squared
error = -tf.reduce_sum(y*tf.log(model))

gradDescent = tf.train.GradientDescentOptimizer(learningRate).minimize(error)

# If model is correct
correctAnswer = tf.equal(tf.argmax(model,1), tf.argmax(y,1))
correctAccuracy = tf.reduce_mean(tf.cast(correctAnswer, tf.float32))

initialize = tf.initialize_all_variables()

In [97]:
# Find optimal weights and error
errors = []
with tf.Session() as session:
    session.run(initialize)
    for epoch in range(iterations):
        # Gradient descent on each iteration and find accuracy
        gradDescent.run(feed_dict={x:X, y:labelsArray})
        accuracy = correctAccuracy.eval(feed_dict={x:X, y:labelsArray})
        # Update errors 
        errors.append(1-accuracy)
    
    summary_writer = tf.train.SummaryWriter('./logs',session.graph)
    
    print("Optimization Finished!")
    
    print("Weight Matrix\n", session.run(W), "\nBias Matrix\n", session.run(b))
    print("Final error:", errors[-1])


Optimization Finished!
Weight Matrix
 [[ 4.76763535 -4.03631687]
 [ 3.15465713 -3.649297  ]
 [-3.19368052  2.77326989]
 [-3.57876539  3.32657003]] 
Bias Matrix
 [-0.65755564  0.65756738]
Final error: 0.0299999713898

In [98]:
plt.plot([np.mean(errors[i-50:i]) for i in range(len(errors))])
plt.show()


/usr/local/lib/python3.4/dist-packages/numpy/core/_methods.py:59: RuntimeWarning: Mean of empty slice.
  warnings.warn("Mean of empty slice.", RuntimeWarning)

In [ ]: