<jupyter_start><jupyter_text>subLSTM test<jupyter_code>%load_ext autoreload
%autoreload 2
%matplotlib inline

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
import sys

sys.path.append('../models/')
from lstm import *

sys.path.append('../src/common/')
from helper import *<jupyter_output><empty_output><jupyter_text>Placeholder and initializers<jupyter_code>hidden_layer_size = 100
target_size = 10
training_steps = 1000
batch_size = 50
time_steps = 784
n_classes = 10
#input image placeholder
X = tf.placeholder(tf.float32, [None, time_steps])
#input label placeholder
y = tf.placeholder(tf.float32, [None, n_classes])<jupyter_output><empty_output><jupyter_text>Models<jupyter_code># Initializing rnn object
rnn = LSTM_cell(time_steps, hidden_layer_size, target_size)

#rnn = tf.nn.rnn_cell.LSTMCell(100, state_is_tuple=True)
#rnn = tf.nn.rnn_cell.MultiRNNCell([rnn] * 2, state_is_tuple=True)
#init_state = rnn.zero_state(batch_size, tf.float32)
#rnn_outputs, final_state = tf.nn.dynamic_rnn(rnn, X, initial_state=init_state)

# Getting all outputs from rnn
outputs = rnn.get_outputs()

# Getting final output through indexing after reversing
last_output = outputs[-1]

# As rnn model output the final layer through Relu activation softmax is
# used for final output
output = tf.nn.softmax(last_output)

# Computing the Cross Entropy loss
cross_entropy = -tf.reduce_sum(y * tf.log(output))

# Trainning with RMSProp Optimizer
train_step = tf.train.RMSPropOptimizer(1e-4).minimize(cross_entropy)

# Calculation of correct prediction and accuracy
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(output, 1))
accuracy = (tf.reduce_mean(tf.cast(correct_prediction, tf.float32))) * 100<jupyter_output><empty_output><jupyter_text>Load dataset<jupyter_code>from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)<jupyter_output>Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz<jupyter_text>Training<jupyter_code>sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())

n_epoch = 200000
losses = np.empty(n_epoch)
train_accuracies = np.empty(n_epoch)
test_accuracies = np.empty(n_epoch)

#Iterations to do trainning
for epoch in range(n_epoch):

    X, Y = mnist.train.next_batch(batch_size)
    X = X.reshape(batch_size, 1, 784)
    sess.run(train_step,feed_dict={rnn._inputs:X, y:Y})

    if epoch%100 == 0:
        Loss=str(sess.run(cross_entropy,feed_dict={rnn._inputs:X, y:Y}))
        Train_accuracy=str(sess.run(accuracy,feed_dict={rnn._inputs:X, y:Y}))
        X_test = mnist.test.images.reshape(10000,1,784)
        Test_accuracy=str(sess.run(accuracy,feed_dict={rnn._inputs:X_test, y:mnist.test.labels}))
    
        losses[epoch] = Loss
        train_accuracies[epoch] = Train_accuracy
        test_accuracies[epoch] = Test_accuracy
    
        print("\rIteration: %s Loss: %s Train Accuracy: %s Test Accuracy: %s"%(epoch,Loss,Train_accuracy,Test_accuracy)),<jupyter_output>Iteration: 0 Loss: 115.176 Train Accuracy: 8.0 Test Accuracy: 9.82
Iteration: 100 Loss: 114.894 Train Accuracy: 36.0 Test Accuracy: 35.96
Iteration: 200 Loss: 110.988 Train Accuracy: 52.0 Test Accuracy: 44.88
Iteration: 300 Loss: 105.726 Train Accuracy: 44.0 Test Accuracy: 48.68
Iteration: 400 Loss: 96.5271 Train Accuracy: 54.0 Test Accuracy: 50.6
Iteration: 500 Loss: 85.4562 Train Accuracy: 50.0 Test Accuracy: 52.83
Iteration: 600 Loss: 78.3674 Train Accuracy: 58.0 Test Accuracy: 58.59
Iteration: 700 Loss: 56.5332 Train Accuracy: 78.0 Test Accuracy: 64.74
Iteration: 800 Loss: 68.5007 Train Accuracy: 70.0 Test Accuracy: 68.01
Iteration: 900 Loss: 52.2507 Train Accuracy: 80.0 Test Accuracy: 71.13
Iteration: 1000 Loss: 53.8068 Train Accuracy: 78.0 Test Accuracy: 75.6
Iteration: 1100 Loss: 43.8281 Train Accuracy: 80.0 Test Accuracy: 78.07
Iteration: 1200 Loss: 50.8524 Train Accuracy: 76.0 Test Accuracy: 79.45
Iteration: 1300 Loss: 50.8523 Train Accuracy: 76.0 Test Accuracy: 80.31
Iteratio[...]<jupyter_text>Peak found of 97.91 at iteration 184500 - paper: 97.96<jupyter_code>plt.plot(range(n_epoch),losses,'b.',)
plt.plot(range(n_epoch),train_accuracies,'r*',)
plt.plot(range(n_epoch),test_accuracies,'g+')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup