建议参考官网文档和伪代码结合BasicLSTMCell / MultiRNNCell等源码理解,尤其是官网的伪代码.
BasicLSTMCell和MultiRNNCell的__call___( )方法都是每次处理每个batch对应的一个词:
for i in range(num_steps):
# The value of state is updated after processing each batch of words.
output, state = lstm(words[:, i], state)
# The rest of the code.
# ...
final_state = state
initial_state = state = stacked_lstm.zero_state(batch_size, tf.float32)
for i in range(num_steps):
# The value of state is updated after processing each batch of words.
output, state = stacked_lstm(words[:, i], state)
# The rest of the code.
# ...
final_state = state
警告: 建议初学者没有必要接触Keras,将tensorflow过于封装了,灵活性差,关键是不能很好的理解底层连接的逻辑结构,也对理解论文里的网络结构和数据传输关系产生障碍.