tf.nn.rnn_cell.DropoutWrapper用法细节案例2
-- coding: utf-8 --
import tensorflow as tf
from tensorflow.contrib import rnn
导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/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):
lstm_fw_cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True,
reuse=tf.get_variable_scope().reuse)
lstm_fw_cell = tf.nn.rnn_cell.DropoutWrapper(lstm_fw_cell, input_keep_prob=0.5, output_keep_prob=0.5, )
lstm_bw_cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0, reuse=tf.get_variable_scope().reuse
, state_is_tuple=True) # , state_is_tuple = True
lstm_bw_cell = tf.nn.rnn_cell.DropoutWrapper(lstm_bw_cell, input_keep_prob=0.5, output_keep_prob=0.5)
stacked_rnn.append(lstm_fw_cell)
stacked_bw_rnn.append(lstm_bw_cell)
outputs, _, _ = rnn.stack_bidirectional_rnn(stacked_rnn,stacked_bw_rnn, x1,
dtype=tf.float32)
‘’’
mcell=tf.nn.rnn_cell.MultiRNNCell(stacked_rnn)
mcell = tf.nn.rnn_cell.DropoutWrapper(mcell, output_keep_prob=0.5, input_keep_prob=1.0)
mcell_bw=tf.nn.rnn_cell.MultiRNNCell(stacked_bw_rnn)
mcell_bw = tf.nn.rnn_cell.DropoutWrapper(mcell_bw, output_keep_prob=0.5, input_keep_prob=1.0)
‘’’
#法一
mcell = tf.nn.rnn_cell.MultiRNNCell(stacked_rnn)
mcell_bw = tf.nn.rnn_cell.MultiRNNCell(stacked_bw_rnn)
inint_state = mcell.zero_state(batch_size, tf.float32)
inint_state2 = mcell_bw.zero_state(batch_size, tf.float32)
‘’’
#法二
def lstm_call():
cell = tf.nn.rnn_cell.LSTMCell(num_units=n_hidden, reuse=tf.get_variable_scope().reuse)
return tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=0.5)
mcell = tf.nn.rnn_cell.MultiRNNCell(cells=[lstm_call() for i in range(3)])
inint_state = mcell.zero_state(batch_size, tf.float32)
mcell_bw = tf.nn.rnn_cell.MultiRNNCell(cells=[lstm_call() for i in range(3)])
inint_state2 = mcell_bw.zero_state(batch_size, tf.float32)
‘’’
outputs, _, _ = rnn.stack_bidirectional_dynamic_rnn([mcell], [mcell_bw], x, initial_states_fw=[inint_state],
initial_states_bw=[inint_state2], dtype=tf.float32)#!!!注意四处[]
print(outputs[0].shape, outputs.shape)
outputs = tf.transpose(outputs, [1, 0, 2])
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}))
上一篇: 『Pytorch』BatchNorm
下一篇: pytorch模型转onnx
推荐阅读
-
tf.nn.rnn_cell.DropoutWrapper用法细节案例2
-
yii2 GridView 日期格式化并实现日期可搜索 案例 android gridview用法 .net gridview c gridview
-
yii2 GridView 日期格式化并实现日期可搜索 案例 android gridview用法 .net gridview c gridview
-
yii2 GridView 下拉搜索实现案例教程 android gridview用法 .net gridview c gridview
-
yii2 GridView 下拉搜索实现案例教程 android gridview用法 .net gridview c gridview