9-1 subtraction
使用python编写简单循环神经网络,拟合一个退位减法的操作,观察其反向传播过程。
程序:
#1 定义基本函数
import copy, numpy as np
np.random.seed(0) # 固定随机数生成器的种子,可以每次得到一样的值
# compute sigmoid nonlinearity
def sigmoid(x): # 激活函数
output = 1 / (1 + np.exp(-x))
return output
# convert output of sigmoid function to its derivative
def sigmoid_output_to_derivative(output): # 激活函数的导数
return output * (1 - output)
#2 建立二进制映射
int2binary = {} # 整数到其二进制表示的映射
binary_dim = 8 # 暂时制作256以内的减法
## 计算0-256的二进制表示
largest_number = pow(2, binary_dim)
binary = np.unpackbits(
np.array([range(largest_number)], dtype=np.uint8).T, axis=1)
for i in range(largest_number):
int2binary[i] = binary[i]
#定义参数 input variables
alpha = 0.9 # 学习速率
input_dim = 2 # 输入的维度是2
hidden_dim = 16
output_dim = 1 # 输出维度为1
# initialize neural network weights
synapse_0 = (2 * np.random.random((input_dim, hidden_dim)) - 1) * 0.05 # 维度为2*16, 2是输入维度,16是隐藏层维度
synapse_1 = (2 * np.random.random((hidden_dim, output_dim)) - 1) * 0.05
synapse_h = (2 * np.random.random((hidden_dim, hidden_dim)) - 1) * 0.05
# => [-0.05, 0.05),
# 用于存放反向传播的权重更新值
synapse_0_update = np.zeros_like(synapse_0)
synapse_1_update = np.zeros_like(synapse_1)
synapse_h_update = np.zeros_like(synapse_h)
#4 准备数据样本
#开始训练 training
for j in range(10000):
# 生成一个数字a
a_int = np.random.randint(largest_number)
# 生成一个数字b,b的最大值取的是largest_number/2,作为被减数,让它小一点。
b_int = np.random.randint(largest_number / 2)
# 如果生成的b大了,那么交换一下
if a_int < b_int:
tt = a_int
b_int = a_int
a_int = tt
a = int2binary[a_int] # binary encoding
b = int2binary[b_int] # binary encoding
# true answer
c_int = a_int - b_int
c = int2binary[c_int]
#5 模型初始化
# 存储神经网络的预测值
d = np.zeros_like(c)
overallError = 0 # 每次把总误差清零
layer_2_deltas = list() # 存储每个时间点输出层的误差
layer_1_values = list() # 存储每个时间点隐藏层的值
layer_1_values.append(np.ones(hidden_dim) * 0.1) # 一开始没有隐藏层,所以初始化一下原始值为0.1
#6 正向传播
# moving along the positions in the binary encoding
for position in range(binary_dim): # 循环遍历每一个二进制位
# generate input and output
X = np.array([[a[binary_dim - position - 1], b[binary_dim - position - 1]]]) # 从右到左,每次去两个输入数字的一个bit位
y = np.array([[c[binary_dim - position - 1]]]).T # 正确答案
# hidden layer (input ~+ prev_hidden)
layer_1 = sigmoid(np.dot(X, synapse_0) + np.dot(layer_1_values[-1],
synapse_h)) # (输入层 + 之前的隐藏层) -> 新的隐藏层,这是体现循环神经网络的最核心的地方!!!
# output layer (new binary representation)
layer_2 = sigmoid(np.dot(layer_1, synapse_1)) # 隐藏层 * 隐藏层到输出层的转化矩阵synapse_1 -> 输出层
layer_2_error = y - layer_2 # 预测误差
layer_2_deltas.append((layer_2_error) * sigmoid_output_to_derivative(layer_2)) # 把每一个时间点的误差导数都记录下来
overallError += np.abs(layer_2_error[0]) # 总误差
d[binary_dim - position - 1] = np.round(layer_2[0][0]) # 记录下每一个预测bit位
# store hidden layer so we can use it in the next timestep
layer_1_values.append(copy.deepcopy(layer_1)) # 记录下隐藏层的值,在下一个时间点用
future_layer_1_delta = np.zeros(hidden_dim)#为反向传播准备的初始化
#7 反向训练
# 反向传播,从最后一个时间点到第一个时间点
for position in range(binary_dim):
X = np.array([[a[position], b[position]]]) # 最后一次的两个输入
layer_1 = layer_1_values[-position - 1] # 当前时间点的隐藏层
prev_layer_1 = layer_1_values[-position - 2] # 前一个时间点的隐藏层
# error at output layer
layer_2_delta = layer_2_deltas[-position - 1] # 当前时间点输出层导数
# error at hidden layer
# 通过后一个时间点(因为是反向传播)的隐藏层误差和当前时间点的输出层误差,计算当前时间点的隐藏层误差
layer_1_delta = (future_layer_1_delta.dot(synapse_h.T) + layer_2_delta.dot(
synapse_1.T)) * sigmoid_output_to_derivative(layer_1)
# 等到完成了所有反向传播误差计算, 才会更新权重矩阵,先暂时把更新矩阵存起来。
synapse_1_update += np.atleast_2d(layer_1).T.dot(layer_2_delta)
synapse_h_update += np.atleast_2d(prev_layer_1).T.dot(layer_1_delta)
synapse_0_update += X.T.dot(layer_1_delta)
future_layer_1_delta = layer_1_delta
# 完成所有反向传播之后,更新权重矩阵。并把矩阵变量清零
synapse_0 += synapse_0_update * alpha
synapse_1 += synapse_1_update * alpha
synapse_h += synapse_h_update * alpha
synapse_0_update *= 0
synapse_1_update *= 0
synapse_h_update *= 0
#8 输出结果
# print out progress
if (j % 800 == 0):#每运行800次将结果输出
# print(synapse_0,synapse_h,synapse_1)
print("总误差:" + str(overallError))
print("Pred:" + str(d))
print("True:" + str(c))
out = 0
for index, x in enumerate(reversed(d)):
out += x * pow(2, index)
print(str(a_int) + " - " + str(b_int) + " = " + str(out))
print("------------")
结果:
总误差:[3.97242498]
Pred:[0 0 0 0 0 0 0 0]
True:[0 0 0 0 0 0 0 0]
9 - 9 = 0
------------
总误差:[2.1721182]
Pred:[0 0 0 0 0 0 0 0]
True:[0 0 0 1 0 0 0 1]
17 - 0 = 0
------------
总误差:[1.1082385]
Pred:[0 0 0 0 0 0 0 0]
True:[0 0 0 0 0 0 0 0]
59 - 59 = 0
------------
总误差:[0.18727913]
Pred:[0 0 0 0 0 0 0 0]
True:[0 0 0 0 0 0 0 0]
19 - 19 = 0
------------
总误差:[0.21914293]
Pred:[0 0 0 0 0 0 0 0]
True:[0 0 0 0 0 0 0 0]
71 - 71 = 0
------------
总误差:[0.26861004]
Pred:[0 0 1 1 1 1 0 0]
True:[0 0 1 1 1 1 0 0]
71 - 11 = 60
------------
总误差:[0.11815367]
Pred:[1 0 0 0 0 0 0 0]
True:[1 0 0 0 0 0 0 0]
230 - 102 = 128
------------
总误差:[0.2927243]
Pred:[0 1 1 1 0 0 0 1]
True:[0 1 1 1 0 0 0 1]
160 - 47 = 113
------------
总误差:[0.04298749]
Pred:[0 0 0 0 0 0 0 0]
True:[0 0 0 0 0 0 0 0]
3 - 3 = 0
------------
总误差:[0.04243453]
Pred:[0 0 0 0 0 0 0 0]
True:[0 0 0 0 0 0 0 0]
17 - 17 = 0
------------
总误差:[0.04588656]
Pred:[1 0 0 1 0 1 1 0]
True:[1 0 0 1 0 1 1 0]
167 - 17 = 150
------------
总误差:[0.08098026]
Pred:[1 0 0 1 1 0 0 0]
True:[1 0 0 1 1 0 0 0]
204 - 52 = 152
------------
总误差:[0.03262333]
Pred:[1 1 0 0 0 0 0 0]
True:[1 1 0 0 0 0 0 0]
209 - 17 = 192
------------
9-2 echo模拟
构建一组序列,生成其对应的模拟回声序列。使用tensorflow创建一个简单循环神经网络拟合这个回声序列。
程序:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
#1 定义参数生成样本数据
num_epochs = 5
total_series_length = 50000
truncated_backprop_length = 15
state_size = 4
num_classes = 2
echo_step = 3
batch_size = 5
num_batches = total_series_length // batch_size // truncated_backprop_length
def generateData():
x = np.array(np.random.choice(2, total_series_length, p=[0.5, 0.5])) # 在0 和1 中选择total_series_length个数
y = np.roll(x, echo_step) # 向右循环移位【1111000】---【0001111】
y[0:echo_step] = 0
x = x.reshape((batch_size, -1)) # 5,10000
y = y.reshape((batch_size, -1))
return (x, y)
#2 定义占位符处理输入数据
batchX_placeholder = tf.placeholder(tf.float32, [batch_size, truncated_backprop_length])
batchY_placeholder = tf.placeholder(tf.int32, [batch_size, truncated_backprop_length])
init_state = tf.placeholder(tf.float32, [batch_size, state_size])
# Unpack columns
inputs_series = tf.unstack(batchX_placeholder, axis=1) # truncated_backprop_length个序列
labels_series = tf.unstack(batchY_placeholder, axis=1)
#3 定义网络结构
current_state = init_state
predictions_series = []
losses = []
#使用一个循环,按照序列逐个输入
for current_input, labels in zip(inputs_series, labels_series):
# for current_input in inputs_series:
current_input = tf.reshape(current_input, [batch_size, 1])
#加入初始状态
input_and_state_concatenated = tf.concat([current_input, current_state], 1) # current_state 4 +1
next_state = tf.contrib.layers.fully_connected(input_and_state_concatenated, state_size
, activation_fn=tf.tanh)
current_state = next_state
logits = tf.contrib.layers.fully_connected(next_state, num_classes, activation_fn=None)
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits)
losses.append(loss)
predictions = tf.nn.softmax(logits)
predictions_series.append(predictions)
total_loss = tf.reduce_mean(losses)
train_step = tf.train.AdagradOptimizer(0.3).minimize(total_loss)
def plot(loss_list, predictions_series, batchX, batchY):
plt.subplot(2, 3, 1)
plt.cla()
plt.plot(loss_list)
for batch_series_idx in range(batch_size):
one_hot_output_series = np.array(predictions_series)[:, batch_series_idx, :]
single_output_series = np.array([(1 if out[0] < 0.5 else 0) for out in one_hot_output_series])
plt.subplot(2, 3, batch_series_idx + 2)
plt.cla()
plt.axis([0, truncated_backprop_length, 0, 2])
left_offset = range(truncated_backprop_length)
left_offset2 = range(echo_step, truncated_backprop_length + echo_step)
label1 = "past values"
label2 = "True echo values"
label3 = "Predictions"
plt.plot(left_offset2, batchX[batch_series_idx, :] * 0.2 + 1.5, "o--b", label=label1)
plt.plot(left_offset, batchY[batch_series_idx, :] * 0.2 + 0.8, "x--b", label=label2)
plt.plot(left_offset, single_output_series * 0.2 + 0.1, "o--y", label=label3)
plt.legend(loc='best')
plt.draw()
plt.pause(0.0001)
#4 建立session训练数据
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
plt.ion()
plt.figure()
plt.show()
loss_list = []
for epoch_idx in range(num_epochs):
x, y = generateData()
_current_state = np.zeros((batch_size, state_size))
print("New data, epoch", epoch_idx)
for batch_idx in range(num_batches): # 50000/ 5 /15=分成多少段
start_idx = batch_idx * truncated_backprop_length
end_idx = start_idx + truncated_backprop_length
batchX = x[:, start_idx:end_idx]
batchY = y[:, start_idx:end_idx]
_total_loss, _train_step, _current_state, _predictions_series = sess.run(
[total_loss, train_step, current_state, predictions_series],
feed_dict={
batchX_placeholder: batchX,
batchY_placeholder: batchY,
init_state: _current_state
})
loss_list.append(_total_loss)
#5 测试模型及可视化
if batch_idx % 100 == 0:#每循环100次,打印数据并调用plot函数生成图像
print("Step", batch_idx, "Loss", _total_loss)
plot(loss_list, _predictions_series, batchX, batchY)
plt.ioff()
plt.show()
结果:
9-3 LSTMMnist
使用单层LSTM网络对MNIST数据集分类
程序:
import tensorflow as tf
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/shendu/MNIST_data/", one_hot=True)
n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10 # MNIST 列别 (0-9 ,一共10类)
tf.reset_default_graph()
# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
x1 = tf.unstack(x, n_steps, 1)
# 1 BasicLSTMCell
lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x1, dtype=tf.float32)
# 2 LSTMCell
# lstm_cell = tf.contrib.rnn.LSTMCell(n_hidden, forget_bias=1.0)
# outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x1, dtype=tf.float32)
# 3 gru
# gru = tf.contrib.rnn.GRUCell(n_hidden)
# outputs = tf.contrib.rnn.static_rnn(gru, x1, dtype=tf.float32)
# 4 创建动态RNN
# outputs,_ = tf.nn.dynamic_rnn(gru,x,dtype=tf.float32)
# outputs = tf.transpose(outputs, [1, 0, 2])
pred = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)
learning_rate = 0.001
training_iters = 100000
batch_size = 128
display_step = 10
# 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))
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
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, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算批次数据的准确率
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(" Finished!")
# 计算准确率 for 128 mnist test images
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}))
结果:
Extracting F:/shendu/MNIST_data/train-images-idx3-ubyte.gz
Extracting F:/shendu/MNIST_data/train-labels-idx1-ubyte.gz
Extracting F:/shendu/MNIST_data/t10k-images-idx3-ubyte.gz
Extracting F:/shendu/MNIST_data/t10k-labels-idx1-ubyte.gz
Iter 1280, Minibatch Loss= 2.098836, Training Accuracy= 0.41406
Iter 2560, Minibatch Loss= 1.738146, Training Accuracy= 0.40625
Iter 3840, Minibatch Loss= 1.479530, Training Accuracy= 0.47656
Iter 5120, Minibatch Loss= 1.133129, Training Accuracy= 0.65625
Iter 6400, Minibatch Loss= 0.978186, Training Accuracy= 0.70312
Iter 7680, Minibatch Loss= 0.916298, Training Accuracy= 0.70312
Iter 8960, Minibatch Loss= 0.671921, Training Accuracy= 0.74219
Iter 10240, Minibatch Loss= 0.625320, Training Accuracy= 0.83594
Iter 11520, Minibatch Loss= 0.569903, Training Accuracy= 0.80469
Iter 12800, Minibatch Loss= 0.555192, Training Accuracy= 0.86719
Iter 14080, Minibatch Loss= 0.502510, Training Accuracy= 0.82031
Iter 15360, Minibatch Loss= 0.499226, Training Accuracy= 0.83594
Iter 16640, Minibatch Loss= 0.550519, Training Accuracy= 0.80469
Iter 17920, Minibatch Loss= 0.453737, Training Accuracy= 0.84375
Iter 19200, Minibatch Loss= 0.455471, Training Accuracy= 0.85938
Iter 20480, Minibatch Loss= 0.294116, Training Accuracy= 0.90625
Iter 21760, Minibatch Loss= 0.348256, Training Accuracy= 0.90625
Iter 23040, Minibatch Loss= 0.339912, Training Accuracy= 0.88281
Iter 24320, Minibatch Loss= 0.427692, Training Accuracy= 0.85156
Iter 25600, Minibatch Loss= 0.325246, Training Accuracy= 0.89844
Iter 26880, Minibatch Loss= 0.299776, Training Accuracy= 0.90625
Iter 28160, Minibatch Loss= 0.266381, Training Accuracy= 0.92969
Iter 29440, Minibatch Loss= 0.293337, Training Accuracy= 0.91406
Iter 30720, Minibatch Loss= 0.451219, Training Accuracy= 0.87500
Iter 32000, Minibatch Loss= 0.301260, Training Accuracy= 0.92188
Iter 33280, Minibatch Loss= 0.207943, Training Accuracy= 0.95312
Iter 34560, Minibatch Loss= 0.289450, Training Accuracy= 0.89844
Iter 35840, Minibatch Loss= 0.222269, Training Accuracy= 0.93750
Iter 37120, Minibatch Loss= 0.244551, Training Accuracy= 0.92188
Iter 38400, Minibatch Loss= 0.327785, Training Accuracy= 0.90625
Iter 39680, Minibatch Loss= 0.225341, Training Accuracy= 0.92969
Iter 40960, Minibatch Loss= 0.150249, Training Accuracy= 0.96094
Iter 42240, Minibatch Loss= 0.334339, Training Accuracy= 0.90625
Iter 43520, Minibatch Loss= 0.281361, Training Accuracy= 0.92188
Iter 44800, Minibatch Loss= 0.291309, Training Accuracy= 0.89844
Iter 46080, Minibatch Loss= 0.212648, Training Accuracy= 0.92188
Iter 47360, Minibatch Loss= 0.165543, Training Accuracy= 0.96094
Iter 48640, Minibatch Loss= 0.186747, Training Accuracy= 0.95312
Iter 49920, Minibatch Loss= 0.214090, Training Accuracy= 0.93750
Iter 51200, Minibatch Loss= 0.170507, Training Accuracy= 0.95312
Iter 52480, Minibatch Loss= 0.237106, Training Accuracy= 0.91406
Iter 53760, Minibatch Loss= 0.237691, Training Accuracy= 0.92969
Iter 55040, Minibatch Loss= 0.227381, Training Accuracy= 0.94531
Iter 56320, Minibatch Loss= 0.134393, Training Accuracy= 0.96875
Iter 57600, Minibatch Loss= 0.119095, Training Accuracy= 0.97656
Iter 58880, Minibatch Loss= 0.117926, Training Accuracy= 0.97656
Iter 60160, Minibatch Loss= 0.110768, Training Accuracy= 0.96094
Iter 61440, Minibatch Loss= 0.146466, Training Accuracy= 0.96094
Iter 62720, Minibatch Loss= 0.170301, Training Accuracy= 0.95312
Iter 64000, Minibatch Loss= 0.150897, Training Accuracy= 0.93750
Iter 65280, Minibatch Loss= 0.154738, Training Accuracy= 0.95312
Iter 66560, Minibatch Loss= 0.143189, Training Accuracy= 0.96094
Iter 67840, Minibatch Loss= 0.184678, Training Accuracy= 0.95312
Iter 69120, Minibatch Loss= 0.236636, Training Accuracy= 0.92188
Iter 70400, Minibatch Loss= 0.158732, Training Accuracy= 0.93750
Iter 71680, Minibatch Loss= 0.219238, Training Accuracy= 0.94531
Iter 72960, Minibatch Loss= 0.139953, Training Accuracy= 0.96094
Iter 74240, Minibatch Loss= 0.075852, Training Accuracy= 0.96875
Iter 75520, Minibatch Loss= 0.132170, Training Accuracy= 0.96094
Iter 76800, Minibatch Loss= 0.136884, Training Accuracy= 0.97656
Iter 78080, Minibatch Loss= 0.188814, Training Accuracy= 0.94531
Iter 79360, Minibatch Loss= 0.131114, Training Accuracy= 0.96094
Iter 80640, Minibatch Loss= 0.167499, Training Accuracy= 0.96094
Iter 81920, Minibatch Loss= 0.104455, Training Accuracy= 0.97656
Iter 83200, Minibatch Loss= 0.153708, Training Accuracy= 0.95312
Iter 84480, Minibatch Loss= 0.233348, Training Accuracy= 0.94531
Iter 85760, Minibatch Loss= 0.163501, Training Accuracy= 0.95312
Iter 87040, Minibatch Loss= 0.061799, Training Accuracy= 0.99219
Iter 88320, Minibatch Loss= 0.112535, Training Accuracy= 0.98438
Iter 89600, Minibatch Loss= 0.132300, Training Accuracy= 0.95312
Iter 90880, Minibatch Loss= 0.099122, Training Accuracy= 0.95312
Iter 92160, Minibatch Loss= 0.077411, Training Accuracy= 0.97656
Iter 93440, Minibatch Loss= 0.217517, Training Accuracy= 0.95312
Iter 94720, Minibatch Loss= 0.150338, Training Accuracy= 0.95312
Iter 96000, Minibatch Loss= 0.211564, Training Accuracy= 0.94531
Iter 97280, Minibatch Loss= 0.098214, Training Accuracy= 0.98438
Iter 98560, Minibatch Loss= 0.105337, Training Accuracy= 0.96094
Iter 99840, Minibatch Loss= 0.053365, Training Accuracy= 1.00000
Finished!
Testing Accuracy: 0.9765625
9-4 LSTMCell
程序:
import tensorflow as tf
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/shendu/MNIST_data/", one_hot=True)
n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10 # MNIST 列别 (0-9 ,一共10类)
tf.reset_default_graph()
# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
x1 = tf.unstack(x, n_steps, 1)
# 2 LSTMCell
lstm_cell = tf.contrib.rnn.LSTMCell(n_hidden, forget_bias=1.0)
outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x1, dtype=tf.float32)
# 3 gru
# gru = tf.contrib.rnn.GRUCell(n_hidden)
# outputs = tf.contrib.rnn.static_rnn(gru, x1, dtype=tf.float32)
# 4 创建动态RNN
# outputs,_ = tf.nn.dynamic_rnn(gru,x,dtype=tf.float32)
# outputs = tf.transpose(outputs, [1, 0, 2])
pred = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)
learning_rate = 0.001
training_iters = 100000
batch_size = 128
display_step = 10
# 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))
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
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, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算批次数据的准确率
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(" Finished!")
# 计算准确率 for 128 mnist test images
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}))
结果:
Extracting F:/shendu/MNIST_data/train-images-idx3-ubyte.gz
Extracting F:/shendu/MNIST_data/train-labels-idx1-ubyte.gz
Extracting F:/shendu/MNIST_data/t10k-images-idx3-ubyte.gz
Extracting F:/shendu/MNIST_data/t10k-labels-idx1-ubyte.gz
Iter 1280, Minibatch Loss= 2.154248, Training Accuracy= 0.19531
Iter 2560, Minibatch Loss= 1.885561, Training Accuracy= 0.32031
Iter 3840, Minibatch Loss= 1.555503, Training Accuracy= 0.42969
Iter 5120, Minibatch Loss= 1.347491, Training Accuracy= 0.55469
Iter 6400, Minibatch Loss= 1.020022, Training Accuracy= 0.64844
Iter 7680, Minibatch Loss= 0.850060, Training Accuracy= 0.67188
Iter 8960, Minibatch Loss= 0.808957, Training Accuracy= 0.76562
Iter 10240, Minibatch Loss= 0.824437, Training Accuracy= 0.76562
Iter 11520, Minibatch Loss= 0.778792, Training Accuracy= 0.72656
Iter 12800, Minibatch Loss= 0.569078, Training Accuracy= 0.82031
Iter 14080, Minibatch Loss= 0.585458, Training Accuracy= 0.83594
Iter 15360, Minibatch Loss= 0.513995, Training Accuracy= 0.84375
Iter 16640, Minibatch Loss= 0.517743, Training Accuracy= 0.82031
Iter 17920, Minibatch Loss= 0.407907, Training Accuracy= 0.88281
Iter 19200, Minibatch Loss= 0.527955, Training Accuracy= 0.85938
Iter 20480, Minibatch Loss= 0.325411, Training Accuracy= 0.91406
Iter 21760, Minibatch Loss= 0.342428, Training Accuracy= 0.89844
Iter 23040, Minibatch Loss= 0.254995, Training Accuracy= 0.91406
Iter 24320, Minibatch Loss= 0.294678, Training Accuracy= 0.91406
Iter 25600, Minibatch Loss= 0.326912, Training Accuracy= 0.90625
Iter 26880, Minibatch Loss= 0.243427, Training Accuracy= 0.92188
Iter 28160, Minibatch Loss= 0.324615, Training Accuracy= 0.90625
Iter 29440, Minibatch Loss= 0.329762, Training Accuracy= 0.88281
Iter 30720, Minibatch Loss= 0.228643, Training Accuracy= 0.93750
Iter 32000, Minibatch Loss= 0.230374, Training Accuracy= 0.91406
Iter 33280, Minibatch Loss= 0.236052, Training Accuracy= 0.91406
Iter 34560, Minibatch Loss= 0.280240, Training Accuracy= 0.92188
Iter 35840, Minibatch Loss= 0.296831, Training Accuracy= 0.91406
Iter 37120, Minibatch Loss= 0.236269, Training Accuracy= 0.94531
Iter 38400, Minibatch Loss= 0.219775, Training Accuracy= 0.93750
Iter 39680, Minibatch Loss= 0.273158, Training Accuracy= 0.92969
Iter 40960, Minibatch Loss= 0.246300, Training Accuracy= 0.92969
Iter 42240, Minibatch Loss= 0.257209, Training Accuracy= 0.91406
Iter 43520, Minibatch Loss= 0.173369, Training Accuracy= 0.93750
Iter 44800, Minibatch Loss= 0.166667, Training Accuracy= 0.96094
Iter 46080, Minibatch Loss= 0.312637, Training Accuracy= 0.90625
Iter 47360, Minibatch Loss= 0.223157, Training Accuracy= 0.92969
Iter 48640, Minibatch Loss= 0.325131, Training Accuracy= 0.92188
Iter 49920, Minibatch Loss= 0.145126, Training Accuracy= 0.95312
Iter 51200, Minibatch Loss= 0.129528, Training Accuracy= 0.96094
Iter 52480, Minibatch Loss= 0.136491, Training Accuracy= 0.95312
Iter 53760, Minibatch Loss= 0.178241, Training Accuracy= 0.92188
Iter 55040, Minibatch Loss= 0.153655, Training Accuracy= 0.95312
Iter 56320, Minibatch Loss= 0.110971, Training Accuracy= 0.96875
Iter 57600, Minibatch Loss= 0.170083, Training Accuracy= 0.96875
Iter 58880, Minibatch Loss= 0.123517, Training Accuracy= 0.96875
Iter 60160, Minibatch Loss= 0.078149, Training Accuracy= 0.97656
Iter 61440, Minibatch Loss= 0.112326, Training Accuracy= 0.93750
Iter 62720, Minibatch Loss= 0.099307, Training Accuracy= 0.96094
Iter 64000, Minibatch Loss= 0.119217, Training Accuracy= 0.96875
Iter 65280, Minibatch Loss= 0.115908, Training Accuracy= 0.95312
Iter 66560, Minibatch Loss= 0.179224, Training Accuracy= 0.95312
Iter 67840, Minibatch Loss= 0.081431, Training Accuracy= 0.98438
Iter 69120, Minibatch Loss= 0.079667, Training Accuracy= 0.97656
Iter 70400, Minibatch Loss= 0.186224, Training Accuracy= 0.94531
Iter 71680, Minibatch Loss= 0.155192, Training Accuracy= 0.94531
Iter 72960, Minibatch Loss= 0.175684, Training Accuracy= 0.94531
Iter 74240, Minibatch Loss= 0.087586, Training Accuracy= 0.98438
Iter 75520, Minibatch Loss= 0.204513, Training Accuracy= 0.92188
Iter 76800, Minibatch Loss= 0.254821, Training Accuracy= 0.92969
Iter 78080, Minibatch Loss= 0.101602, Training Accuracy= 0.97656
Iter 79360, Minibatch Loss= 0.206491, Training Accuracy= 0.92969
Iter 80640, Minibatch Loss= 0.174672, Training Accuracy= 0.96094
Iter 81920, Minibatch Loss= 0.173631, Training Accuracy= 0.93750
Iter 83200, Minibatch Loss= 0.111860, Training Accuracy= 0.97656
Iter 84480, Minibatch Loss= 0.124209, Training Accuracy= 0.96094
Iter 85760, Minibatch Loss= 0.118957, Training Accuracy= 0.96094
Iter 87040, Minibatch Loss= 0.145324, Training Accuracy= 0.96094
Iter 88320, Minibatch Loss= 0.102478, Training Accuracy= 0.96875
Iter 89600, Minibatch Loss= 0.097595, Training Accuracy= 0.97656
Iter 90880, Minibatch Loss= 0.131613, Training Accuracy= 0.96875
Iter 92160, Minibatch Loss= 0.114286, Training Accuracy= 0.98438
Iter 93440, Minibatch Loss= 0.131136, Training Accuracy= 0.95312
Iter 94720, Minibatch Loss= 0.151210, Training Accuracy= 0.95312
Iter 96000, Minibatch Loss= 0.052597, Training Accuracy= 0.99219
Iter 97280, Minibatch Loss= 0.215274, Training Accuracy= 0.92969
Iter 98560, Minibatch Loss= 0.074140, Training Accuracy= 0.97656
Iter 99840, Minibatch Loss= 0.049567, Training Accuracy= 1.00000
Finished!
Testing Accuracy: 0.984375
9-5 gru
程序:
import tensorflow as tf
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/shendu/MNIST_data/", one_hot=True)
n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10 # MNIST 列别 (0-9 ,一共10类)
tf.reset_default_graph()
# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
x1 = tf.unstack(x, n_steps, 1)
# 3 gru
gru = tf.contrib.rnn.GRUCell(n_hidden)
outputs = tf.contrib.rnn.static_rnn(gru, x1, dtype=tf.float32)
# 4 创建动态RNN
# outputs,_ = tf.nn.dynamic_rnn(gru,x,dtype=tf.float32)
# outputs = tf.transpose(outputs, [1, 0, 2])
pred = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)
learning_rate = 0.001
training_iters = 100000
batch_size = 128
display_step = 10
# 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))
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
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, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算批次数据的准确率
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(" Finished!")
# 计算准确率 for 128 mnist test images
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}))
结果:
Extracting F:/shendu/MNIST_data/train-images-idx3-ubyte.gz
Extracting F:/shendu/MNIST_data/train-labels-idx1-ubyte.gz
Extracting F:/shendu/MNIST_data/t10k-images-idx3-ubyte.gz
Extracting F:/shendu/MNIST_data/t10k-labels-idx1-ubyte.gz
Iter 1280, Minibatch Loss= 2.121243, Training Accuracy= 0.31250
Iter 2560, Minibatch Loss= 1.672723, Training Accuracy= 0.47656
Iter 3840, Minibatch Loss= 1.437920, Training Accuracy= 0.56250
Iter 5120, Minibatch Loss= 1.207757, Training Accuracy= 0.60938
Iter 6400, Minibatch Loss= 0.989345, Training Accuracy= 0.71875
Iter 7680, Minibatch Loss= 0.852286, Training Accuracy= 0.71875
Iter 8960, Minibatch Loss= 0.905224, Training Accuracy= 0.71875
Iter 10240, Minibatch Loss= 0.583478, Training Accuracy= 0.78906
Iter 11520, Minibatch Loss= 0.570538, Training Accuracy= 0.84375
Iter 12800, Minibatch Loss= 0.615531, Training Accuracy= 0.79688
Iter 14080, Minibatch Loss= 0.558513, Training Accuracy= 0.82031
Iter 15360, Minibatch Loss= 0.448084, Training Accuracy= 0.86719
Iter 16640, Minibatch Loss= 0.462735, Training Accuracy= 0.84375
Iter 17920, Minibatch Loss= 0.380609, Training Accuracy= 0.86719
Iter 19200, Minibatch Loss= 0.336009, Training Accuracy= 0.92188
Iter 20480, Minibatch Loss= 0.318660, Training Accuracy= 0.90625
Iter 21760, Minibatch Loss= 0.492344, Training Accuracy= 0.86719
Iter 23040, Minibatch Loss= 0.252908, Training Accuracy= 0.91406
Iter 24320, Minibatch Loss= 0.308526, Training Accuracy= 0.89062
Iter 25600, Minibatch Loss= 0.260227, Training Accuracy= 0.91406
Iter 26880, Minibatch Loss= 0.339938, Training Accuracy= 0.91406
Iter 28160, Minibatch Loss= 0.175708, Training Accuracy= 0.96094
Iter 29440, Minibatch Loss= 0.322279, Training Accuracy= 0.90625
Iter 30720, Minibatch Loss= 0.225579, Training Accuracy= 0.93750
Iter 32000, Minibatch Loss= 0.236572, Training Accuracy= 0.92969
Iter 33280, Minibatch Loss= 0.315312, Training Accuracy= 0.89844
Iter 34560, Minibatch Loss= 0.225239, Training Accuracy= 0.91406
Iter 35840, Minibatch Loss= 0.234438, Training Accuracy= 0.92969
Iter 37120, Minibatch Loss= 0.189988, Training Accuracy= 0.95312
Iter 38400, Minibatch Loss= 0.208012, Training Accuracy= 0.93750
Iter 39680, Minibatch Loss= 0.179319, Training Accuracy= 0.92969
Iter 40960, Minibatch Loss= 0.200202, Training Accuracy= 0.92969
Iter 42240, Minibatch Loss= 0.188499, Training Accuracy= 0.94531
Iter 43520, Minibatch Loss= 0.179329, Training Accuracy= 0.94531
Iter 44800, Minibatch Loss= 0.067407, Training Accuracy= 0.99219
Iter 46080, Minibatch Loss= 0.202994, Training Accuracy= 0.93750
Iter 47360, Minibatch Loss= 0.141127, Training Accuracy= 0.96094
Iter 48640, Minibatch Loss= 0.090299, Training Accuracy= 0.97656
Iter 49920, Minibatch Loss= 0.125628, Training Accuracy= 0.96094
Iter 51200, Minibatch Loss= 0.121807, Training Accuracy= 0.96094
Iter 52480, Minibatch Loss= 0.263896, Training Accuracy= 0.91406
Iter 53760, Minibatch Loss= 0.069584, Training Accuracy= 0.99219
Iter 55040, Minibatch Loss= 0.187888, Training Accuracy= 0.92969
Iter 56320, Minibatch Loss= 0.144109, Training Accuracy= 0.95312
Iter 57600, Minibatch Loss= 0.236046, Training Accuracy= 0.94531
Iter 58880, Minibatch Loss= 0.121935, Training Accuracy= 0.96094
Iter 60160, Minibatch Loss= 0.111401, Training Accuracy= 0.98438
Iter 61440, Minibatch Loss= 0.216807, Training Accuracy= 0.92969
Iter 62720, Minibatch Loss= 0.135810, Training Accuracy= 0.96094
Iter 64000, Minibatch Loss= 0.124714, Training Accuracy= 0.96875
Iter 65280, Minibatch Loss= 0.184132, Training Accuracy= 0.95312
Iter 66560, Minibatch Loss= 0.170255, Training Accuracy= 0.93750
Iter 67840, Minibatch Loss= 0.050932, Training Accuracy= 0.98438
Iter 69120, Minibatch Loss= 0.172704, Training Accuracy= 0.95312
Iter 70400, Minibatch Loss= 0.155783, Training Accuracy= 0.96875
Iter 71680, Minibatch Loss= 0.079348, Training Accuracy= 0.96875
Iter 72960, Minibatch Loss= 0.125507, Training Accuracy= 0.95312
Iter 74240, Minibatch Loss= 0.102363, Training Accuracy= 0.97656
Iter 75520, Minibatch Loss= 0.129889, Training Accuracy= 0.97656
Iter 76800, Minibatch Loss= 0.143853, Training Accuracy= 0.96094
Iter 78080, Minibatch Loss= 0.120432, Training Accuracy= 0.96875
Iter 79360, Minibatch Loss= 0.136715, Training Accuracy= 0.96094
Iter 80640, Minibatch Loss= 0.187569, Training Accuracy= 0.95312
Iter 81920, Minibatch Loss= 0.184631, Training Accuracy= 0.94531
Iter 83200, Minibatch Loss= 0.174440, Training Accuracy= 0.93750
Iter 84480, Minibatch Loss= 0.144966, Training Accuracy= 0.93750
Iter 85760, Minibatch Loss= 0.170742, Training Accuracy= 0.95312
Iter 87040, Minibatch Loss= 0.171544, Training Accuracy= 0.94531
Iter 88320, Minibatch Loss= 0.154567, Training Accuracy= 0.94531
Iter 89600, Minibatch Loss= 0.053761, Training Accuracy= 0.99219
Iter 90880, Minibatch Loss= 0.152882, Training Accuracy= 0.96094
Iter 92160, Minibatch Loss= 0.143918, Training Accuracy= 0.94531
Iter 93440, Minibatch Loss= 0.130506, Training Accuracy= 0.96094
Iter 94720, Minibatch Loss= 0.119805, Training Accuracy= 0.96094
Iter 96000, Minibatch Loss= 0.067229, Training Accuracy= 0.98438
Iter 97280, Minibatch Loss= 0.118083, Training Accuracy= 0.96094
Iter 98560, Minibatch Loss= 0.108051, Training Accuracy= 0.97656
Iter 99840, Minibatch Loss= 0.134662, Training Accuracy= 0.95312
Finished!
Testing Accuracy: 0.9609375
9-6 创建动态RNN
使用单层动态RNN网络对MNIST数据集分类
程序:
import tensorflow as tf
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/shendu/MNIST_data/", one_hot=True)
n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10 # MNIST 列别 (0-9 ,一共10类)
tf.reset_default_graph()
# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
x1 = tf.unstack(x, n_steps, 1)
# 3 gru
gru = tf.contrib.rnn.GRUCell(n_hidden)
# outputs = tf.contrib.rnn.static_rnn(gru, x1, dtype=tf.float32)
# 4 创建动态RNN
outputs, _ = tf.nn.dynamic_rnn(gru, x, dtype=tf.float32)
outputs = tf.transpose(outputs, [1, 0, 2])
pred = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)
learning_rate = 0.001
training_iters = 100000
batch_size = 128
display_step = 10
# 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))
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
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, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算批次数据的准确率
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(" Finished!")
# 计算准确率 for 128 mnist test images
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}))
结果:
Extracting F:/shendu/MNIST_data/train-images-idx3-ubyte.gz
Extracting F:/shendu/MNIST_data/train-labels-idx1-ubyte.gz
Extracting F:/shendu/MNIST_data/t10k-images-idx3-ubyte.gz
Extracting F:/shendu/MNIST_data/t10k-labels-idx1-ubyte.gz
Iter 1280, Minibatch Loss= 2.096751, Training Accuracy= 0.35938
Iter 2560, Minibatch Loss= 1.861276, Training Accuracy= 0.32031
Iter 3840, Minibatch Loss= 1.530022, Training Accuracy= 0.56250
Iter 5120, Minibatch Loss= 1.455643, Training Accuracy= 0.48438
Iter 6400, Minibatch Loss= 1.091004, Training Accuracy= 0.66406
Iter 7680, Minibatch Loss= 0.974519, Training Accuracy= 0.68750
Iter 8960, Minibatch Loss= 0.836227, Training Accuracy= 0.68750
Iter 10240, Minibatch Loss= 0.756247, Training Accuracy= 0.72656
Iter 11520, Minibatch Loss= 0.742727, Training Accuracy= 0.73438
Iter 12800, Minibatch Loss= 0.686739, Training Accuracy= 0.80469
Iter 14080, Minibatch Loss= 0.404348, Training Accuracy= 0.87500
Iter 15360, Minibatch Loss= 0.487385, Training Accuracy= 0.85156
Iter 16640, Minibatch Loss= 0.477118, Training Accuracy= 0.84375
Iter 17920, Minibatch Loss= 0.519093, Training Accuracy= 0.83594
Iter 19200, Minibatch Loss= 0.423369, Training Accuracy= 0.88281
Iter 20480, Minibatch Loss= 0.397531, Training Accuracy= 0.85938
Iter 21760, Minibatch Loss= 0.398649, Training Accuracy= 0.87500
Iter 23040, Minibatch Loss= 0.377197, Training Accuracy= 0.87500
Iter 24320, Minibatch Loss= 0.366344, Training Accuracy= 0.89844
Iter 25600, Minibatch Loss= 0.475577, Training Accuracy= 0.84375
Iter 26880, Minibatch Loss= 0.168300, Training Accuracy= 0.94531
Iter 28160, Minibatch Loss= 0.181610, Training Accuracy= 0.96094
Iter 29440, Minibatch Loss= 0.365425, Training Accuracy= 0.92969
Iter 30720, Minibatch Loss= 0.287737, Training Accuracy= 0.92188
Iter 32000, Minibatch Loss= 0.261110, Training Accuracy= 0.89844
Iter 33280, Minibatch Loss= 0.346259, Training Accuracy= 0.87500
Iter 34560, Minibatch Loss= 0.185590, Training Accuracy= 0.93750
Iter 35840, Minibatch Loss= 0.212532, Training Accuracy= 0.92188
Iter 37120, Minibatch Loss= 0.185118, Training Accuracy= 0.93750
Iter 38400, Minibatch Loss= 0.128478, Training Accuracy= 0.96875
Iter 39680, Minibatch Loss= 0.219710, Training Accuracy= 0.92188
Iter 40960, Minibatch Loss= 0.276444, Training Accuracy= 0.92188
Iter 42240, Minibatch Loss= 0.151335, Training Accuracy= 0.96875
Iter 43520, Minibatch Loss= 0.408926, Training Accuracy= 0.90625
Iter 44800, Minibatch Loss= 0.268802, Training Accuracy= 0.91406
Iter 46080, Minibatch Loss= 0.124335, Training Accuracy= 0.97656
Iter 47360, Minibatch Loss= 0.147359, Training Accuracy= 0.93750
Iter 48640, Minibatch Loss= 0.108923, Training Accuracy= 0.97656
Iter 49920, Minibatch Loss= 0.206078, Training Accuracy= 0.95312
Iter 51200, Minibatch Loss= 0.393352, Training Accuracy= 0.90625
Iter 52480, Minibatch Loss= 0.124124, Training Accuracy= 0.94531
Iter 53760, Minibatch Loss= 0.214818, Training Accuracy= 0.95312
Iter 55040, Minibatch Loss= 0.176891, Training Accuracy= 0.96094
Iter 56320, Minibatch Loss= 0.217090, Training Accuracy= 0.93750
Iter 57600, Minibatch Loss= 0.193886, Training Accuracy= 0.92969
Iter 58880, Minibatch Loss= 0.114970, Training Accuracy= 0.96875
Iter 60160, Minibatch Loss= 0.189792, Training Accuracy= 0.96094
Iter 61440, Minibatch Loss= 0.161060, Training Accuracy= 0.95312
Iter 62720, Minibatch Loss= 0.167060, Training Accuracy= 0.95312
Iter 64000, Minibatch Loss= 0.093352, Training Accuracy= 0.96875
Iter 65280, Minibatch Loss= 0.172693, Training Accuracy= 0.94531
Iter 66560, Minibatch Loss= 0.264184, Training Accuracy= 0.90625
Iter 67840, Minibatch Loss= 0.136439, Training Accuracy= 0.95312
Iter 69120, Minibatch Loss= 0.148011, Training Accuracy= 0.93750
Iter 70400, Minibatch Loss= 0.077776, Training Accuracy= 0.98438
Iter 71680, Minibatch Loss= 0.130682, Training Accuracy= 0.96875
Iter 72960, Minibatch Loss= 0.128137, Training Accuracy= 0.95312
Iter 74240, Minibatch Loss= 0.105223, Training Accuracy= 0.96094
Iter 75520, Minibatch Loss= 0.073009, Training Accuracy= 0.98438
Iter 76800, Minibatch Loss= 0.166195, Training Accuracy= 0.94531
Iter 78080, Minibatch Loss= 0.055242, Training Accuracy= 0.98438
Iter 79360, Minibatch Loss= 0.153105, Training Accuracy= 0.95312
Iter 80640, Minibatch Loss= 0.086605, Training Accuracy= 0.97656
Iter 81920, Minibatch Loss= 0.199217, Training Accuracy= 0.95312
Iter 83200, Minibatch Loss= 0.133533, Training Accuracy= 0.95312
Iter 84480, Minibatch Loss= 0.114999, Training Accuracy= 0.96875
Iter 85760, Minibatch Loss= 0.123663, Training Accuracy= 0.96875
Iter 87040, Minibatch Loss= 0.144682, Training Accuracy= 0.96094
Iter 88320, Minibatch Loss= 0.085445, Training Accuracy= 0.96875
Iter 89600, Minibatch Loss= 0.143874, Training Accuracy= 0.96094
Iter 90880, Minibatch Loss= 0.077745, Training Accuracy= 0.97656
Iter 92160, Minibatch Loss= 0.079885, Training Accuracy= 0.98438
Iter 93440, Minibatch Loss= 0.107086, Training Accuracy= 0.96875
Iter 94720, Minibatch Loss= 0.133987, Training Accuracy= 0.95312
Iter 96000, Minibatch Loss= 0.061017, Training Accuracy= 0.96875
Iter 97280, Minibatch Loss= 0.181021, Training Accuracy= 0.95312
Iter 98560, Minibatch Loss= 0.106145, Training Accuracy= 0.95312
Iter 99840, Minibatch Loss= 0.062076, Training Accuracy= 0.98438
Finished!
Testing Accuracy: 0.9921875
9-7 McellMNIST
使用静态多层LSTM网络对MNIST数据集分类
程序;
import numpy as np
import tensorflow as tf
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/shendu/MNIST_data/", one_hot=True)
n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10 # MNIST 列别 (0-9 ,一共10类)
batch_size = 128
tf.reset_default_graph()
'''------------------------------------------------------------------------'''
# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
stacked_rnn = []
for i in range(3):
stacked_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
mcell = tf.contrib.rnn.MultiRNNCell(stacked_rnn)
x1 = tf.unstack(x, n_steps, 1)
outputs, states = tf.contrib.rnn.static_rnn(mcell, x1, dtype=tf.float32)
pred = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)
learning_rate = 0.001
# 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))
training_iters = 100000
display_step = 10
'''-------------------------------------------------------------------------'''
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
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, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算批次数据的准确率
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(" Finished!")
# 计算准确率 for 128 mnist test images
test_len = 100
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}))
结果:
Extracting F:/shendu/MNIST_data/train-images-idx3-ubyte.gz
Extracting F:/shendu/MNIST_data/train-labels-idx1-ubyte.gz
Extracting F:/shendu/MNIST_data/t10k-images-idx3-ubyte.gz
Extracting F:/shendu/MNIST_data/t10k-labels-idx1-ubyte.gz
Iter 1280, Minibatch Loss= 2.024895, Training Accuracy= 0.30469
Iter 2560, Minibatch Loss= 1.486717, Training Accuracy= 0.50000
Iter 3840, Minibatch Loss= 1.161208, Training Accuracy= 0.63281
Iter 5120, Minibatch Loss= 0.889335, Training Accuracy= 0.71094
Iter 6400, Minibatch Loss= 0.887313, Training Accuracy= 0.69531
Iter 7680, Minibatch Loss= 0.789331, Training Accuracy= 0.71875
Iter 8960, Minibatch Loss= 0.625995, Training Accuracy= 0.82031
Iter 10240, Minibatch Loss= 0.578243, Training Accuracy= 0.82812
Iter 11520, Minibatch Loss= 0.505247, Training Accuracy= 0.84375
Iter 12800, Minibatch Loss= 0.497843, Training Accuracy= 0.79688
Iter 14080, Minibatch Loss= 0.471385, Training Accuracy= 0.84375
Iter 15360, Minibatch Loss= 0.398706, Training Accuracy= 0.89062
Iter 16640, Minibatch Loss= 0.456422, Training Accuracy= 0.84375
Iter 17920, Minibatch Loss= 0.299054, Training Accuracy= 0.92188
Iter 19200, Minibatch Loss= 0.380351, Training Accuracy= 0.88281
Iter 20480, Minibatch Loss= 0.311767, Training Accuracy= 0.91406
Iter 21760, Minibatch Loss= 0.321105, Training Accuracy= 0.90625
Iter 23040, Minibatch Loss= 0.425354, Training Accuracy= 0.87500
Iter 24320, Minibatch Loss= 0.376458, Training Accuracy= 0.89844
Iter 25600, Minibatch Loss= 0.244626, Training Accuracy= 0.91406
Iter 26880, Minibatch Loss= 0.199259, Training Accuracy= 0.93750
Iter 28160, Minibatch Loss= 0.141835, Training Accuracy= 0.96875
Iter 29440, Minibatch Loss= 0.245714, Training Accuracy= 0.91406
Iter 30720, Minibatch Loss= 0.202359, Training Accuracy= 0.93750
Iter 32000, Minibatch Loss= 0.230693, Training Accuracy= 0.92188
Iter 33280, Minibatch Loss= 0.261713, Training Accuracy= 0.93750
Iter 34560, Minibatch Loss= 0.187279, Training Accuracy= 0.95312
Iter 35840, Minibatch Loss= 0.349192, Training Accuracy= 0.89844
Iter 37120, Minibatch Loss= 0.194743, Training Accuracy= 0.94531
Iter 38400, Minibatch Loss= 0.123681, Training Accuracy= 0.96875
Iter 39680, Minibatch Loss= 0.154273, Training Accuracy= 0.96875
Iter 40960, Minibatch Loss= 0.166156, Training Accuracy= 0.96094
Iter 42240, Minibatch Loss= 0.221127, Training Accuracy= 0.92969
Iter 43520, Minibatch Loss= 0.184999, Training Accuracy= 0.93750
Iter 44800, Minibatch Loss= 0.129644, Training Accuracy= 0.95312
Iter 46080, Minibatch Loss= 0.086950, Training Accuracy= 0.96094
Iter 47360, Minibatch Loss= 0.163142, Training Accuracy= 0.93750
Iter 48640, Minibatch Loss= 0.128150, Training Accuracy= 0.96094
Iter 49920, Minibatch Loss= 0.056789, Training Accuracy= 0.99219
Iter 51200, Minibatch Loss= 0.150238, Training Accuracy= 0.96875
Iter 52480, Minibatch Loss= 0.146947, Training Accuracy= 0.95312
Iter 53760, Minibatch Loss= 0.209401, Training Accuracy= 0.93750
Iter 55040, Minibatch Loss= 0.327227, Training Accuracy= 0.90625
Iter 56320, Minibatch Loss= 0.129579, Training Accuracy= 0.97656
Iter 57600, Minibatch Loss= 0.139996, Training Accuracy= 0.96875
Iter 58880, Minibatch Loss= 0.115297, Training Accuracy= 0.96094
Iter 60160, Minibatch Loss= 0.095211, Training Accuracy= 0.99219
Iter 61440, Minibatch Loss= 0.145662, Training Accuracy= 0.96094
Iter 62720, Minibatch Loss= 0.103417, Training Accuracy= 0.97656
Iter 64000, Minibatch Loss= 0.089319, Training Accuracy= 0.99219
Iter 65280, Minibatch Loss= 0.168895, Training Accuracy= 0.94531
Iter 66560, Minibatch Loss= 0.119453, Training Accuracy= 0.96875
Iter 67840, Minibatch Loss= 0.154639, Training Accuracy= 0.94531
Iter 69120, Minibatch Loss= 0.123485, Training Accuracy= 0.96875
Iter 70400, Minibatch Loss= 0.041742, Training Accuracy= 1.00000
Iter 71680, Minibatch Loss= 0.131384, Training Accuracy= 0.96875
Iter 72960, Minibatch Loss= 0.099000, Training Accuracy= 0.96875
Iter 74240, Minibatch Loss= 0.092006, Training Accuracy= 0.96875
Iter 75520, Minibatch Loss= 0.099472, Training Accuracy= 0.97656
Iter 76800, Minibatch Loss= 0.166030, Training Accuracy= 0.96875
Iter 78080, Minibatch Loss= 0.153196, Training Accuracy= 0.96094
Iter 79360, Minibatch Loss= 0.058163, Training Accuracy= 0.98438
Iter 80640, Minibatch Loss= 0.046836, Training Accuracy= 0.99219
Iter 81920, Minibatch Loss= 0.071704, Training Accuracy= 0.96094
Iter 83200, Minibatch Loss= 0.082740, Training Accuracy= 0.96875
Iter 84480, Minibatch Loss= 0.077883, Training Accuracy= 0.96875
Iter 85760, Minibatch Loss= 0.049954, Training Accuracy= 0.98438
Iter 87040, Minibatch Loss= 0.081227, Training Accuracy= 0.97656
Iter 88320, Minibatch Loss= 0.156184, Training Accuracy= 0.94531
Iter 89600, Minibatch Loss= 0.105250, Training Accuracy= 0.96094
Iter 90880, Minibatch Loss= 0.039424, Training Accuracy= 1.00000
Iter 92160, Minibatch Loss= 0.043280, Training Accuracy= 0.99219
Iter 93440, Minibatch Loss= 0.081290, Training Accuracy= 0.98438
Iter 94720, Minibatch Loss= 0.098623, Training Accuracy= 0.96875
Iter 96000, Minibatch Loss= 0.033196, Training Accuracy= 0.99219
Iter 97280, Minibatch Loss= 0.260645, Training Accuracy= 0.92188
Iter 98560, Minibatch Loss= 0.058427, Training Accuracy= 0.99219
Iter 99840, Minibatch Loss= 0.055389, Training Accuracy= 0.97656
Finished!
Testing Accuracy: 0.99
9-8 mcellLSTMGRU
程序:
import numpy as np
import tensorflow as tf
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/shendu/MNIST_data/", one_hot=True)
n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10 # MNIST 列别 (0-9 ,一共10类)
batch_size = 128
tf.reset_default_graph()
# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
'''------------------------------------------------------------------------'''
#
#stacked_rnn = []
#for i in range(3):
# stacked_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
#mcell = tf.contrib.rnn.MultiRNNCell(stacked_rnn)
gru = tf.contrib.rnn.GRUCell(n_hidden*2)
lstm_cell = tf.contrib.rnn.LSTMCell(n_hidden)
mcell = tf.contrib.rnn.MultiRNNCell([lstm_cell,gru])
'''-------------------------------------------------------------------------'''
x1 = tf.unstack(x, n_steps, 1)
outputs, states = tf.contrib.rnn.static_rnn(mcell, x1, dtype=tf.float32)
pred = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)
learning_rate = 0.001
# 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))
training_iters = 100000
display_step = 10
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
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, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算批次数据的准确率
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(" Finished!")
# 计算准确率 for 128 mnist test images
test_len = 100
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}))
结果:
Extracting F:/shendu/MNIST_data/train-images-idx3-ubyte.gz
Extracting F:/shendu/MNIST_data/train-labels-idx1-ubyte.gz
Extracting F:/shendu/MNIST_data/t10k-images-idx3-ubyte.gz
Extracting F:/shendu/MNIST_data/t10k-labels-idx1-ubyte.gz
Iter 1280, Minibatch Loss= 1.704408, Training Accuracy= 0.36719
Iter 2560, Minibatch Loss= 1.171062, Training Accuracy= 0.60938
Iter 3840, Minibatch Loss= 1.116253, Training Accuracy= 0.56250
Iter 5120, Minibatch Loss= 0.823117, Training Accuracy= 0.73438
Iter 6400, Minibatch Loss= 0.814699, Training Accuracy= 0.70312
Iter 7680, Minibatch Loss= 0.642769, Training Accuracy= 0.80469
Iter 8960, Minibatch Loss= 0.550666, Training Accuracy= 0.82812
Iter 10240, Minibatch Loss= 0.404024, Training Accuracy= 0.86719
Iter 11520, Minibatch Loss= 0.410674, Training Accuracy= 0.87500
Iter 12800, Minibatch Loss= 0.342280, Training Accuracy= 0.87500
Iter 14080, Minibatch Loss= 0.368628, Training Accuracy= 0.90625
Iter 15360, Minibatch Loss= 0.399377, Training Accuracy= 0.87500
Iter 16640, Minibatch Loss= 0.258065, Training Accuracy= 0.92969
Iter 17920, Minibatch Loss= 0.442166, Training Accuracy= 0.84375
Iter 19200, Minibatch Loss= 0.327701, Training Accuracy= 0.87500
Iter 20480, Minibatch Loss= 0.215760, Training Accuracy= 0.94531
Iter 21760, Minibatch Loss= 0.180150, Training Accuracy= 0.96875
Iter 23040, Minibatch Loss= 0.297834, Training Accuracy= 0.88281
Iter 24320, Minibatch Loss= 0.197211, Training Accuracy= 0.94531
Iter 25600, Minibatch Loss= 0.201410, Training Accuracy= 0.92969
Iter 26880, Minibatch Loss= 0.199701, Training Accuracy= 0.90625
Iter 28160, Minibatch Loss= 0.187964, Training Accuracy= 0.94531
Iter 29440, Minibatch Loss= 0.282932, Training Accuracy= 0.91406
Iter 30720, Minibatch Loss= 0.150648, Training Accuracy= 0.92188
Iter 32000, Minibatch Loss= 0.179374, Training Accuracy= 0.96094
Iter 33280, Minibatch Loss= 0.257124, Training Accuracy= 0.91406
Iter 34560, Minibatch Loss= 0.105023, Training Accuracy= 0.97656
Iter 35840, Minibatch Loss= 0.140232, Training Accuracy= 0.95312
Iter 37120, Minibatch Loss= 0.200730, Training Accuracy= 0.94531
Iter 38400, Minibatch Loss= 0.161329, Training Accuracy= 0.95312
Iter 39680, Minibatch Loss= 0.213002, Training Accuracy= 0.95312
Iter 40960, Minibatch Loss= 0.148071, Training Accuracy= 0.94531
Iter 42240, Minibatch Loss= 0.133764, Training Accuracy= 0.96094
Iter 43520, Minibatch Loss= 0.101090, Training Accuracy= 0.96094
Iter 44800, Minibatch Loss= 0.190879, Training Accuracy= 0.93750
Iter 46080, Minibatch Loss= 0.161736, Training Accuracy= 0.94531
Iter 47360, Minibatch Loss= 0.128403, Training Accuracy= 0.93750
Iter 48640, Minibatch Loss= 0.074586, Training Accuracy= 0.98438
Iter 49920, Minibatch Loss= 0.128215, Training Accuracy= 0.96094
Iter 51200, Minibatch Loss= 0.170761, Training Accuracy= 0.94531
Iter 52480, Minibatch Loss= 0.167473, Training Accuracy= 0.96094
Iter 53760, Minibatch Loss= 0.143248, Training Accuracy= 0.93750
Iter 55040, Minibatch Loss= 0.085302, Training Accuracy= 0.97656
Iter 56320, Minibatch Loss= 0.070920, Training Accuracy= 0.98438
Iter 57600, Minibatch Loss= 0.210906, Training Accuracy= 0.93750
Iter 58880, Minibatch Loss= 0.100869, Training Accuracy= 0.96094
Iter 60160, Minibatch Loss= 0.060385, Training Accuracy= 0.97656
Iter 61440, Minibatch Loss= 0.065903, Training Accuracy= 0.98438
Iter 62720, Minibatch Loss= 0.084168, Training Accuracy= 0.98438
Iter 64000, Minibatch Loss= 0.039691, Training Accuracy= 1.00000
Iter 65280, Minibatch Loss= 0.146266, Training Accuracy= 0.96094
Iter 66560, Minibatch Loss= 0.079453, Training Accuracy= 0.96094
Iter 67840, Minibatch Loss= 0.028447, Training Accuracy= 1.00000
Iter 69120, Minibatch Loss= 0.030509, Training Accuracy= 0.99219
Iter 70400, Minibatch Loss= 0.170530, Training Accuracy= 0.94531
Iter 71680, Minibatch Loss= 0.092618, Training Accuracy= 0.97656
Iter 72960, Minibatch Loss= 0.033546, Training Accuracy= 1.00000
Iter 74240, Minibatch Loss= 0.128716, Training Accuracy= 0.95312
Iter 75520, Minibatch Loss= 0.103495, Training Accuracy= 0.95312
Iter 76800, Minibatch Loss= 0.085188, Training Accuracy= 0.98438
Iter 78080, Minibatch Loss= 0.116295, Training Accuracy= 0.97656
Iter 79360, Minibatch Loss= 0.123180, Training Accuracy= 0.96094
Iter 80640, Minibatch Loss= 0.043947, Training Accuracy= 0.99219
Iter 81920, Minibatch Loss= 0.074503, Training Accuracy= 0.96875
Iter 83200, Minibatch Loss= 0.047790, Training Accuracy= 0.99219
Iter 84480, Minibatch Loss= 0.049433, Training Accuracy= 0.98438
Iter 85760, Minibatch Loss= 0.096839, Training Accuracy= 0.97656
Iter 87040, Minibatch Loss= 0.042759, Training Accuracy= 0.99219
Iter 88320, Minibatch Loss= 0.083123, Training Accuracy= 0.96094
Iter 89600, Minibatch Loss= 0.077954, Training Accuracy= 0.97656
Iter 90880, Minibatch Loss= 0.035374, Training Accuracy= 0.98438
Iter 92160, Minibatch Loss= 0.092402, Training Accuracy= 0.96094
Iter 93440, Minibatch Loss= 0.081790, Training Accuracy= 0.96875
Iter 94720, Minibatch Loss= 0.124706, Training Accuracy= 0.96875
Iter 96000, Minibatch Loss= 0.065185, Training Accuracy= 0.97656
Iter 97280, Minibatch Loss= 0.030543, Training Accuracy= 1.00000
Iter 98560, Minibatch Loss= 0.050131, Training Accuracy= 0.99219
Iter 99840, Minibatch Loss= 0.068575, Training Accuracy= 0.97656
Finished!
Testing Accuracy: 1.0
9-9 动态多层
使用动态多层RNN网络对MNIST数据集分类
程序:
import numpy as np
import tensorflow as tf
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/shendu/MNIST_data/", one_hot=True)
n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10 # MNIST 列别 (0-9 ,一共10类)
batch_size = 128
tf.reset_default_graph()
# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
gru = tf.contrib.rnn.GRUCell(n_hidden * 2)
lstm_cell = tf.contrib.rnn.LSTMCell(n_hidden)
mcell = tf.contrib.rnn.MultiRNNCell([lstm_cell, gru])
'''----------------------------------------------------------------------------'''
outputs, states = tf.nn.dynamic_rnn(mcell, x, dtype=tf.float32) # (?, 28, 256)
outputs = tf.transpose(outputs, [1, 0, 2]) # (28, ?, 256) 28个时序,取最后一个时序outputs[-1]=(?,256)
pred = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)
'''----------------------------------------------------------------------------'''
learning_rate = 0.001
# 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))
training_iters = 100000
display_step = 10
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
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, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算批次数据的准确率
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(" Finished!")
# 计算准确率 for 128 mnist test images
test_len = 100
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}))
结果:
。。。
Testing Accuracy: 0.95
9-14 BiRNNMnist
使用单层动态双向RNN网络对MNIST数据集分类
程序:
import tensorflow as tf
from tensorflow.contrib import rnn
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/shendu/MNIST_data/", one_hot=True)
#参数设置
learning_rate = 0.001
training_iters = 100000
batch_size = 128
display_step = 10
# Network Parameters
n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10 # MNIST 列别 (0-9 ,一共10类)
tf.reset_default_graph()
#定义占位符 tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
x1 = tf.unstack(x, n_steps, 1)
lstm_fw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
# 反向cell
lstm_bw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
outputs, output_states = tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell,lstm_bw_cell,x,
dtype=tf.float32)
print(len(outputs),outputs[0].shape,outputs[1].shape)
outputs = tf.concat(outputs, 2)
outputs = tf.transpose(outputs, [1, 0, 2])
pred = tf.contrib.layers.fully_connected(outputs[-1],n_classes,activation_fn = None)
'''---------------------------------------------------------------------------------'''
# 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))
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
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, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算批次数据的准确率
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 (" Finished!")
# 计算准确率 for 128 mnist test images
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}))
结果:
。。。
Testing Accuracy: 0.984375
9-15 单层静态双向rnn
使用单层静态双向RNN网络对MNIST数据集分类
程序:
import tensorflow as tf
from tensorflow.contrib import rnn
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/shendu/MNIST_data/", one_hot=True)
#参数设置
learning_rate = 0.001
training_iters = 100000
batch_size = 128
display_step = 10
# Network Parameters
n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10 # MNIST 列别 (0-9 ,一共10类)
tf.reset_default_graph()
#定义占位符 tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
x1 = tf.unstack(x, n_steps, 1)
lstm_fw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
# 反向cell
lstm_bw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
'''-------------------------------------------------------------------------------'''
outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x1,
dtype=tf.float32)
print(outputs[0].shape,len(outputs))
pred = tf.contrib.layers.fully_connected(outputs[-1],n_classes,activation_fn = None)
'''---------------------------------------------------------------------------------'''
# 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))
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
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, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算批次数据的准确率
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 (" Finished!")
# 计算准确率 for 128 mnist test images
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}))
结果:
。。。
Testing Accuracy: 0.9765625
9-16 多层双向RNN
程序:
import tensorflow as tf
from tensorflow.contrib import rnn
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/shendu/MNIST_data/", one_hot=True)
#参数设置
learning_rate = 0.001
training_iters = 100000
batch_size = 128
display_step = 10
# Network Parameters
n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10 # MNIST 列别 (0-9 ,一共10类)
tf.reset_default_graph()
#定义占位符 tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
x1 = tf.unstack(x, n_steps, 1)
lstm_fw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
# 反向cell
lstm_bw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
'''-------------------------------------------------------------------------------'''
outputs, _, _ = rnn.stack_bidirectional_rnn([lstm_fw_cell],[lstm_bw_cell], x1,
dtype=tf.float32)
'''---------------------------------------------------------------------------------'''
print(outputs[0].shape,len(outputs))
pred = tf.contrib.layers.fully_connected(outputs[-1],n_classes,activation_fn = None)
# 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))
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
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, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算批次数据的准确率
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 (" Finished!")
# 计算准确率 for 128 mnist test images
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}))
结果:
。。。
Testing Accuracy: 0.984375
9-17 list多层双向RNN
程序:
import tensorflow as tf
from tensorflow.contrib import rnn
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/shendu/MNIST_data/", one_hot=True)
#参数设置
learning_rate = 0.001
training_iters = 100000
batch_size = 128
display_step = 10
# Network Parameters
n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10 # MNIST 列别 (0-9 ,一共10类)
tf.reset_default_graph()
#定义占位符 tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
x1 = tf.unstack(x, n_steps, 1)
lstm_fw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
# 反向cell
lstm_bw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
'''-------------------------------------------------------------------------------'''
stacked_rnn = []
stacked_bw_rnn = []
for i in range(3):
stacked_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
stacked_bw_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
outputs, _, _ = rnn.stack_bidirectional_rnn(stacked_rnn, stacked_bw_rnn, x1,
dtype=tf.float32)
'''---------------------------------------------------------------------------------'''
pred = tf.contrib.layers.fully_connected(outputs[-1],n_classes,activation_fn = None)
# 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))
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
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, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算批次数据的准确率
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 (" Finished!")
# 计算准确率 for 128 mnist test images
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}))
结果:
。。。
Testing Accuracy: 0.9765625
9-18 Multi双向RNN
程序:
import tensorflow as tf
from tensorflow.contrib import rnn
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/shendu/MNIST_data/", one_hot=True)
#参数设置
learning_rate = 0.001
training_iters = 100000
batch_size = 128
display_step = 10
# Network Parameters
n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10 # MNIST 列别 (0-9 ,一共10类)
tf.reset_default_graph()
#定义占位符 tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
x1 = tf.unstack(x, n_steps, 1)
lstm_fw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
# 反向cell
lstm_bw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
'''-------------------------------------------------------------------------------'''
stacked_rnn = []
stacked_bw_rnn = []
for i in range(3):
stacked_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
stacked_bw_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
mcell = tf.contrib.rnn.MultiRNNCell(stacked_rnn)
mcell_bw = tf.contrib.rnn.MultiRNNCell(stacked_bw_rnn)
outputs, _, _ = rnn.stack_bidirectional_rnn([mcell], [mcell_bw], x1,
dtype=tf.float32)
'''--------------------------------------------------------------------------------'''
pred = tf.contrib.layers.fully_connected(outputs[-1],n_classes,activation_fn = None)
# 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))
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
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, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算批次数据的准确率
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 (" Finished!")
# 计算准确率 for 128 mnist test images
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}))
结果:
。。。
Testing Accuracy: 0.9921875
9-19 动态Multi双向RNN
程序:
import tensorflow as tf
from tensorflow.contrib import rnn
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/shndu/MNIST_data/", one_hot=True)
# 参数设置
learning_rate = 0.001
training_iters = 100000
batch_size = 128
display_step = 10
# Network Parameters
n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10 # MNIST 列别 (0-9 ,一共10类)
tf.reset_default_graph()
# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
x1 = tf.unstack(x, n_steps, 1)
lstm_fw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
lstm_bw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
# outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x1,
# dtype=tf.float32)
# outputs, _, _ = rnn.stack_bidirectional_rnn([lstm_fw_cell],[lstm_bw_cell], x1,
# dtype=tf.float32)
'''---------------------------------------------------------------'''
stacked_rnn = []
stacked_bw_rnn = []
for i in range(3):
stacked_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
stacked_bw_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
# outputs, _, _ = rnn.stack_bidirectional_rnn(stacked_rnn,stacked_bw_rnn, x1,
# dtype=tf.float32)
mcell = tf.contrib.rnn.MultiRNNCell(stacked_rnn)
mcell_bw = tf.contrib.rnn.MultiRNNCell(stacked_bw_rnn)
# outputs, _, _ = rnn.stack_bidirectional_rnn([mcell],[mcell_bw], x1,
# dtype=tf.float32)
outputs, _, _ = rnn.stack_bidirectional_dynamic_rnn([mcell], [mcell_bw], x,
dtype=tf.float32)
outputs = tf.transpose(outputs,
[1, 0, 2]) # batch_size, max_time, layers_output]`
print(outputs[0].shape, outputs.shape)
pred = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)
'''--------------------------------------------------------------------------------'''
# 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))
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
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, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算批次数据的准确率
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(" Finished!")
# 计算准确率 for 128 mnist test images
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}))
结果:
。。。
Testing Accuracy: 0.953125
9-20 inGRUonMnist
手动构建GRUCell的LN代码,使用该cell对MNIST数据集分类
程序:
import numpy as np
import tensorflow as tf
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/shendu/MNIST_data/", one_hot=True)
from tensorflow.python.ops.rnn_cell_impl import RNNCell
from tensorflow.python.ops.rnn_cell_impl import LayerRNNCell
from tensorflow.python.ops.math_ops import sigmoid
from tensorflow.python.ops.math_ops import tanh
from tensorflow.python.ops import variable_scope as vs
from tensorflow.python.ops import array_ops
from tensorflow.contrib.rnn.python.ops.core_rnn_cell import _linear
from tensorflow.python.layers import base as base_layer
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import init_ops
from tensorflow.python.ops import nn_ops
print("Tensorflow版本:")
print(tf.__version__)
tf.reset_default_graph()
n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10 # MNIST 列别 (0-9 ,一共10类)
tf.reset_default_graph()
# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
batch_size = 128
xnorm = x*2-1
x1 = tf.unstack(xnorm, n_steps, 1)
#1 BasicLSTMCell
#lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
#outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x1, dtype=tf.float32)
#2 LSTMCell
#lstm_cell = tf.contrib.rnn.LSTMCell(n_hidden, forget_bias=1.0)
lstm_cell = tf.contrib.rnn.LayerNormBasicLSTMCell(n_hidden,layer_norm=True)
outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x1, dtype=tf.float32)
#3 gru
#gru = tf.contrib.rnn.GRUCell(n_hidden)
#gru = LNGRUCell(n_hidden)
#LayerNormBasicLSTMCell
#outputs = tf.contrib.rnn.static_rnn(gru, x1, dtype=tf.float32)
#4 创建动态RNN
#outputs,_ = tf.nn.dynamic_rnn(gru,xnorm,dtype=tf.float32)
#outputs = tf.transpose(outputs, [1, 0, 2])
pred = tf.contrib.layers.fully_connected(outputs[-1],n_classes,activation_fn = None)
learning_rate = 0.001
training_iters = 100000
display_step = 10
# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(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))
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
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, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 计算批次数据的准确率
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 (" Finished!")
# 计算准确率 for 128 mnist test images
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}))
结果:
。。。
Testing Accuracy: 0.9921875