Deep Learning · TEE · section
LSTM numerical when gates must first be calculated
Recurrent neural networks
Question. At an LSTM time step, xₜ = 1, hₜ₋₁ = 0 and Cₜ₋₁ = 0.2. The affine preactivations of the forget, input and output gates are all 0. The candidate preactivation is 0.5. Calculate the activated gates, candidate cell content, new cell state and new hidden state.
Given
x = 1; old hidden state = 0; old cell = 0.2.
Forget/input/output preactivations are all 0.
Candidate preactivation = 0.5.
To find
Activate all gates and candidate, then find C and h.
Formula — which one and why
Activate raw gate scores first. A preactivation is an unprocessed score. Convert forget/input/output scores with sigmoid and candidate scores with tanh before using the cell-update equations.
z_f,z_i,z_o = gate preactivations; z_c = candidate preactivation. An already activated gate must not be passed through sigmoid again.
f=\sigma(z_f),\quad i=\sigma(z_i),\quad o=\sigma(z_o)\widetilde C=\tanh(z_c)Yaad rakhne ka cue: Raw score pehle activate; activated gate ko dobara sigmoid nahi.
LSTM cell and hidden state. Multiply old memory by the forget gate; multiply candidate content by the input gate; add those two contributions. Apply tanh to the new cell and multiply by the output gate.
f,i,o = activated forget/input/output gates; C_old = previous cell; C_tilde = candidate content; C = new cell; h = visible hidden state. ⊙ means elementwise multiplication.
C=f\odot C_{\mathrm{old}}+i\odot\widetilde Ch=o\odot\tanh CYaad rakhne ka cue: Purana rakho + naya lo; phir tanh aur output gate.
Solution — step by step
Distinguish preactivations from gate outputs
The affine transformations have already been evaluated in this question. Their outputs are preactivations, so activation functions are still required. Sigmoid is used for f, i and o; tanh is used for the candidate.
\sigma(a)=\frac1{1+e^{-a}}C_t=f_tC_{t-1}+i_t\widetilde C_t,\qquad h_t=o_t\tanh(C_t)Step 1: activate the three gates.
f_t=i_t=o_t=\sigma(0)=\frac1{1+e^0}=0.5Step 2: activate the candidate preactivation.
\widetilde C_t=\tanh(0.5)\approx0.462117157Step 3: calculate the old and new cell contributions separately.
f_tC_{t-1}=0.5(0.2)=0.1i_t\widetilde C_t=0.5\tanh(0.5)\approx0.231058579Step 4: calculate the updated cell.
C_t=0.1+0.5\tanh(0.5)\approx0.331058579Step 5: calculate the hidden output from the updated cell.
h_t=0.5\tanh(C_t)\approx0.159735818Final answer: fₜ = iₜ = oₜ = 0.5; C̃ₜ ≈ 0.462117157; Cₜ ≈ 0.331058579; hₜ ≈ 0.159735818. Retain unrounded calculator values between steps. A gate preactivation of 0 is different from a supplied gate output of 0: the former becomes 0.5 under sigmoid, while the latter would suppress its contribution completely.
LSTM gates from raw score 0
Question. An LSTM has forget, input and output gate raw scores all equal to 0, candidate raw score 0, and old cell 1. Calculate all gates, candidate, new cell and hidden state.
Given
Raw scores z_f = z_i = z_o = z_candidate = 0.
Old cell C_old = 1; gate activation sigmoid; candidate activation tanh.
To find
f,i,o,candidate,C_new,h_new.
Formula — which one and why
Raw gate scores require sigmoid first. The candidate uses tanh; do not apply sigmoid to it.
f retains old memory, i admits candidate, o controls exposed hidden output; C is cell memory.
f=i=o=\sigma(z)\widetilde C=\tanh(z)C_{\mathrm{new}}=fC_{\mathrm{old}}+i\widetilde Ch_{\mathrm{new}}=o\tanh(C_{\mathrm{new}})Yaad rakhne ka cue: Gates sigmoid; candidate tanh; cell mein add; hidden pe output gate.
Solution — step by step
Step 1: calculate the shared gate value.
\sigma(0)=\frac1{1+e^{-0}}\approx0.500000000Step 2: calculate the candidate.
\widetilde C=\tanh(0)\approx0.000000000Step 3: retain the old-cell part.
fC_{\mathrm{old}}=(0.500000000)(1)\approx0.500000000Step 4: admit the new content.
i\widetilde C=(0.500000000)(0.000000000)\approx0.000000000Step 5: add the memory contributions.
C_{\mathrm{new}}=0.500000000+(0.000000000)\approx0.500000000Step 6: apply tanh to the cell.
\tanh(C_{\mathrm{new}})\approx0.462117157Step 7: apply the output gate.
h_{\mathrm{new}}=(0.500000000)(0.462117157)\approx0.231058579Final answer: f=i=o≈0.500000000; candidate≈0.000000000; cell≈0.500000000; hidden≈0.231058579.
LSTM gates from raw score 1
Question. An LSTM has forget, input and output gate raw scores all equal to 1, candidate raw score 1, and old cell -0.5. Calculate all gates, candidate, new cell and hidden state.
Given
Raw scores z_f = z_i = z_o = z_candidate = 1.
Old cell C_old = -0.5; gate activation sigmoid; candidate activation tanh.
To find
f,i,o,candidate,C_new,h_new.
Formula — which one and why
Raw gate scores require sigmoid first. The candidate uses tanh; do not apply sigmoid to it.
f retains old memory, i admits candidate, o controls exposed hidden output; C is cell memory.
f=i=o=\sigma(z)\widetilde C=\tanh(z)C_{\mathrm{new}}=fC_{\mathrm{old}}+i\widetilde Ch_{\mathrm{new}}=o\tanh(C_{\mathrm{new}})Yaad rakhne ka cue: Gates sigmoid; candidate tanh; cell mein add; hidden pe output gate.
Solution — step by step
Step 1: calculate the shared gate value.
\sigma(1)=\frac1{1+e^{-1}}\approx0.731058579Step 2: calculate the candidate.
\widetilde C=\tanh(1)\approx0.761594156Step 3: retain the old-cell part.
fC_{\mathrm{old}}=(0.731058579)(-0.5)\approx-0.365529289Step 4: admit the new content.
i\widetilde C=(0.731058579)(0.761594156)\approx0.556769941Step 5: add the memory contributions.
C_{\mathrm{new}}=-0.365529289+(0.556769941)\approx0.191240652Step 6: apply tanh to the cell.
\tanh(C_{\mathrm{new}})\approx0.188942847Step 7: apply the output gate.
h_{\mathrm{new}}=(0.731058579)(0.188942847)\approx0.138128289Final answer: f=i=o≈0.731058579; candidate≈0.761594156; cell≈0.191240652; hidden≈0.138128289.