简洁单层RNN
import tensorflow as tf
from tensorflow.contrib.rnn import LSTMCell
from tensorflow.examples.tutorials.mnist import input_data
"""加载样本集:手写数字"""
mnist = input_data.read_data_sets('data', one_hot=True)
"""网络结构"""
num_units = 50
batch_size = 500
X = tf.placeholder(tf.float32, [batch_size, 28, 28])
y = tf.placeholder(tf.float32, [batch_size, 10])
lstm_cell = LSTMCell(num_units)
LSTMStateTuple = lstm_cell.zero_state(batch_size, tf.float32)
for time_step in range(28):
h, LSTMStateTuple = lstm_cell(X[:, time_step, :], LSTMStateTuple)
W = tf.Variable(tf.truncated_normal([num_units, 10], stddev=.1))
b = tf.Variable(tf.constant(.1, tf.float32, [10]), dtype=tf.float32)
o = tf.matmul(h, W) + b
loss = tf.nn.softmax_cross_entropy_with_logits(lab