网站首页 文章专栏 tensorflow lstm原理与代码从头构建
tensorflow lstm原理与代码从头构建
创建于:2019-06-23 06:49:41 更新于:2024-04-19 01:32:41 羽瀚尘 1043
深度学习 LSTM



tensorflow lstm原理与代码从头构建

简单的一层lstm


”`python

使用 basic LSTM Cell.

lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden_units, forget_bias=1.0, state_is_tuple=True)
init_state = lstm_cell.zero_state(batch_size, dtype=tf.float32) # 初始化全零 state

如果使用tf.nn.dynamic_rnn(cell, inputs), 我们要确定 inputs 的格式. tf.nn.dynamic_rnn 中的 time_major 参数会针对不同 inputs 格式有不同的值.

如果 inputs 为 (batches, steps, inputs) ==> time_major=False;

如果 inputs 为 (steps, batches, inputs) ==> time_major=True;


outputs, final_state = tf.nn.dynamic_rnn(lstm_cell, X_in, initial_state=init_state, time_major=False)

outputs size: [batch_size, max_time, cell_state_size]
final_state[1] size: [batch_size, cell_state_size]
”`

Reference

  1. 各种LSTM代码比较全 地址