简体   繁体   中英

nn.CrossEntropyLoss() function results in torch.FloatTensor has no 'requires_gradient' attribute error

I am using Resnet18 pre-trained model. So, basically, I take the "output" of the model and then call CrossEntropyLoss() . Let the output of the model be "output" and "labels" is the class label. So, my CrossEntropyLoss(output,labels) is called. I checked the type of "output", it is <tensor.autograde.variable.Variable> . I tried different combinations with "labels" also. First, made it as a numpy array and then a variable. But nothing seems to work. I am using pytorch 0.3.1 . Please refrain yourself from suggesting to upgrade the pytorch as it can't possibly be done in my current situation. I have also attached the error stack. However, it seems to work in version 0.4.0.

Criterion function is the CrossEntropyLoss function.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-76-6d5f48373efd> in <module>()
      5 
      6 # Train and evaluate
----> 7 model_ft, hist = train_model(model_ft, data, criterion, optimizer_ft, num_epochs=num_epochs, is_inception=(model_name=="inception"))

<ipython-input-70-bfd03f976e97> in train_model(model, dataloaders, criterion, optimizer, num_epochs, is_inception)
     47                 labels=(torch.from_numpy(np.array([labels])))
     48                 #print(((outputs.requires_gradient)))
---> 49                 loss = criterion(outputs, labels)  ##calculate entropy loss
     50 
     51                 _, preds = torch.max(outputs, 1)

/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    355             result = self._slow_forward(*input, **kwargs)
    356         else:
--> 357             result = self.forward(*input, **kwargs)
    358         for hook in self._forward_hooks.values():
    359             hook_result = hook(self, input, result)

/usr/local/lib/python3.5/dist-packages/torch/nn/modules/loss.py in forward(self, input, target)
    675 
    676     def forward(self, input, target):
--> 677         _assert_no_grad(target)
    678         return F.cross_entropy(input, target, self.weight, self.size_average,
    679                                self.ignore_index, self.reduce)

/usr/local/lib/python3.5/dist-packages/torch/nn/modules/loss.py in _assert_no_grad(variable)
      8 
      9 def _assert_no_grad(variable):
---> 10     assert not variable.requires_grad, \
     11         "nn criterions don't compute the gradient w.r.t. targets - please " \
     12         "mark these variables as volatile or not requiring gradients"

AttributeError: 'torch.LongTensor' object has no attribute 'requires_grad'

My code:

val_acc_history = []      

best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0

for epoch in range(num_epochs):
    print('Epoch {}/{}'.format(epoch, num_epochs - 1))
    print('-' * 10) 

    # Each epoch has a training and validation phase
    for phase in ['train', 'val']:
        if phase == 'train':
            model.train()  # Set model to training mode
        else:
            model.eval()   # Set model to evaluate mode

        running_loss = 0.0
        running_corrects = 0

        # Iterate over data.
        count=0
        for inputs, labels in dataloaders[phase]:

            # zero the parameter gradients
            optimizer.zero_grad() 


            outputs = model(inputs.unsqueeze(0))  ###input to the model and output porduced
            labels=(torch.from_numpy(np.array([labels])))
            loss = criterion(outputs, labels)  ##calculate entropy loss

            _, preds = torch.max(outputs, 1) 

            # backward + optimize only if in training phase
            if phase == 'train':
                loss.backward()    ### loss gradient going backward
                optimizer.step()    ### Optimizer performs parameter update based on current gradient

            # statistics
            running_loss += loss.item() * inputs.size(0)    
            running_corrects += torch.sum(preds == labels.data)
            count=count+1

        epoch_loss = running_loss / count
        epoch_acc = running_corrects.double() / count

        print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc))

        # deep copy the model
        if phase == 'val' and epoch_acc > best_acc:
            best_acc = epoch_acc
            best_model_wts = copy.deepcopy(model.state_dict())
        if phase == 'val':
            val_acc_history.append(epoch_acc)

    #print()

time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(best_acc))

# load best model weights
model.load_state_dict(best_model_wts)
return model, val_acc_history

'''

It seems like tensors in version v0.3.1 didn't support the requires_grad flag (try searching for it). I suggest upgrading PyTorch if possible and it should work ( see requires_grad in latest version ) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM