Md. Asif Uddin
    Problem I.10.B04

    The same weights, two GRUs

    counterexample▲▲△

    Exact values where possible; six decimal places for non-exact numerical results.

    STATEMENT

    Show that applying the reset gate before or after the recurrent matrix gives different GRUs, and find exactly when the two agree.

    GIVEN

    A two-unit GRU step with zero input contribution and zero candidate biases. The recurrent candidate matrix swaps coordinates, Wn=[0110]\mat W_n=\begin{bmatrix}0&1\\1&0\end{bmatrix}. The preceding state is ht−1=(0.5,−0.5)\vec h_{t-1}=(0.5,-0.5). The reset pre-activations are (ln⁡3,−ln⁡3)(\ln3,-\ln3) and the update pre-activations are zero. Use PyTorch’s update, ht=(1−zt)⊙nt+zt⊙ht−1\vec h_t=(1-\vec z_t)\odot\vec n_t+\vec z_t\odot\vec h_{t-1}, for both.

    FIND

    The gates, both candidates and both new states; then a condition on Wn\mat W_n and rt\vec r_t under which the two placements agree for every state.

    STRATEGY

    Compute W(r⊙h)\mat W(\vec r\odot\vec h) and r⊙(Wh)\vec r\odot(\mat W\vec h) separately. For the general condition, write both as products with diag⁡(r)\operatorname{diag}(\vec r) and compare entries.

    SOLUTION

    The gates are r=(σ(ln⁡3),σ(−ln⁡3))=(0.75,0.25)\vec r=(\sigma(\ln3),\sigma(-\ln3))=(0.75,0.25) and z=(0.5,0.5)\vec z=(0.5,0.5). The update gate is the same in both models, so any difference comes from the candidate.

    Reset before the matrix, as in Cho and colleagues: r⊙h=(0.375,−0.125)\vec r\odot\vec h=(0.375,-0.125), and the swap gives (−0.125,0.375)(-0.125,0.375). Hence

    nbefore=(tanh⁡(−0.125),tanh⁡(0.375))=(−0.124353,0.358357).\vec n^{\text{before}}=(\tanh(-0.125),\tanh(0.375))=(-0.124353,0.358357).

    Reset after the matrix, as in PyTorch: Wh=(−0.5,0.5)\mat W\vec h=(-0.5,0.5), then r⊙Wh=(−0.375,0.125)\vec r\odot\mat W\vec h=(-0.375,0.125), and

    nafter=(−0.358357,0.124353).\vec n^{\text{after}}=(-0.358357,0.124353).

    With z=1/2z=1/2, the new states are the averages of candidate and old state: (0.187823,−0.070821)(0.187823,-0.070821) before and (0.070821,−0.187823)(0.070821,-0.187823) after. Same weights, same state, same gates, and every coordinate differs by more than 0.10.1.

    When they agree. The candidates’ recurrent parts are Wdiag⁡(r)h\mat W\operatorname{diag}(\vec r)\vec h and diag⁡(r)Wh\operatorname{diag}(\vec r)\mat W\vec h. They agree for every h\vec h exactly when the matrices are equal. Their (j,k)(j,k) entries are WjkrkW_{jk}r_k and rjWjkr_jW_{jk}, so the condition is

    Wjk(rk−rj)=0for all j,k.W_{jk}(r_k-r_j)=0\quad\text{for all }j,k.

    A weight joining two coordinates may be nonzero only if their reset values are equal. A uniform reset, r=ρ1\vec r=\rho\vec 1, satisfies it for every W\mat W; a diagonal W\mat W satisfies it for every r\vec r. The swap matrix with unequal resets satisfies neither, which is why this example separates them.

    Biases add a second difference. In the after form the recurrent bias bhn\vec b_{hn} is inside the reset, r⊙(Wh+bhn)\vec r\odot(\mat W\vec h+\vec b_{hn}), so the reset scales it. In the before form a bias added outside is not scaled. Even scalar models differ when that bias is nonzero.

    Answer

    r=(0.75,0.25)\vec r=(0.75,0.25), z=(0.5,0.5)\vec z=(0.5,0.5). Before: candidate (−0.124353,0.358357)(-0.124353,0.358357), state (0.187823,−0.070821)(0.187823,-0.070821). After: candidate (−0.358357,0.124353)(-0.358357,0.124353), state (0.070821,−0.187823)(0.070821,-0.187823). They agree for all states exactly when Wjk(rk−rj)=0W_{jk}(r_k-r_j)=0 for all j,kj,k.

    Check — sanity

    The swap matrix moves each coordinate’s reset to the other side. Reset before: coordinate one’s value, scaled by 0.750.75, lands in coordinate two. Reset after: coordinate two’s value lands in coordinate one and is scaled there by 0.750.75. The mirror-image answers are what that bookkeeping predicts.

    Where this breaks

    The commutation test is for one step with fixed gates. Over a sequence the two models also produce different gates, because the gates read states that have already diverged. Load a checkpoint into the placement it was trained with. The update-gate convention can be converted by negating weights; this example gives no such conversion for the reset placement.

    Variation

    Make the reset uniform at (0.5,0.5)(0.5,0.5) and keep the swap matrix. Both forms now give the candidate (tanh⁡(−0.25),tanh⁡(0.25))=(−0.244919,0.244919)(\tanh(-0.25),\tanh(0.25))=(-0.244919,0.244919).

    Draws on