TypeError: 'Tensor' object is not callable

Shantanu_Nath: <p>I don’t know what is actual error. Why is it showin:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/6/5668c28a7b76ad90a54acaaeb32bde0b052ec359.png" data-download-href="https://discuss.pytorch.org/uploads/default/5668c28a7b76ad90a54acaaeb32bde0b052ec359" title="Capture"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/5668c28a7b76ad90a54acaaeb32bde0b052ec359_2_690x170.png" alt="Capture" data-base62-sha1="ckpvALn8kIRHTIf0z47JhrrEf1f" width="690" height="170" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/5668c28a7b76ad90a54acaaeb32bde0b052ec359_2_690x170.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/5668c28a7b76ad90a54acaaeb32bde0b052ec359_2_1035x255.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/6/5668c28a7b76ad90a54acaaeb32bde0b052ec359_2_1380x340.png 2x" data-dominant-color="272B31"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Capture</span><span class="informations">1468×362 39.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Here is My Script:</p>
<pre><code class="lang-auto">def train_one_epoch(epoch, model, optimizer,loss, train_loader, device, train_data):
    print('Training')
    model.train()
    train_running_loss = 0.0
    train_running_correct = 0
    for i, data in tqdm(enumerate(train_loader), total=int(len(train_data)/train_loader.batch_size)):
        data, target = data[0].to(device), data[1].to(device)
        optimizer.zero_grad()
        outputs = model(data)
        print(target.shape, outputs.shape)
        loss = loss(outputs, target)
        train_running_loss += loss.item()
        _, preds = torch.max(outputs.data, 1)
        train_running_correct += (preds == target).sum().item()
        loss.backward()
        optimizer.step()

    train_loss = train_running_loss/len(train_loader.dataset)
    train_accuracy = 100. * train_running_correct/len(train_loader.dataset)    
    return train_loss, train_accuracy

for fold, (trn_idx, val_idx) in enumerate(folds):
        if fold&gt;0:
            break
        print('Training with {} started'.format(fold))

        print(len(trn_idx), len(val_idx))
        train_loader, val_loader = prepare_dataloader(train, trn_idx, val_idx, data_root=TRAIN_PATH)

        device = torch.device(CFG['device'])
        
        model = CassvaImgClassifier(CFG['model_arch'], train.label.nunique(), pretrained=True).to(device)
        scaler = GradScaler()   
        optimizer = torch.optim.Adam(model.parameters(), lr=CFG['lr'], weight_decay=CFG['weight_decay'])
        #scheduler = torch.optim.lr_scheduler.StepLR(optimizer, gamma=0.1, step_size=CFG['epochs']-1)
        scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer, T_0=CFG['T_0'], T_mult=1, eta_min=CFG['min_lr'], last_epoch=-1)
        #scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer=optimizer, pct_start=0.1, div_factor=25, 
        #                                                max_lr=CFG['lr'], epochs=CFG['epochs'], steps_per_epoch=len(train_loader))
        
        loss_tr = torch.nn.CrossEntropyLoss()#MyCrossEntropyLoss().to(device)
        loss_fn = torch.nn.CrossEntropyLoss()
        
        valdition_loss = 99999
        for epoch in range(CFG['epochs']):
            
            train_loss , train_auc = train_one_epoch(epoch, model, optimizer, loss_tr, train_loader, device,trn_idx)
            
            val_loss, val_auc = valid_one_epoch(epoch, model, optimizer, loss_fn, val_loader, device, val_idx)
            print(f"Train Loss: {train_loss:.4f}, Train Acc: {train_auc:.2f}")
            print(f'Val Loss: {val_loss:.4f}, Val Acc: {val_auc:.2f}')
            if val_loss&lt;validation_loss:
                torch.save(model.state_dict(),'{}_fold_{}_{}'.format(CFG['model_arch'], fold, epoch))
</code></pre>

ptrblck: <p>You are overriding the <code>loss</code> function with the <code>loss</code> tensor.<br>
Change the name of either the function (to e.g. <code>criterion</code>) or the tensor (to e.g. <code>loss_val</code>).</p>

Shantanu_Nath: <p>Thanks for immediate response.<br>
I have changed</p>
<pre><code class="lang-auto">loss_tr = torch.nn.CrossEntropyLoss()#MyCrossEntropyLoss().to(device)
loss_fn = torch.nn.CrossEntropyLoss()
</code></pre>
<p>to</p>
<pre><code class="lang-auto">loss = torch.nn.CrossEntropyLoss()
</code></pre>
<p>But it still shows this error</p>

ptrblck: <p>This line of code is creating the issue:</p>
<pre><code class="lang-python">loss = loss(outputs, target)
</code></pre>
<p>since <code>loss</code> is passed as an argument to <code>train_one_epoch</code>.<br>
Change the loss function object name to <code>criterion</code> in <code>train_one_epoch</code> and it should work.</p>

AyaELS: <p>Hi, I am getting the same error from this code</p>
<pre><code class="lang-auto">def _compute_gradients(self, loss, var_list, grad_loss=None):
    with backprop.GradientTape() as tape:
      if not callable(var_list):
        tape.watch(var_list)
      loss_value = loss()          ##Error here
    if callable(var_list):
      var_list = var_list()
    var_list = nest.flatten(var_list)
    with backend.name_scope(self._name + "/gradients"):
      grads = tape.gradient(loss_value, var_list, grad_loss)

      if hasattr(self, "clipnorm"):
        grads = [clip_ops.clip_by_norm(g, self.clipnorm) for g in grads]
      if hasattr(self, "clipvalue"):
        grads = [
            clip_ops.clip_by_value(g, -self.clipvalue, self.clipvalue)
            for g in grads
        ]

    grads_and_vars = list(zip(grads, var_list))
    self._assert_valid_dtypes([
        v for g, v in grads_and_vars
        if g is not None and v.dtype != dtypes.resource
    ])

    return grads_and_vars
</code></pre>
<p>I think I don’t have the same problem as I don’t have the same name for the tensor and the function.<br>
Any clue please?</p>

ptrblck: <p>Based on the code snippet it seems you are using TensorFlow so I would recommend posting this question in their discussion board.</p>

zy258369: <p>Hello，I meet the same problem, how do you solve it finally?</p>