{"generator":"Jekyll","link":[{"@attributes":{"href":"https:\/\/sssaha.github.io\/feed.xml","rel":"self","type":"application\/atom+xml"}},{"@attributes":{"href":"https:\/\/sssaha.github.io\/","rel":"alternate","type":"text\/html"}}],"updated":"2023-04-15T11:38:53-07:00","id":"https:\/\/sssaha.github.io\/feed.xml","title":"Shammya Shananda Saha","subtitle":"Scientist II at Electric Power Research Institute","author":{"name":"Dr. Shammya Shananda Saha","email":"shammya.saha@gmail.com"},"entry":{"title":"Feedforward and Backward Propagation in Gated Recurrent Unit (GRU)","link":{"@attributes":{"href":"https:\/\/sssaha.github.io\/posts\/2019\/12\/gru-formula\/","rel":"alternate","type":"text\/html","title":"Feedforward and Backward Propagation in Gated Recurrent Unit (GRU)"}},"published":"2019-12-09T00:00:00-07:00","updated":"2019-12-09T00:00:00-07:00","id":"https:\/\/sssaha.github.io\/posts\/2019\/12\/blog-post-1","content":"<p>In this post, I\u2019ll discuss how to implement a simple Recurrent Neural Network (RNN), specifically the Gated Recurrent Unit (GRU). I\u2019ll present the feed forward proppagation of a GRU Cell at a single time stamp and then derive the formulas for determining parameter gradients using the concept of Backpropagation through time (BPTT).<\/p>\n<p align=\"center\">\n<img src=\"\/images\/blog\/GRU.PNG\" \/>\n<\/p>\n\n<h1 id=\"forward-propagation\">Forward Propagation<\/h1>\n<p>The feedforward propagation equations for a GRU cell are expressed as:<\/p>\n\n\\[z_t = \\sigma(W_{zh}\\ast h_{t-1} + W_{zx}\\ast x_t)\\]\n\n\\[r_t = \\sigma(W_{rh}\\ast h_{t-1} + W_{rx}\\ast x_t)\\]\n\n\\[\\tilde{h}_t = f(W_h\\ast (r_t \\circ h_{t-1}) + W_x\\ast x_t)\\]\n\n\\[h_t = z_t\\circ h_{t-1} + (1-z_t)\\circ \\tilde{h}_t,\\]\n\n<p>where $x_t$ is the input vector at time $t$, $h_t$ is the output vector, $\\ast$ denotes matrix product, $\\circ$ denotes element-wise product, $\\sigma$ and $f$ are the sigmoid and Tanh activation functions, respectively.<\/p>\n\n<h1 id=\"backward-propagation\">Backward Propagation<\/h1>\n<p>Lets rewrite these set of equation in terms of unary and binary operations following the same order.<\/p>\n\n\\[g_1 = W_{zh} \\ast h_{t-1}\\]\n\n\\[g_2 = W_{zx} \\ast x_{t}\\]\n\n\\[g_3 = g_1 + g_2\\]\n\n\\[z_t = \\sigma(g_3)\\]\n\n\\[g_4 = W_{rh} \\ast h_{t-1}\\]\n\n\\[g_5 = W_{rx} \\ast x_{t}\\]\n\n\\[g_6 = g_4 + g_5\\]\n\n\\[r_t = \\sigma(g_6)\\]\n\n\\[g_7 = r_t \\circ h_{t-1}\\]\n\n\\[g_8 = W_{h} \\ast g_7\\]\n\n\\[g_9 = W_{x} \\ast x_{t}\\]\n\n\\[g_{10} = g_8 + g_9\\]\n\n\\[\\tilde{h}_t = f(g_{10})\\]\n\n\\[g_{11} = z_t\\circ h_{t-1}\\]\n\n\\[g_{12} = (1-z_t)\\circ \\tilde{h}_t\\]\n\n\\[h_t = g_{11} + g_{12}\\]\n\n<p>Now, we\u2019\u2018ll work our way backward to compute the parameter gradients, i.e. the derivatives of loss $L$ with respect to parameters. Let assume that gradient of loss with repect to output is known and denoted as $\\Delta_{h_t}L$.<\/p>\n\n<ul>\n  <li>Eq. (20) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{g_{11}}L = \\Delta_{g_{12}}L = \\Delta_{h_t}L\\]\n\n<ul>\n  <li>Eq. (19) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{z_{t}}L = -\\Delta_{g_{12}}L \\circ \\tilde{h}_t\\]\n\n\\[\\Delta_{\\tilde{h}_t}L = \\Delta_{g_{12}}L \\circ (1-z_t)\\]\n\n<ul>\n  <li>Eq. (18) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{z_{t}}L += \\Delta_{g_{11}}L \\circ h_{t-1}\\]\n\n\\[\\Delta_{h_{t-1}}L = \\Delta_{g_{11}}L \\circ z_t\\]\n\n<blockquote>\n  <p>Second time we\u2019re computing a derivative for $z_t$, so we increment the derivative $(+=)$.<\/p>\n<\/blockquote>\n\n<ul>\n  <li>Eq. (17) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{g_{10}}L = \\Delta_{\\tilde{h}_t}L \\circ f'(g_{10})\\]\n\n<ul>\n  <li>Eq. (16) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{g_{8}}L = \\Delta_{g_{9}}L = \\Delta_{g_{10}}L\\]\n\n<ul>\n  <li>Eq. (15) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{W_x}L = \\Delta_{g_9}L \\ast x_t^T\\]\n\n\\[\\Delta_{x_t}L = W_x^T \\ast \\Delta_{g_9}L\\]\n\n<ul>\n  <li>Eq. (14) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{W_h}L = \\Delta_{g_8}L \\ast g_7^T\\]\n\n\\[\\Delta_{g_7}L = W_h^T \\ast \\Delta_{g_8}L\\]\n\n<ul>\n  <li>Eq. (13) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{r_t}L = \\Delta_{g_7}L \\circ h_{t-1}\\]\n\n\\[\\Delta_{h_{t-1}}L += \\Delta_{g_7}L \\circ r_t\\]\n\n<ul>\n  <li>Eq. (12) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{g_6}L = \\Delta_{r_t}L \\circ \\sigma'(g_6)\\]\n\n<ul>\n  <li>Eq. (11) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{g_4}L = \\Delta_{g_5}L = \\Delta_{g_6}L\\]\n\n<ul>\n  <li>Eq. (10) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{W_{rx}}L = \\Delta_{g_5}L \\ast x_t^T\\]\n\n\\[\\Delta_{x_t}L += W_{rx}^T \\ast \\Delta_{g_5}L\\]\n\n<ul>\n  <li>Eq. (9) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{W_{rh}}L = \\Delta_{g_4}L \\ast h_{t-1}^T\\]\n\n\\[\\Delta_{h_{t-1}}L += W_{rh}^T \\ast \\Delta_{g_4}L\\]\n\n<ul>\n  <li>Eq. (8) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{g_3}L = \\Delta_{z_t}L \\circ \\sigma'(g_3)\\]\n\n<ul>\n  <li>Eq. (7) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{g_1}L = \\Delta_{g_2}L = \\Delta_{g_3}L\\]\n\n<ul>\n  <li>Eq. (6) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{W_{zx}}L = \\Delta_{g_2}L \\ast x_t^T\\]\n\n\\[\\Delta_{x_t}L += W_{zx}^T \\ast \\Delta_{g_2}L\\]\n\n<ul>\n  <li>Eq. (5) gives<\/li>\n<\/ul>\n\n\\[\\Delta_{W_{zh}}L = \\Delta_{g_1}L \\ast h_{t-1}^T\\]\n\n\\[\\Delta_{h_{t-1}}L += W_{zh}^T \\ast \\Delta_{g_1}L\\]\n\n<p>This completes all the required formulas for computing derivative with respect to all the parameters of network.<\/p>","author":{"name":"Dr. Shammya Shananda Saha","email":"shammya.saha@gmail.com"},"category":[{"@attributes":{"term":"deep learning"}},{"@attributes":{"term":"RNN"}},{"@attributes":{"term":"derivation"}}],"summary":"In this post, I\u2019ll discuss how to implement a simple Recurrent Neural Network (RNN), specifically the Gated Recurrent Unit (GRU). I\u2019ll present the feed forward proppagation of a GRU Cell at a single time stamp and then derive the formulas for determining parameter gradients using the concept of Backpropagation through time (BPTT)."}}