# How can I get .grad ? .grad is None

**URL:** <https://discuss.pytorch.org/t/how-can-i-get-grad-grad-is-none/77337>\
**Category:** autograd\
**Created:** [April 18, 2020, 3:59pm UTC](https://discuss.pytorch.org/t/how-can-i-get-grad-grad-is-none/77337 "2020-04-18T15:59:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yuki\_Takezawa](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yuki_takezawa/32/12357_2.png) [@Yuki\_Takezawa](https://discuss.pytorch.org/u/Yuki_Takezawa)\
**Post date:** [April 18, 2020, 3:59pm UTC](https://discuss.pytorch.org/t/how-can-i-get-grad-grad-is-none/77337/1 "2020-04-18T15:59:00Z")

</div>

I want to get d(model(x))/dx, but following program returns None.

```auto
class Net(nn.Module):
    def __init__ (self):
        super(Net, self). __init__ ()
        self.conv1 = nn.Conv2d(1, 32, 3) # 28x28x32 -> 26x26x32
        self.conv2 = nn.Conv2d(32, 64, 3) # 26x26x64 -> 24x24x64 
        self.pool = nn.MaxPool2d(2, 2) # 24x24x64 -> 12x12x64
        self.dropout1 = nn.Dropout2d()
        self.fc1 = nn.Linear(12 * 12 * 64, 128)
        self.dropout2 = nn.Dropout2d()
        self.fc2 = nn.Linear(128, 1)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.dropout1(x)
        x = x.view(-1, 12 * 12 * 64)
        x = F.relu(self.fc1(x))
        x = self.dropout2(x)
        x = self.fc2(x)
        return x

```

```auto
model = Net()
input = torch.randn((1, 1, 28, 28), requires_grad=True)
y = model(input)
y.backward()
print(x.grad) # This is None.

```

How can I get this grad ?

---

<div class="post-metadata">

**Author:** ![kshitij](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kshitij/32/50811_2.png) [@kshitij](https://discuss.pytorch.org/u/kshitij)\
**Post date:** [April 18, 2020, 5:36pm UTC](https://discuss.pytorch.org/t/how-can-i-get-grad-grad-is-none/77337/2 "2020-04-18T17:36:49Z")

</div>

Should that `print(x.grad)` be `print(input.grad)`?

I tried the following and it worked.

```python
import torch
import torch.nn as nn
from torch.nn import functional as F

class Net(nn.Module):
    def __init__ (self):
        super(Net, self). __init__ ()
        self.conv1 = nn.Conv2d(1, 32, 3) # 28x28x32 -> 26x26x32
        self.conv2 = nn.Conv2d(32, 64, 3) # 26x26x64 -> 24x24x64 
        self.pool = nn.MaxPool2d(2, 2) # 24x24x64 -> 12x12x64
        self.dropout1 = nn.Dropout2d()
        self.fc1 = nn.Linear(12 * 12 * 64, 128)
        self.dropout2 = nn.Dropout2d()
        self.fc2 = nn.Linear(128, 1)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.dropout1(x)
        x = x.view(-1, 12 * 12 * 64)
        x = F.relu(self.fc1(x))
        x = self.dropout2(x)
        x = self.fc2(x)
        return x

model = Net()
input = torch.randn((1, 1, 28, 28), requires_grad=True)
y = model(input)
y.backward()
print(input.grad)

```

---

<div class="post-metadata">

**Author:** ![Yuki\_Takezawa](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yuki_takezawa/32/12357_2.png) [@Yuki\_Takezawa](https://discuss.pytorch.org/u/Yuki_Takezawa)\
**Post date:** [April 19, 2020, 2:49am UTC](https://discuss.pytorch.org/t/how-can-i-get-grad-grad-is-none/77337/3 "2020-04-19T02:49:21Z")

</div>

Sorry, It’s stupid mistake.  
Thank you.
