<jupyter_start><jupyter_text>Assignment - 4b)The best accuracy I got with the spam dataset is 97% ; Optimum numEpochs = 12000 and Learning rate = 0.0008<jupyter_code>from __future__ import division
import tensorflow as tf
import numpy as np
import tarfile
import os
import matplotlib.pyplot as plt
%matplotlib inline
import time
def csv_to_numpy_array(filePath, delimiter):
    return np.genfromtxt(filePath, delimiter=delimiter, dtype=None)
trainX = csv_to_numpy_array("data/trainX.csv", delimiter="\t")
trainY = csv_to_numpy_array("data/trainY.csv", delimiter="\t")
testX = csv_to_numpy_array("data/testX.csv", delimiter="\t")
testY = csv_to_numpy_array("data/testY.csv", delimiter="\t")
print(trainX.shape, trainY.shape, testX.shape, testY.shape)
numFeatures = trainX.shape[1]
numLabels = trainY.shape[1]
numHLnodes = 5
numEpochs = 12000
learningRate = tf.train.exponential_decay(learning_rate=0.0008,
                                          global_step= 1,
                                          decay_steps=trainX.shape[0],
                                          decay_rate= 0.95,
                                          staircase=True)
X = tf.placeholder(tf.float32, [None, numFeatures])
yGold = tf.placeholder(tf.float32, [None, numLabels])
weights = {
    'hiddenlayer': tf.Variable(tf.random_normal([numFeatures, numHLnodes], mean=0, stddev=(np.sqrt(6/numFeatures+numHLnodes+1)),name="weights1")),
    'outputlayer': tf.Variable(tf.random_normal([numHLnodes, numLabels], mean=0, stddev=(np.sqrt(6/numHLnodes+numLabels+1)),name="weights2"))
}


biases = {
    'b1': tf.Variable(tf.random_normal([1,numHLnodes],mean=0,stddev=(np.sqrt(6/numFeatures+numHLnodes+1)),name="bias1")),
    'b2': tf.Variable(tf.random_normal([1,numLabels],mean=0,stddev=(np.sqrt(6/numHLnodes+numLabels+1)),name="bias2"))
}

init_OP = tf.initialize_all_variables()

# PREDICTION ALGORITHM i.e. FEEDFORWARD ALGORITHM

#HIDDEN LAYER
apply_weights_HL = tf.matmul(X, weights['hiddenlayer'], name="apply_weights_1")
add_bias_HL = tf.add(apply_weights_HL, biases['b1'], name="add_bias1") 
activation_HL = tf.nn.sigmoid(add_bias_HL, name="activation1")

#OUTPUT LAYER
apply_weights_OP = tf.matmul(activation_HL, weights['outputlayer'], name="apply_weights_2")
add_bias_OP = tf.add(apply_weights_OP, biases['b2'], name="add_bias2") 
activation_OP = tf.nn.sigmoid(add_bias_OP, name="activation2")
cost_OP = tf.nn.l2_loss(activation_OP-yGold, name="squared_error_cost")
training_OP = tf.train.GradientDescentOptimizer(learningRate).minimize(cost_OP)
epoch_values=[]
accuracy_values=[]
cost_values=[]
errors = []
sess = tf.Session()
sess.run(init_OP)
correct_predictions_OP = tf.equal(tf.argmax(activation_OP,1),tf.argmax(yGold,1))
accuracy_OP = tf.reduce_mean(tf.cast(correct_predictions_OP, "float"))


# Training epochs
for i in range(numEpochs):
    sess.run([training_OP], feed_dict={X: trainX, yGold: trainY})
    print(sess.run([cost_OP], feed_dict={X: trainX, yGold: trainY}))
    accuracy_value = sess.run(accuracy_OP, feed_dict={X: testX, yGold: testY})
    errors.append(1-accuracy_value)
    print (errors[-1])
plt.plot([np.mean(errors[i-100:i]) for i in range(len(errors))])
plt.show()<jupyter_output>/usr/local/lib/python3.4/dist-packages/numpy/core/_methods.py:59: RuntimeWarning: Mean of empty slice.
  warnings.warn("Mean of empty slice.", RuntimeWarning)
{'fraction_non_alphanumeric': 0.14375186400238593, 'fraction_numerical': 0.022368028631076647, 'mean_word_length': 5.109289617486339, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '517795', 'license': 'Apache-2.0', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1191, 'n_words': 196, 'path': 'assignment/Meka-Rajitha/assign-4b-rajithameka.ipynb', 'repo_name': 'ml6973/Content', 'size': '3353'}