Question: Study the following RNN code for image classification and list the dimensions of the input ( X ), dimensions of outputs, states of the RNN
Study the following RNN code for image classification and list the dimensions of the input (X), dimensions of outputs, states of the RNN layer and dimensions of w1,b1 and logits associated with the output layer. Draw a picture for better illustration.
the code
import time import tensorflow as tf from tensorflow.contrib import rnn from tensorflow.examples.tutorials.mnist import input_data # Training Parameters training_steps = 5000 batch_size = 128 display_step = 200 # Network Parameters num_input = 28 # MNIST data input (img shape: 28*28) time_steps = 28 # time_steps num_hidden = 128 # hidden layer num of features num_classes = 10 # MNIST total classes (0-9 digits) # tf Graph input X = tf.placeholder(tf.float32, [None, time_steps, num_input],name='X') Y = tf.placeholder(tf.int32, [None, num_classes],name='Y') #variation 1: initializing W1 and b1 Wl = tf.Variable(tf.random_normal([num_hidden, num_classes])) bl = tf.Variable(tf.random_normal([num_classes])) #Wl = tf.Variable(tf.truncated_normal([num_hidden, num_classes], mean=0, stddev=.01)) #bl = tf.Variable(tf.truncated_normal([num_classes], mean=0, stddev=.01)) #variation 2: choosing RNN cell #cell = rnn.BasicRNNCell(num_hidden) cell=rnn.BasicLSTMCell(num_hidden, forget_bias=1.0) #vairation 3: choosing static/dynamic rnn X_T = tf.unstack(X, time_steps, 1) outputs, states = tf.nn.static_rnn(cell, X_T, dtype=tf.float32) logits=tf.matmul(outputs[-1], Wl) + bl #outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32) #last_rnn_output = outputs[:, -1, :] #logits=tf.matmul(last_rnn_output, Wl) + bl prediction = tf.nn.softmax(logits) loss_op = tf.losses.softmax_cross_entropy(logits=logits, onehot_labels=Y) #variation 4: choose optimizer #train_op = tf.train.GradientDescentOptimizer(0.001).minimize(loss_op) train_op=tf.train.RMSPropOptimizer(0.001, 0.9).minimize(loss_op) # Evaluate model correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) start_time = time.time() #initialization mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) sess=tf.Session() sess.run(tf.global_variables_initializer()) #training loop for step in range(1, training_steps+1): batch_x, batch_y = mnist.train.next_batch(batch_size) # Reshape data to get 28 seq of 28 elements batch_x = batch_x.reshape((batch_size, time_steps, num_input)) # Run optimization op (backprop) sess.run(train_op, feed_dict={X: batch_x, Y: batch_y}) if step % display_step == 0 or step == 1: # Calculate batch loss and accuracy loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x,Y: batch_y}) print("Step " + str(step) + ", Minibatch Loss= " + \ "{:.4f}".format(loss) + ", Training Accuracy= " + \ "{:.3f}".format(acc)) print("Optimization Finished!") print("--- %s seconds ---" % (time.time() - start_time)) #testing using a batch test_data = mnist.test.images[:batch_size].reshape(-1, time_steps, num_input) test_label = mnist.test.labels[:batch_size] test_acc=sess.run(accuracy, feed_dict={X: test_data, Y: test_label}) print("Testing Accuracy:", test_acc)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
