<p dir="auto">The README dynamic's graph gif ( e.g <a href="https://github.com/pytorch/pytorch/blob/master/docs/source/_static/img/dynamic_graph.gif">dynamic graph</a> ) is still in 0.3 mode and has Variable all over. Considering the recent release, would make sense to update it.</p>
<p dir="auto">This GIF (see below) needs to be tweaked a bit:</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://raw.githubusercontent.com/pytorch/pytorch/master/docs/source/_static/img/dynamic_graph.gif"><img src="https://raw.githubusercontent.com/pytorch/pytorch/master/docs/source/_static/img/dynamic_graph.gif" alt="GIF" data-animated-image="" style="max-width: 100%;"></a></p>
<p dir="auto">When I run this code, the first error is:</p>
<blockquote>
<p dir="auto">RuntimeError: element 0 of variables does not require grad and does not have a grad_fn</p>
</blockquote>
<p dir="auto">Looking at the direction of arrows and assuming that we need to calculate the gradient with respect to three variables (prev_h, W_h, W_x), setting <code class="notranslate">requires_grad=True</code> for all four variables raises another error:</p>
<blockquote>
<p dir="auto">RuntimeError: output and gradOutput shapes do not match: output [20 x 1], gradOutput [1 x 20]</p>
</blockquote>
<p dir="auto">So I changed the <code class="notranslate">next_h.backward</code> to <code class="notranslate">next_h.backward(torch.ones(20, 1))</code>. So now the code is:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import torch
from torch.autograd import Variable

x = Variable(torch.randn(1, 10))
prev_h = Variable(torch.randn(1, 20), requires_grad=True)
W_h = Variable(torch.randn(20, 20), requires_grad=True)
W_x = Variable(torch.randn(20, 10), requires_grad=True)

i2h = torch.mm(W_x, x.t())
h2h = torch.mm(W_h, prev_h.t())
next_h = i2h + h2h
next_h = next_h.tanh()

next_h.backward(torch.ones(20, 1))"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">torch</span>
<span class="pl-k">from</span> <span class="pl-s1">torch</span>.<span class="pl-s1">autograd</span> <span class="pl-k">import</span> <span class="pl-v">Variable</span>

<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-v">Variable</span>(<span class="pl-s1">torch</span>.<span class="pl-en">randn</span>(<span class="pl-c1">1</span>, <span class="pl-c1">10</span>))
<span class="pl-s1">prev_h</span> <span class="pl-c1">=</span> <span class="pl-v">Variable</span>(<span class="pl-s1">torch</span>.<span class="pl-en">randn</span>(<span class="pl-c1">1</span>, <span class="pl-c1">20</span>), <span class="pl-s1">requires_grad</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-v">W_h</span> <span class="pl-c1">=</span> <span class="pl-v">Variable</span>(<span class="pl-s1">torch</span>.<span class="pl-en">randn</span>(<span class="pl-c1">20</span>, <span class="pl-c1">20</span>), <span class="pl-s1">requires_grad</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-v">W_x</span> <span class="pl-c1">=</span> <span class="pl-v">Variable</span>(<span class="pl-s1">torch</span>.<span class="pl-en">randn</span>(<span class="pl-c1">20</span>, <span class="pl-c1">10</span>), <span class="pl-s1">requires_grad</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)

<span class="pl-s1">i2h</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-en">mm</span>(<span class="pl-v">W_x</span>, <span class="pl-s1">x</span>.<span class="pl-en">t</span>())
<span class="pl-s1">h2h</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-en">mm</span>(<span class="pl-v">W_h</span>, <span class="pl-s1">prev_h</span>.<span class="pl-en">t</span>())
<span class="pl-s1">next_h</span> <span class="pl-c1">=</span> <span class="pl-s1">i2h</span> <span class="pl-c1">+</span> <span class="pl-s1">h2h</span>
<span class="pl-s1">next_h</span> <span class="pl-c1">=</span> <span class="pl-s1">next_h</span>.<span class="pl-en">tanh</span>()

<span class="pl-s1">next_h</span>.<span class="pl-en">backward</span>(<span class="pl-s1">torch</span>.<span class="pl-en">ones</span>(<span class="pl-c1">20</span>, <span class="pl-c1">1</span>))</pre></div>
<p dir="auto">I am submitting this issue because I don't want this GIF to mislead beginners when learning PyTorch.</p>