Autograd does not appear to be setting gradient: TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

username: mulholio
cooked: <p>Hey there,</p>
<p>Whilst implementing a simple MNIST digit classifier, I’ve got stuck on a bug where <code>grad</code> seems to be set to <code>None</code> after I call <code>loss.backward()</code>. Any ideas how I get this not to be None? What am I missing?</p>
<p>Here’s the error I get:</p>
<pre><code>---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-15-22a0da261727&gt; in &lt;module&gt;
     23 
     24         with torch.no_grad():
---&gt; 25             weights -= weights.grad * LR
     26             bias -= bias * LR
     27 

TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
</code></pre>
<p>If I run the code again, I get a different error, namely:</p>
<pre><code>RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-25-455a55143419&gt; in &lt;module&gt;
      7         predictions = xb@weights + bias
      8         loss = get_loss(predictions, yb)
----&gt; 9         loss.backward()
     10 
     11         with torch.no_grad():

/usr/local/lib/python3.6/dist-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
    116                 products. Defaults to ``False``.
    117         """
--&gt; 118         torch.autograd.backward(self, gradient, retain_graph, create_graph)
    119 
    120     def register_hook(self, hook):

/usr/local/lib/python3.6/dist-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
     91     Variable._execution_engine.run_backward(
     92         tensors, grad_tensors, retain_graph, create_graph,
---&gt; 93         allow_unreachable=True)  # allow_unreachable flag
     94 
     95 

RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.
</code></pre>
<p>And here’s what I think are the relevant parts of my code:</p>
<pre><code># Skipped DataLoader setup for brevity

def get_accuracy(precictions, actual):
    return (precictions &gt;= 0.5).float() == actual

def get_loss(predictions, actual):
    normalised = predictions.sigmoid()
    return torch.where(actual == IS_7, 1 - normalised, normalised).mean()

def init_params(size, variance=1.0):
    return torch.randn(size, dtype=torch.float, requires_grad=True) * variance

weights = init_params((IMG_SIZE, 1))
bias = init_params(1)

for epoch in range(1):
    #  Iterate over dataset batches
    # xb is a tensor with the independent variables for the batch (tensor of pixel values)
    # yb         ""           dependent             ""            (which digit it is)
    for xb, yb in dl:
        print(xb.shape)
        predictions = xb@weights + bias
        loss = get_loss(predictions, yb)
        loss.backward()

        with torch.no_grad():
            weights -= weights.grad * LR # &lt;-- Error here: unsupported operand type(s) for *: 'NoneType' and 'float'
            bias -= bias * LR
        
            weights.grad.zero_()
            bias.grad.zero_()
</code></pre>
<p>Some useful notes:</p>
<ul>
<li>I also tried to use <code>.data</code> instead of <code>with torch.no_grad()</code> but that didn’t help. <code>with</code> seems to be the preferred method from PyTorch (<a href="https://pytorch.org/tutorials/beginner/pytorch_with_examples.html" rel="noopener nofollow ugc">https://pytorch.org/tutorials/beginner/pytorch_with_examples.html</a>)</li>
<li>Calling <code>@</code> for matrix multiplication in the predictions vs <code>torch.mm</code> makes no difference.</li>
<li>I previously made a mistake with my tensor setup but I think that’s all fixed now. <code>weights.shape</code>, <code>bias.shape</code> outputs <code>(torch.Size([784, 1]), torch.Size([1]))</code>
</li>
</ul>

username: mulholio
cooked: <p>Found the fix. The tensor returned by <code>init_params</code> needs to be wrapped in <code>.requires_grad_()</code>, not the individual tensors within it.</p>
<pre><code class="lang-auto">def init_params(size, variance=1.0):
    return (torch.randn(size, dtype=torch.float)*variance).requires_grad_()
</code></pre>

username: Nafiz375
cooked: <p>I’m trying to find my velocity field gradients by using <code>torch.autograd</code>. Initially, I assigned my domain co-ordinates by a function named ‘generate_coords’, then I passed this to ‘compute_gradients’ function to calculate the gradients. My actual_image_inputs_test shape is [30, 2, 32, 32] (30 samples, 2 channels is for both Ux and Uy).</p>
<p><strong>Showing similar types of errors. Any suggestions?</strong></p>
<p>The code I used:</p>
<pre><code class="lang-auto">def generate_coords(height, width, x_max, y_max,batch_size):
    x = torch.linspace(0, x_max, width)
    y = torch.linspace(0, y_max, height)
    xx, yy = torch.meshgrid(x, y, indexing='ij')
    coords = torch.stack([xx, yy], dim=0)
    coords = coords.unsqueeze(0).repeat(batch_size, 1, 1, 1)  
    return coords

def compute_gradients(U, coords):
    U = U.requires_grad_()
    coords = coords.requires_grad_()

    Ux = U[:, 0, :, :]  # Extracting Ux component
    Uy = U[:, 1, :, :]  # Extracting Uy component

    x_coords = coords[:, 0, :, :]  # Extracting x coordinates
    y_coords = coords[:, 1, :, :]  # Extracting y coordinates

    # Computing the spatial gradients
    grad_Ux_x = torch.autograd.grad(outputs=Ux.sum(), inputs=x_coords, grad_outputs=torch.ones_like(Ux), create_graph=True, allow_unused=True)[0]
    grad_Uy_y = torch.autograd.grad(outputs=Uy.sum(), inputs=y_coords, grad_outputs=torch.ones_like(Uy), create_graph=True, allow_unused=True)[0]

    # Computing residuals
    residuals = grad_Ux_x + grad_Uy_y

    return residuals

coords = generate_coords(32, 32, 2520, 504) 
actual_image_inputs_test=torch.tensor(actual_image_inputs_test,requires_grad=True)
grad_Ux_x, grad_Uy_y = compute_gradients(actual_image_inputs_test, coords)
print(grad_Ux_x, grad_Uy_y)
</code></pre>

username: ptrblck
cooked: <p>Your code fails initially with:</p>
<pre data-code-wrap="python"><code class="lang-python">TypeError: generate_coords() missing 1 required positional argument: 'batch_size'
</code></pre>
<p>After setting the <code>batch_size</code> to any value, it fails with:</p>
<pre data-code-wrap="python"><code class="lang-python">NameError: name 'actual_image_inputs_test' is not defined
</code></pre>

username: Nafiz375
cooked: <p>Thanks for your reply. Actually this is one portion of my entire code. After solving the error you mentioned (batch_size=30 and actual_image_inputs_test = torch.randn((30, 2, 32, 32)) , it shows:</p>
<pre><code class="lang-auto">RuntimeError: Mismatch in shape: grad_output[0] has a shape of torch.Size([30, 32, 32]) and output[0] has a shape of torch.Size([]).
</code></pre>
<p>I think gradient is not calculated. How to resolve it ? Any suggestions</p>

username: Nafiz375
cooked: <p>any insights plz suggest</p>