Assignment 4

Abhijith Ravikumar Puthussery (qaw164)

Here is a link to a sample Logistic Regression model for email spam/ham classification: http://jrmeyer.github.io/tutorial/2016/02/01/TensorFlow-Tutorial.html

Follow the steps in the tutorial and write a Tensorflow program using your cloud ML instance. Questions: a)

  • What's the best accuracy you get on the test dataset?
  • What's the optimum value of the numEpochs and the learningRate.
  • Plot the error with Matplotlib

b)

  • Add new neural network layers and training the model again. comapre the error function between these two models.

In [11]:
import matplotlib
matplotlib.use('Agg')
from __future__ import division
import tensorflow as tf
import numpy as np
import tarfile
import os
import matplotlib.pyplot as plt
import time
%matplotlib inline


/usr/lib/python3/dist-packages/matplotlib/__init__.py:1175: UserWarning:  This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

  warnings.warn(_use_error_msg)

In [12]:
## Import Data

def csv_to_numpy_array(filePath, delimiter):
    return np.genfromtxt(filePath, delimiter=delimiter, dtype=None)

def import_data():
    if "data" not in os.listdir(os.getcwd()):
        # Untar directory of data if we haven't already
        tarObject = tarfile.open("data.tar.gz")
        tarObject.extractall()
        tarObject.close()
        print("Extracted tar to current directory")
    else:
        # we've already extracted the files
        pass

    print("loading training data")
    trainX = csv_to_numpy_array("data/trainX.csv", delimiter="\t")
    trainY = csv_to_numpy_array("data/trainY.csv", delimiter="\t")
    print("loading test data")
    testX = csv_to_numpy_array("data/testX.csv", delimiter="\t")
    testY = csv_to_numpy_array("data/testY.csv", delimiter="\t")
    return trainX,trainY,testX,testY

trainX,trainY,testX,testY = import_data()


loading training data
loading test data

In [13]:
# DATA SET PARAMETERS
# Get our dimensions for our different variables and placeholders:
# numFeatures = the number of words extracted from each email(great,dog,pill)
numFeatures = trainX.shape[1]
# numLabels = number of classes we are predicting (here just 2: Ham or Spam)
numLabels = trainY.shape[1]

# TRAINING SESSION PARAMETERS
# number of times we iterate through training data
# tensorboard shows that accuracy plateaus at ~25k epochs
numEpochs = 1000
# a smarter learning rate for gradientOptimizer
learningRate = tf.train.exponential_decay(learning_rate=0.0008,
                                          global_step= 1,
                                          decay_steps=trainX.shape[0],
                                          decay_rate= 0.95,
                                          staircase=True)

In [14]:
# Define Hidden Layer
hidden=4

In [15]:
### Placeholders

# X = X-matrix / feature-matrix / data-matrix... It's a tensor to hold our email data. '
# None' here means that we can hold any number of emails
X = tf.placeholder(tf.float32, [None, numFeatures])
# yGold = Y-matrix / label-matrix / labels... This will be our correct answers matrix. 
# Every row has either [1,0] for SPAM or [0,1] for HAM. 'None' here means that we can hold any number of emails
yGold = tf.placeholder(tf.float32, [None, numLabels])

In [18]:
# Values are randomly sampled from a Gaussian with a standard deviation of sqrt(6 / (numInputNodes + numOutputNodes + 1))

hidden_weights = tf.Variable(tf.random_normal([numFeatures,hidden],
                                       mean=0,
                                       stddev=(np.sqrt(6/numFeatures+numLabels+1)),
                                       name="hidden_weights"))
weights = tf.Variable(tf.random_normal([hidden,numLabels],
                                       mean=0,
                                       stddev=(np.sqrt(6/numFeatures+numLabels+1)),
                                       name="weights"))

bias = tf.Variable(tf.random_normal([1,numLabels],
                                    mean=0,
                                    stddev=(np.sqrt(6/numFeatures+numLabels+1)),
                                    name="bias"))

In [19]:
### Prediction Ops

# INITIALIZE our weights and biases
init_OP = tf.initialize_all_variables()

# PREDICTION ALGORITHM i.e. FEEDFORWARD ALGORITHM
apply_hidden_weights_OP = tf.matmul(X, hidden_weights, name="apply_hidden_weights")
apply_weights_OP = tf.matmul(apply_hidden_weights_OP, weights, name="apply_weights")
add_bias_OP = tf.add(apply_weights_OP, bias, name="add_bias") 
activation_OP = tf.nn.sigmoid(add_bias_OP, name="activation")

In [20]:
# COST FUNCTION i.e. MEAN SQUARED ERROR
cost_OP = tf.nn.l2_loss(activation_OP-yGold, name="squared_error_cost")

In [21]:
# OPTIMIZATION ALGORITHM i.e. GRADIENT DESCENT
training_OP = tf.train.GradientDescentOptimizer(learningRate).minimize(cost_OP)

In [22]:
### Visualization

epoch_values=[]
accuracy_values=[]
cost_values=[]

In [23]:
# Turn on interactive plotting
plt.ion()
# Create the main, super plot
fig = plt.figure()
# Create two subplots on their own axes and give titles
ax1 = plt.subplot("211")
ax1.set_title("TRAINING ACCURACY", fontsize=10)
ax2 = plt.subplot("212")
ax2.set_title("TRAINING COST", fontsize=10)
# plt.tight_layout()

# Create a tensorflow session
sess = tf.Session()

# Initialize all tensorflow variables
sess.run(init_OP)

## Ops for vizualization
# argmax(activation_OP, 1) gives the label our model thought was most likely
# argmax(yGold, 1) is the correct label
correct_predictions_OP = tf.equal(tf.argmax(activation_OP,1),tf.argmax(yGold,1))
# False is 0 and True is 1, what was our average?
accuracy_OP = tf.reduce_mean(tf.cast(correct_predictions_OP, "float"))
# Summary op for regression output
activation_summary_OP = tf.histogram_summary("output", activation_OP)
# Summary op for accuracy
accuracy_summary_OP = tf.scalar_summary("accuracy", accuracy_OP)
# Summary op for cost
cost_summary_OP = tf.scalar_summary("cost", cost_OP)
# Summary ops to check how variables (W, b) are updating after each iteration
weightSummary = tf.histogram_summary("weights", weights.eval(session=sess))
biasSummary = tf.histogram_summary("biases", bias.eval(session=sess))
# Merge all summaries
#####all_summary_OPS = tf.merge_all_summaries()
# Summary writer
#writer = tf.train.SummaryWriter("summary_logs", sess.graph_def)

# Initialize reporting variables
cost = 0
diff = 1

# Training epochs
for i in range(numEpochs):
    if i > 1 and diff < .0001:
        print("change in cost %g; convergence."%diff)
        break
    else:
        # Run training step
        step = sess.run(training_OP, feed_dict={X: trainX, yGold: trainY})
        # Report occasional stats
        if i % 10 == 0:
            # Add epoch to epoch_values
            epoch_values.append(i)
            # Generate accuracy stats on test data
            #summary_results = sess.run(all_summary_OPS, feed_dict={X: trainX, yGold: trainY})
            train_accuracy = sess.run(accuracy_OP, feed_dict={X: trainX, yGold: trainY})
            newCost = sess.run(cost_OP, feed_dict={X: trainX, yGold: trainY})
            # Add accuracy to live graphing variable
            accuracy_values.append(train_accuracy)
            # Add cost to live graphing variable
            cost_values.append(newCost)
            # Write summary stats to writer
            #writer.add_summary(summary_results, i)
            # Re-assign values for variables
            diff = abs(newCost - cost)
            cost = newCost
            
            #generate print statements
            print("step %d, training accuracy %g"%(i, train_accuracy))
            print("step %d, cost %g"%(i, newCost))
            print("step %d, change in cost %g"%(i, diff))

            # Plot progress to our two subplots
            accuracyLine, = ax1.plot(epoch_values, accuracy_values)
            costLine, = ax2.plot(epoch_values, cost_values)
            fig.canvas.draw()
            time.sleep(1)


# How well do we perform on held-out test data?
print("final accuracy on test set: %s" %str(sess.run(accuracy_OP, 
                                                     feed_dict={X: testX, 
                                                                yGold: testY})))
plt.plot(epoch_values,accuracy_values)


step 0, training accuracy 0.4617
step 0, cost 347.439
step 0, change in cost 347.439
step 10, training accuracy 0.463799
step 10, cost 336.726
step 10, change in cost 10.7136
step 20, training accuracy 0.474292
step 20, cost 323.819
step 20, change in cost 12.9064
step 30, training accuracy 0.474292
step 30, cost 308.626
step 30, change in cost 15.1938
step 40, training accuracy 0.478489
step 40, cost 292.422
step 40, change in cost 16.2037
step 50, training accuracy 0.50787
step 50, cost 277.727
step 50, change in cost 14.6953
step 60, training accuracy 0.520462
step 60, cost 266.572
step 60, change in cost 11.1544
step 70, training accuracy 0.533054
step 70, cost 259.107
step 70, change in cost 7.4649
step 80, training accuracy 0.528856
step 80, cost 254.257
step 80, change in cost 4.85068
step 90, training accuracy 0.521511
step 90, cost 250.904
step 90, change in cost 3.35303
step 100, training accuracy 0.527807
step 100, cost 248.324
step 100, change in cost 2.58002
step 110, training accuracy 0.524659
step 110, cost 246.129
step 110, change in cost 2.19507
step 120, training accuracy 0.528856
step 120, cost 244.127
step 120, change in cost 2.00145
step 130, training accuracy 0.533054
step 130, cost 242.229
step 130, change in cost 1.8985
step 140, training accuracy 0.541448
step 140, cost 240.39
step 140, change in cost 1.8385
step 150, training accuracy 0.546695
step 150, cost 238.591
step 150, change in cost 1.79904
step 160, training accuracy 0.557188
step 160, cost 236.821
step 160, change in cost 1.77007
step 170, training accuracy 0.564533
step 170, cost 235.074
step 170, change in cost 1.7468
step 180, training accuracy 0.570829
step 180, cost 233.347
step 180, change in cost 1.72733
step 190, training accuracy 0.582371
step 190, cost 231.636
step 190, change in cost 1.7104
step 200, training accuracy 0.592865
step 200, cost 229.94
step 200, change in cost 1.69594
step 210, training accuracy 0.603358
step 210, cost 228.257
step 210, change in cost 1.6835
step 220, training accuracy 0.612802
step 220, cost 226.584
step 220, change in cost 1.67268
step 230, training accuracy 0.620147
step 230, cost 224.92
step 230, change in cost 1.66385
step 240, training accuracy 0.627492
step 240, cost 223.264
step 240, change in cost 1.65665
step 250, training accuracy 0.63064
step 250, cost 221.613
step 250, change in cost 1.6507
step 260, training accuracy 0.644281
step 260, cost 219.967
step 260, change in cost 1.64636
step 270, training accuracy 0.652676
step 270, cost 218.324
step 270, change in cost 1.64325
step 280, training accuracy 0.656873
step 280, cost 216.682
step 280, change in cost 1.64136
step 290, training accuracy 0.663169
step 290, cost 215.042
step 290, change in cost 1.64053
step 300, training accuracy 0.667366
step 300, cost 213.401
step 300, change in cost 1.64058
step 310, training accuracy 0.671564
step 310, cost 211.759
step 310, change in cost 1.6418
step 320, training accuracy 0.673662
step 320, cost 210.116
step 320, change in cost 1.64339
step 330, training accuracy 0.684155
step 330, cost 208.47
step 330, change in cost 1.64569
step 340, training accuracy 0.690451
step 340, cost 206.821
step 340, change in cost 1.64867
step 350, training accuracy 0.699895
step 350, cost 205.169
step 350, change in cost 1.65207
step 360, training accuracy 0.705142
step 360, cost 203.514
step 360, change in cost 1.65572
step 370, training accuracy 0.710388
step 370, cost 201.854
step 370, change in cost 1.65959
step 380, training accuracy 0.714586
step 380, cost 200.19
step 380, change in cost 1.6637
step 390, training accuracy 0.721931
step 390, cost 198.523
step 390, change in cost 1.66777
step 400, training accuracy 0.727177
step 400, cost 196.851
step 400, change in cost 1.67191
step 410, training accuracy 0.729276
step 410, cost 195.175
step 410, change in cost 1.67577
step 420, training accuracy 0.732424
step 420, cost 193.495
step 420, change in cost 1.67967
step 430, training accuracy 0.737671
step 430, cost 191.812
step 430, change in cost 1.68317
step 440, training accuracy 0.741868
step 440, cost 190.126
step 440, change in cost 1.68631
step 450, training accuracy 0.748164
step 450, cost 188.437
step 450, change in cost 1.68922
step 460, training accuracy 0.748164
step 460, cost 186.745
step 460, change in cost 1.69159
step 470, training accuracy 0.751312
step 470, cost 185.052
step 470, change in cost 1.69347
step 480, training accuracy 0.755509
step 480, cost 183.357
step 480, change in cost 1.69495
step 490, training accuracy 0.759706
step 490, cost 181.661
step 490, change in cost 1.69571
step 500, training accuracy 0.760755
step 500, cost 179.965
step 500, change in cost 1.69594
step 510, training accuracy 0.762854
step 510, cost 178.269
step 510, change in cost 1.69559
step 520, training accuracy 0.76915
step 520, cost 176.575
step 520, change in cost 1.69461
step 530, training accuracy 0.773347
step 530, cost 174.882
step 530, change in cost 1.69289
step 540, training accuracy 0.776495
step 540, cost 173.191
step 540, change in cost 1.69044
step 550, training accuracy 0.781742
step 550, cost 171.504
step 550, change in cost 1.68745
step 560, training accuracy 0.785939
step 560, cost 169.82
step 560, change in cost 1.68372
step 570, training accuracy 0.790136
step 570, cost 168.141
step 570, change in cost 1.67924
step 580, training accuracy 0.795383
step 580, cost 166.467
step 580, change in cost 1.67416
step 590, training accuracy 0.798531
step 590, cost 164.799
step 590, change in cost 1.66824
step 600, training accuracy 0.802728
step 600, cost 163.137
step 600, change in cost 1.6618
step 610, training accuracy 0.809024
step 610, cost 161.482
step 610, change in cost 1.65451
step 620, training accuracy 0.813221
step 620, cost 159.836
step 620, change in cost 1.64667
step 630, training accuracy 0.81532
step 630, cost 158.197
step 630, change in cost 1.63823
step 640, training accuracy 0.81532
step 640, cost 156.568
step 640, change in cost 1.62895
step 650, training accuracy 0.816369
step 650, cost 154.949
step 650, change in cost 1.61919
step 660, training accuracy 0.819517
step 660, cost 153.34
step 660, change in cost 1.60887
step 670, training accuracy 0.822665
step 670, cost 151.742
step 670, change in cost 1.59792
step 680, training accuracy 0.820567
step 680, cost 150.156
step 680, change in cost 1.58635
step 690, training accuracy 0.825813
step 690, cost 148.582
step 690, change in cost 1.57437
step 700, training accuracy 0.826863
step 700, cost 147.02
step 700, change in cost 1.56175
step 710, training accuracy 0.83001
step 710, cost 145.471
step 710, change in cost 1.54883
step 720, training accuracy 0.836306
step 720, cost 143.936
step 720, change in cost 1.53531
step 730, training accuracy 0.841553
step 730, cost 142.415
step 730, change in cost 1.52135
step 740, training accuracy 0.844701
step 740, cost 140.907
step 740, change in cost 1.50705
step 750, training accuracy 0.8468
step 750, cost 139.415
step 750, change in cost 1.4924
step 760, training accuracy 0.850997
step 760, cost 137.938
step 760, change in cost 1.47729
step 770, training accuracy 0.850997
step 770, cost 136.476
step 770, change in cost 1.46204
step 780, training accuracy 0.853095
step 780, cost 135.029
step 780, change in cost 1.44632
step 790, training accuracy 0.853095
step 790, cost 133.599
step 790, change in cost 1.43037
step 800, training accuracy 0.856243
step 800, cost 132.185
step 800, change in cost 1.41428
step 810, training accuracy 0.858342
step 810, cost 130.787
step 810, change in cost 1.39792
step 820, training accuracy 0.859391
step 820, cost 129.405
step 820, change in cost 1.38136
step 830, training accuracy 0.86149
step 830, cost 128.041
step 830, change in cost 1.36461
step 840, training accuracy 0.863589
step 840, cost 126.693
step 840, change in cost 1.34776
step 850, training accuracy 0.863589
step 850, cost 125.362
step 850, change in cost 1.33081
step 860, training accuracy 0.864638
step 860, cost 124.049
step 860, change in cost 1.31372
step 870, training accuracy 0.866737
step 870, cost 122.752
step 870, change in cost 1.29664
step 880, training accuracy 0.870934
step 880, cost 121.473
step 880, change in cost 1.27941
step 890, training accuracy 0.874082
step 890, cost 120.21
step 890, change in cost 1.2622
step 900, training accuracy 0.875131
step 900, cost 118.965
step 900, change in cost 1.24501
step 910, training accuracy 0.87618
step 910, cost 117.738
step 910, change in cost 1.22773
step 920, training accuracy 0.87723
step 920, cost 116.527
step 920, change in cost 1.21063
step 930, training accuracy 0.87723
step 930, cost 115.333
step 930, change in cost 1.19348
step 940, training accuracy 0.879328
step 940, cost 114.157
step 940, change in cost 1.1764
step 950, training accuracy 0.881427
step 950, cost 112.998
step 950, change in cost 1.15937
step 960, training accuracy 0.882476
step 960, cost 111.855
step 960, change in cost 1.14247
step 970, training accuracy 0.884575
step 970, cost 110.73
step 970, change in cost 1.12569
step 980, training accuracy 0.886674
step 980, cost 109.621
step 980, change in cost 1.10899
step 990, training accuracy 0.886674
step 990, cost 108.528
step 990, change in cost 1.09241
final accuracy on test set: 0.933333
Out[23]:
[<matplotlib.lines.Line2D at 0x7fa7c6c18a90>]

Optimum value of the numEpochs=1000 and the learningRate=0.8. Final accuracy on test set=0.93333


In [ ]: