Tensorflow的GRUCell
的以下代碼顯示了當前一個隱藏狀態與序列中的當前輸入一起提供時,獲取更新隱藏狀態的典型操作。Tensorflow中GRU單元的說明?
def __call__(self, inputs, state, scope=None):
"""Gated recurrent unit (GRU) with nunits cells."""
with vs.variable_scope(scope or type(self).__name__): # "GRUCell"
with vs.variable_scope("Gates"): # Reset gate and update gate.
# We start with bias of 1.0 to not reset and not update.
r, u = array_ops.split(1, 2, _linear([inputs, state],
2 * self._num_units, True, 1.0))
r, u = sigmoid(r), sigmoid(u)
with vs.variable_scope("Candidate"):
c = self._activation(_linear([inputs, r * state],
self._num_units, True))
new_h = u * state + (1 - u) * c
return new_h, new_h
但我看不出有任何weights
和biases
這裏。 例如我的理解是,獲得r
和u
將需要權重和偏差與當前輸入和/或隱藏狀態相乘以獲得更新的隱藏狀態。
我已經寫了GRU單位如下:
def gru_unit(previous_hidden_state, x):
r = tf.sigmoid(tf.matmul(x, Wr) + br)
z = tf.sigmoid(tf.matmul(x, Wz) + bz)
h_ = tf.tanh(tf.matmul(x, Wx) + tf.matmul(previous_hidden_state, Wh) * r)
current_hidden_state = tf.mul((1 - z), h_) + tf.mul(previous_hidden_state, z)
return current_hidden_state
在這裏,我明確地使用權Wx, Wr, Wz, Wh
和偏見br, bh, bz
等得到更新隱藏狀態。這些權重和偏差是訓練後學習/調整的內容。
如何使用Tensorflow的內置GRUCell
實現與上述相同的結果?
他們串聯了'r'和'z'門做這一切一氣呵成,節省了計算。 –