What are conditions that cause AttributeError: no attribute 'requires_grad'

mathematics: <p>I was getting this error <strong>AttributeError: ‘int’ object has no attribute ‘requires_grad’</strong> even hit and trialed i’m not getting over it. I want to know the ground truth , of how does this error occur?</p>
<p>What are the list of  undesirable conditions that this type of error happens?</p>
<p>Thank you for your kind help <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> <img src="https://discuss.pytorch.org/images/emoji/apple/heart.png?v=9" title=":heart:" class="emoji" alt=":heart:"></p>

braindotai: <p>Well that error is kind of self explanatory, the code you are running is dealing with a python integer, followed by some pytorch operation on it, which needs its <code>.requires_grad</code> attribute.<br>
Since python integer don’t have this attribute it’s showing you that attribution error.</p>
<p>Could you provide some code? So that we can provide you with actual solutions.</p>

mathematics: <p>oh . Thanks. I was not able to debug it where the error is.<br>
Please check my code. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<details>
<summary>
Summary</summary>
<pre><code class="lang-auto">def train(n_epochs, loaders, model, optimizer, criterion, use_cuda, save_path):
    valid_loss_min = np.Inf 
    for epoch in range(1, n_epochs+1):
        train_loss = 0.0
        valid_loss = 0.0
        model.train()
        for batch_idx, (data, target) in enumerate(loaders['train']):
            optimizer.zero_grad() 
            output = model(data.unsqueeze_(0) )
            loss = criterion(output ,target)
            loss.backward()
            optimizer.step()
            train_loss = train_loss + (1/(batch_idx+1)) + (loss.data - train_loss)
        model.eval()
        for batch_idx, (data, target) in enumerate(loaders['valid']):
            # move to GPU
            if use_cuda:
                data, target = data.cuda(), target.cuda()
            ## update the average validation loss
            output = model(data.unsqueeze_(0) )
            loss = criterion(output, target)
            valid_loss = valid_loss + (1/(batch_idx+1)) + (loss.data - valid_loss)
            
    return model
</code></pre>
</details>

braindotai: <p>Which line is showing that error</p>

mathematics: <p>having error in<br>
<code>loss = criterion(output, target)</code> which does not runs.</p>
<p>but loss previously made is not an integer<br>
here is my criterion</p>
<pre><code class="lang-auto">criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model_scratch.parameters())
</code></pre>

mathematics: <p>Oh problem was solved. What i was doing was</p>
<p>After loading data from <strong>Imagefolder</strong> as<br>
<code>train_data = datasets.ImageFolder('images/train')</code></p>
<p>and directly passing through the for loop enumerate</p>
<pre><code class="lang-auto">for batch, (data, target) in enumerate(train_data):
            output  = model(data, target)
</code></pre>
<p>And find out that target type was <strong>int</strong></p>
<p>So in order to enumerate it I had to use <strong>DataLoader</strong> then use it in for loop .<br>
<code>train_loader = torch.utils.data.DataLoader(train_data,  shuffle=True)</code></p>
<p>Thanks for helping anyway. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>