Tensorflow 简明教程
TensorFlow - Recurrent Neural Networks
递归神经网络是一种面向深度学习的算法,它遵循顺序方法。在神经网络中,我们总是假设每个输入和输出独立于所有其他层。这些类型的神经网络被称为递归网络,因为它们以顺序方式执行数学计算。
Recurrent neural networks is a type of deep learning-oriented algorithm, which follows a sequential approach. In neural networks, we always assume that each input and output is independent of all other layers. These type of neural networks are called recurrent because they perform mathematical computations in sequential manner.
考虑以下训练递归神经网络的步骤 −
Consider the following steps to train a recurrent neural network −
Step 1 − 从数据集中输入一个特定示例。
Step 1 − Input a specific example from dataset.
Step 2 − 网络将采用一个示例,并使用随机初始化的变量计算一些计算。
Step 2 − Network will take an example and compute some calculations using randomly initialized variables.
Step 3 − 然后计算预期的结果。
Step 3 − A predicted result is then computed.
Step 4 − 用产生的实际结果与期望值进行比较将产生一个误差。
Step 4 − The comparison of actual result generated with the expected value will produce an error.
Step 5 − 为了追踪误差,它通过变量也调整的相同路径传播。
Step 5 − To trace the error, it is propagated through same path where the variables are also adjusted.
Step 6 − 从 1 到 5 的步骤重复,直到我们确信宣告的变量以获得输出被正确定义。
Step 6 − The steps from 1 to 5 are repeated until we are confident that the variables declared to get the output are defined properly.
Step 7 − 通过应用这些变量来获得新的看不见的输入,做出一个系统的预测。
Step 7 − A systematic prediction is made by applying these variables to get new unseen input.
递归神经网络的示意图方法如下所述 −
The schematic approach of representing recurrent neural networks is described below −
Recurrent Neural Network Implementation with TensorFlow
在本节中,我们将学习如何用 TensorFlow 实现递归神经网络。
In this section, we will learn how to implement recurrent neural network with TensorFlow.
Step 1 − TensorFlow 包含用于实现递归神经网络模块的各种库。
Step 1 − TensorFlow includes various libraries for specific implementation of the recurrent neural network module.
#Import necessary modules
from __future__ import print_function
import tensorflow as tf
from tensorflow.contrib import rnn
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot = True)
如上所述,这些库有助于定义输入数据,这形成了递归神经网络实现的主要部分。
As mentioned above, the libraries help in defining the input data, which forms the primary part of recurrent neural network implementation.
Step 2 − 我们的主要动机是使用循环神经网络对图像进行分类,其中我们把每行图像看成像素序列。MNIST 的图像形状具体定义为 28*28 px。现在我们将处理前面提到的每个样本的 28 个 28 步骤的序列。我们将定义输入参数以便完成序列模式。
Step 2 − Our primary motive is to classify the images using a recurrent neural network, where we consider every image row as a sequence of pixels. MNIST image shape is specifically defined as 28*28 px. Now we will handle 28 sequences of 28 steps for each sample that is mentioned. We will define the input parameters to get the sequential pattern done.
n_input = 28 # MNIST data input with img shape 28*28
n_steps = 28
n_hidden = 128
n_classes = 10
# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes]
weights = {
'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))
}
biases = {
'out': tf.Variable(tf.random_normal([n_classes]))
}
Step 3 − 使用 RNN 中定义的函数计算结果以获得最佳结果。此处,将每个数据形状与当前输入形状进行比较,并对结果进行计算以保持准确率。
Step 3 − Compute the results using a defined function in RNN to get the best results. Here, each data shape is compared with current input shape and the results are computed to maintain the accuracy rate.
def RNN(x, weights, biases):
x = tf.unstack(x, n_steps, 1)
# Define a lstm cell with tensorflow
lstm_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
# Get lstm cell output
outputs, states = rnn.static_rnn(lstm_cell, x, dtype = tf.float32)
# Linear activation, using rnn inner loop last output
return tf.matmul(outputs[-1], weights['out']) + biases['out']
pred = RNN(x, weights, biases)
# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = pred, labels = y))
optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost)
# Evaluate model
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
# Initializing the variables
init = tf.global_variables_initializer()
Step 4 − 在此步骤中,我们将启动图表以获取计算结果。这也将帮助计算出测试结果的准确率。
Step 4 − In this step, we will launch the graph to get the computational results. This also helps in calculating the accuracy for test results.
with tf.Session() as sess:
sess.run(init)
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
batch_x, batch_y = mnist.train.next_batch(batch_size)
batch_x = batch_x.reshape((batch_size, n_steps, n_input))
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# Calculate batch accuracy
acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})
# Calculate batch loss
loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})
print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
"{:.6f}".format(loss) + ", Training Accuracy= " + \
"{:.5f}".format(acc))
step += 1
print("Optimization Finished!")
test_len = 128
test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input))
test_label = mnist.test.labels[:test_len]
print("Testing Accuracy:", \
sess.run(accuracy, feed_dict={x: test_data, y: test_label}))
下面的屏幕截图给出了输出结果 −
The screenshots below show the output generated −