Training of RNN in TensorFlow

Last updated on Oct 24 2021
Ashutosh Wakiroo

Table of Contents

Training of RNN in TensorFlow

Recurrent neural networks are a type of deep learning-oriented algorithm, which follows a sequential approach. In neural networks, we assume that each input and output of all layers is independent. These types of neural networks are called recurrent because they sequentially perform mathematical computations.

tensorFlow 47
tensorFlow

The following steps to train a recurrent neural network:
Step 1- Input a specific example from the dataset.
Step 2- The network will take an example and compute some calculations using randomly initialized variables.
Step 3- A predicted result is then computed.
Step 4- The comparison of the actual results generated with the expected value will produce an error.
Step 5- It is propagated through the same path where the variable is also adjusted to trace the error.
Step 6- The levels from 1 to 5 are repeated until we are confident that the variables declared to get the output are appropriately defined.
Step 7- In the last step, a systematic prediction is made by applying these variables to get new unseen input.
The schematic approach of representing recurrent neural network is described below-

tensorFlow 48
tensorFlow

Recurrent Neural Network Implementation with TensorFlow

Complete code

1. from __future__ import print_function 
2. import tensorflow as tf 
3. from tensorflow.contrib import rnn 
4. 
5. # Importing MNIST data 
6. from tensorflow.examples.tutorials.mnist import input_data 
7. mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) 
8. ''' 
9. To classify images using a recurrent neural network, we consider every image 
10. row as a sequence of pixels. Because MNIST image shape is 28*28px, we will then 
11. handle 28 sequences of 28 steps for every sample. 
12. 
13. ''' 
14. # Training the Parameters 
15. learning_rate = 0.001 
16. training_steps = 10000 
17. batch_size = 128 
18. display_step = 200 
19. 
20. # Here the code describe the Network Parameters 
21. num_input = 28 # MNIST data input (img shape: 28*28) 
22. timesteps = 28 # timesteps 
23. num_hidden = 128 # hidden layer num of features 
24. num_classes = 10 # MNIST total classes (0-9 digits) 
25. 
26. # tf Graph input 
27. X = tf.placeholder("float", [None, timesteps, num_input]) 
28. Y = tf.placeholder("float", [None, num_classes]) 
29. 
30. # Defining weights 
31. weights = { 
32. 'out': tf.Variable(tf.random_normal([num_hidden, num_classes])) 
33. } 
34. biases = { 
35. 'out': tf.Variable(tf.random_normal([num_classes])) 
36. } 
37. def RNN(x, weights, biases): 
38. 
39. # Preparing data shape to match `rnn` function requirements 
40. # Current data input shape: 
41. (batch_size, timesteps, n_input) 
42. # Required shape: 
43. 'timesteps' tensors list of shape (batch_size, n_input) 
44. # Unstack to get a list of 'timesteps' tensors of shape (batch_size, n_input) 
45. x = tf.unstack(x, timesteps, 1) 
46. 
47. # Here we define a LSTM cell with tensorflow 
48. lstm_cell = rnn.BasicLSTMCell(num_hidden, forget_bias=1.0) 
49. 
50. # Get LSTM cell output 
51. outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32) 
52. 
53. # Linear activation, using RNN inner loop last output 
54. return tf.matmul(outputs[-1], weights['out']) + biases['out'] 
55. 
56. logits = RNN(X, weights, biases) 
57. prediction = tf.nn.softmax(logits) 
58. 
59. # Then here we define loss and optimizer 
60. loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits( 
61. logits=logits, labels=Y)) 
62. optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) 
63. train_op = optimizer.minimize(loss_op) 
64. 
65. # Evaluate model (with test logits, for dropout to be disabled) 
66. correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1)) 
67. accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) 
68. 
69. # Initializing the variables (i.e. assign their default value) 
70. init = tf.global_variables_initializer() 
71. 
72. # Start the training 
73. with tf.Session() as sess: 
74. 
75. # Run the initializer 
76. sess.run(init) 
77. 
78. for step in range(1, training_steps+1): 
79. batch_x, batch_y = mnist.train.next_batch(batch_size) 
80. # Reshape data to get 28 sequence of 28 elements 
81. batch_x = batch_x.reshape((batch_size, timesteps, num_input)) 
82. # Run optimization op (back prop) 
83. sess.run(train_op, feed_dict={X: batch_x, Y: batch_y}) 
84. if step % display_step == 0 or step == 1: 
85. # calculate batch loss and accuracy 
86. loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x, 
87. Y: batch_y}) 
88. print("Step " + str(step) + ", Minibatch Loss= " + \ 
89. "{:.4f}".format(loss) + ", Training Accuracy= " + \ 
90. "{:.3f}".format(acc)) 
91. print("Optimization Finished!") 
92. 
93. #, At last, calculate the accuracy for 128 mnist test images 
94. test_len = 128 
95. test_data = mnist.test.images[:test_len].reshape((-1, timesteps, num_input)) 
96. test_label = mnist.test.labels[:test_len] 
97. print("Testing Accuracy:", \ 
98. sess.run(accuracy, feed_dict={X: test_data, Y: test_label}))
Output:
Instructions for updating:
Future major versions of TensorFlow will allow gradients to flow
into the label's input on backprop by default.

See `tf.nn.softmax_cross_entropy_with_logits_v2`.

Step 1, Minibatch Loss= 2.6592, Training Accuracy= 0.148
Step 200, Minibatch Loss= 2.1379, Training Accuracy= 0.250
Step 400, Minibatch Loss= 1.8860, Training Accuracy= 0.445
Step 600, Minibatch Loss= 1.8542, Training Accuracy= 0.367
Step 800, Minibatch Loss= 1.7489, Training Accuracy= 0.477
Step 1000, Minibatch Loss= 1.6399, Training Accuracy= 0.492
Step 1200, Minibatch Loss= 1.4379, Training Accuracy= 0.570
Step 1400, Minibatch Loss= 1.4319, Training Accuracy= 0.500
Step 1600, Minibatch Loss= 1.3899, Training Accuracy= 0.547
Step 1800, Minibatch Loss= 1.3563, Training Accuracy= 0.570
Step 2000, Minibatch Loss= 1.2134, Training Accuracy= 0.617
Step 2200, Minibatch Loss= 1.2582, Training Accuracy= 0.609
Step 2400, Minibatch Loss= 1.2412, Training Accuracy= 0.578
Step 2600, Minibatch Loss= 1.1655, Training Accuracy= 0.625
Step 2800, Minibatch Loss= 1.0927, Training Accuracy= 0.656
Step 3000, Minibatch Loss= 1.2648, Training Accuracy= 0.617
Step 3200, Minibatch Loss= 0.9734, Training Accuracy= 0.695
Step 3400, Minibatch Loss= 0.8705, Training Accuracy= 0.773
Step 3600, Minibatch Loss= 1.0188, Training Accuracy= 0.680
Step 3800, Minibatch Loss= 0.8047, Training Accuracy= 0.719
Step 4000, Minibatch Loss= 0.8417, Training Accuracy= 0.758
Step 4200, Minibatch Loss= 0.8516, Training Accuracy= 0.703
Step 4400, Minibatch Loss= 0.8496, Training Accuracy= 0.773
Step 4600, Minibatch Loss= 0.9925, Training Accuracy= 0.719
Step 4800, Minibatch Loss= 0.6316, Training Accuracy= 0.812
Step 5000, Minibatch Loss= 0.7585, Training Accuracy= 0.750
Step 5200, Minibatch Loss= 0.6965, Training Accuracy= 0.797
Step 5400, Minibatch Loss= 0.7134, Training Accuracy= 0.836
Step 5600, Minibatch Loss= 0.6509, Training Accuracy= 0.812
Step 5800, Minibatch Loss= 0.7797, Training Accuracy= 0.750
Step 6000, Minibatch Loss= 0.6225, Training Accuracy= 0.859
Step 6200, Minibatch Loss= 0.6776, Training Accuracy= 0.781
Step 6400, Minibatch Loss= 0.6090, Training Accuracy= 0.781
Step 6600, Minibatch Loss= 0.5446, Training Accuracy= 0.836
Step 6800, Minibatch Loss= 0.6514, Training Accuracy= 0.750
Step 7000, Minibatch Loss= 0.7421, Training Accuracy= 0.758
Step 7200, Minibatch Loss= 0.5114, Training Accuracy= 0.844
Step 7400, Minibatch Loss= 0.5999, Training Accuracy= 0.844
Step 7600, Minibatch Loss= 0.5764, Training Accuracy= 0.789
Step 7800, Minibatch Loss= 0.6225, Training Accuracy= 0.805
Step 8000, Minibatch Loss= 0.4691, Training Accuracy= 0.875
Step 8200, Minibatch Loss= 0.4859, Training Accuracy= 0.852
Step 8400, Minibatch Loss= 0.5820, Training Accuracy= 0.828
Step 8600, Minibatch Loss= 0.4873, Training Accuracy= 0.883
Step 8800, Minibatch Loss= 0.5194, Training Accuracy= 0.828
Step 9000, Minibatch Loss= 0.6888, Training Accuracy= 0.820
Step 9200, Minibatch Loss= 0.6094, Training Accuracy= 0.812
Step 9400, Minibatch Loss= 0.5852, Training Accuracy= 0.852
Step 9600, Minibatch Loss= 0.4656, Training Accuracy= 0.844
Step 9800, Minibatch Loss= 0.4595, Training Accuracy= 0.875
Step 10000, Minibatch Loss= 0.4404, Training Accuracy= 0.883
Optimization Finished!
Testing Accuracy: 0.890625

So, this brings us to the end of blog. This Tecklearn ‘Training of RNN in Tensor Flow’ blog helps you with commonly asked questions if you are looking out for a job in Artificial Intelligence. If you wish to learn Artificial Intelligence and build a career in AI or Machine Learning domain, then check out our interactive, Artificial Intelligence and Deep Learning with TensorFlow Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/artificial-intelligence-and-deep-learning-with-tensorflow/

Artificial Intelligence and Deep Learning with TensorFlow Training

About the Course

Tecklearn’s Artificial Intelligence and Deep Learning with Tensor Flow course is curated by industry professionals as per the industry requirements & demands and aligned with the latest best practices. You’ll master convolutional neural networks (CNN), TensorFlow, TensorFlow code, transfer learning, graph visualization, recurrent neural networks (RNN), Deep Learning libraries, GPU in Deep Learning, Keras and TFLearn APIs, backpropagation, and hyperparameters via hands-on projects. The trainee will learn AI by mastering natural language processing, deep neural networks, predictive analytics, reinforcement learning, and more programming languages needed to shine in this field.

Why Should you take Artificial Intelligence and Deep Learning with Tensor Flow Training?

• According to Paysa.com, an Artificial Intelligence Engineer earns an average of $171,715, ranging from $124,542 at the 25th percentile to $201,853 at the 75th percentile, with top earners earning more than $257,530.
• Worldwide Spending on Artificial Intelligence Systems Will Be Nearly $98 Billion in 2023, According to New IDC Spending Guide at a GACR of 28.5%.
• IBM, Amazon, Apple, Google, Facebook, Microsoft, Oracle and almost all the leading companies are working on Artificial Intelligence to innovate future technologies.

What you will Learn in this Course?

Introduction to Deep Learning and AI
• What is Deep Learning?
• Advantage of Deep Learning over Machine learning
• Real-Life use cases of Deep Learning
• Review of Machine Learning: Regression, Classification, Clustering, Reinforcement Learning, Underfitting and Overfitting, Optimization
• Pre-requisites for AI & DL
• Python Programming Language
• Installation & IDE
Environment Set Up and Essentials
• Installation
• Python – NumPy
• Python for Data Science and AI
• Python Language Essentials
• Python Libraries – Numpy and Pandas
• Numpy for Mathematical Computing
More Prerequisites for Deep Learning and AI
• Pandas for Data Analysis
• Machine Learning Basic Concepts
• Normalization
• Data Set
• Machine Learning Concepts
• Regression
• Logistic Regression
• SVM – Support Vector Machines
• Decision Trees
• Python Libraries for Data Science and AI
Introduction to Neural Networks
• Creating Module
• Neural Network Equation
• Sigmoid Function
• Multi-layered perception
• Weights, Biases
• Activation Functions
• Gradient Decent or Error function
• Epoch, Forward & backword propagation
• What is TensorFlow?
• TensorFlow code-basics
• Graph Visualization
• Constants, Placeholders, Variables
Multi-layered Neural Networks
• Error Back propagation issues
• Drop outs
Regularization techniques in Deep Learning
Deep Learning Libraries
• Tensorflow
• Keras
• OpenCV
• SkImage
• PIL
Building of Simple Neural Network from Scratch from Simple Equation
• Training the model
Dual Equation Neural Network
• TensorFlow
• Predicting Algorithm
Introduction to Keras API
• Define Keras
• How to compose Models in Keras
• Sequential Composition
• Functional Composition
• Predefined Neural Network Layers
• What is Batch Normalization
• Saving and Loading a model with Keras
• Customizing the Training Process
• Using TensorBoard with Keras
• Use-Case Implementation with Keras
GPU in Deep Learning
• Introduction to GPUs and how they differ from CPUs
• Importance of GPUs in training Deep Learning Networks
• The GPU constituent with simpler core and concurrent hardware
• Keras Model Saving and Reusing
• Deploying Keras with TensorBoard
Keras Cat Vs Dog Modelling
• Activation Functions in Neural Network
Optimization Techniques
• Some Examples for Neural Network
Convolutional Neural Networks (CNN)
• Introduction to CNNs
• CNNs Application
• Architecture of a CNN
• Convolution and Pooling layers in a CNN
• Understanding and Visualizing a CNN
RNN: Recurrent Neural Networks
• Introduction to RNN Model
• Application use cases of RNN
• Modelling sequences
• Training RNNs with Backpropagation
• Long Short-Term memory (LSTM)
• Recursive Neural Tensor Network Theory
• Recurrent Neural Network Model
Application of Deep Learning in image recognition, NLP and more
Real world projects in recommender systems and others
Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "Training of RNN in TensorFlow"

Leave a Message

Your email address will not be published. Required fields are marked *