def model(X, W, B, lstm_size):
# X, input shape: (batch_size, time_step_size, input_vec_size)
XT = tf.transpose(X, [1, 0, 2]) # permute time_step_size and batch_size
# XT shape: (time_step_size, batch_size, input_vec_size)
XR = tf.reshape(XT, [-1, lstm_size]) # each row has input for each lstm cell (lstm_size=input_vec_size)
# XR shape: (time_step_size * batch_size, input_vec_size)
X_split = tf.split(XR, time_step_size, 0) # split them to time_step_size (28 arrays)
# Each array shape: (batch_size, input_vec_size)
# Make lstm with lstm_size (each input vector size)
lstm = rnn.BasicLSTMCell(lstm_size, forget_bias=1.0, state_is_tuple=True)
# Get lstm cell output, time_step_size (28) arrays with lstm_size output: (batch_size, lstm_size)
outputs, _states = rnn.static_rnn(lstm, X_split, dtype=tf.float32)
# Linear activation
# Get the last output
return tf.matmul(outputs[-1], W) + B, lstm.state_size # State size to initialize the stat
############################## model definition end ######################################
lstm_mnist.py 文件源码
python
阅读 15
收藏 0
点赞 0
评论 0
评论列表
文章目录