Problems
Loading...
Step 4 of 6
Hidden State Update
Easy

Combine the previous GRU hidden state and the candidate state with the update gate:

ht=zt⊙ht−1+(1−zt)⊙h~t.h_t=z_t\odot h_{t-1}+(1-z_t)\odot\widetilde{h}_t.

Here, ztz_t is the update gate, ht−1h_{t-1} is the previous state, h~t\widetilde{h}_t is the candidate state, and ⊙\odot means elementwise multiplication. Accept one sample or a batch and return the new state as a matching float64 NumPy array.

Loading visualization...

Examples

Input: z_t = [0.5,0.5], h_prev = [1,-1], h_tilde = [0.2,0.4]

Output: [0.6,-0.3]

Explanation: A gate value of 0.5 averages the corresponding old and candidate values.

Input: z_t = [1,1], h_prev = [2,-3], h_tilde = [8,9]

Output: [2,-3]

Input: z_t = [[0,0.25],[0.75,1]], h_prev = [[1,2],[3,4]], h_tilde = [[5,6],[7,8]]

Output: [[5,5],[4,4]]

Hint 1

Compute z_t * h_prev + (1.0 - z_t) * h_tilde elementwise.

Requirements

  • Blend the previous and candidate states elementwise using z_t.
  • Return a float64 NumPy array with the same shape as the inputs.

Constraints

  • All three arrays have the same shape, either (H,)(H,) or (N,H)(N,H).
  • Gate values lie in [0,1][0,1].
  • Every input is float64 and finite.

Related practice