TypeError: 'Tensor' object is not callable'

abebual: <p>Hello,</p>
<p>I got the following error while training my model and got stuck.<br>
<code>TypeError: 'Tensor' object is not callable</code></p>
<p>loaders:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/0/b/0b0ed42ad8ec0911524f55868f9cb1fe6e93acf0.png" alt="loaders" data-base62-sha1="1zP1pcWC15dUKGne9feLYapwoEg" width="643" height="378"></p>
<p>model:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/2/92e8416821e7041eec3de18fb3f82db3a3dc3477.png" data-download-href="https://discuss.pytorch.org/uploads/default/92e8416821e7041eec3de18fb3f82db3a3dc3477" title="model"><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/2/92e8416821e7041eec3de18fb3f82db3a3dc3477.png" alt="model" data-base62-sha1="kXBjzTnktHtLwnGmqHArCtyw7wr" width="419" height="500" data-dominant-color="EEEFEF"><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">model</span><span class="informations">526×627 29.9 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>helper functions to train and validate<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/6/d6d5a701fac080781add56fc946159920314d3b0.png" data-download-href="https://discuss.pytorch.org/uploads/default/d6d5a701fac080781add56fc946159920314d3b0" title="train and validate"><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/6/d6d5a701fac080781add56fc946159920314d3b0.png" alt="train and validate" data-base62-sha1="uEvVswIhUYLCvnZ8xIqTzCb56LK" width="390" height="500" data-dominant-color="ECEDEC"><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">train and validate</span><span class="informations">479×613 27.8 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>training loop and error log:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/9/c93d1fa9bc3d566b7207759331a08ca78ec6965c.png" data-download-href="https://discuss.pytorch.org/uploads/default/c93d1fa9bc3d566b7207759331a08ca78ec6965c" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/9/c93d1fa9bc3d566b7207759331a08ca78ec6965c.png" alt="image" data-base62-sha1="sIeVyXiWbZPRGtlZake5lmp8gs4" width="510" height="500" data-dominant-color="EFF0F0"><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">image</span><span class="informations">620×607 36.7 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>

Nikronic: <p>Hi,</p>
<p>The problem is the way you defined criterion. line 6 in last image or first line of method <code>fit</code>.<br>
You need to pass a class object like <code>criterion = torch.nn.BCELossWithLogits()</code> note that you dont need to pass input/output at the time of definition.</p>
<p>Also it seems you have defined a custom method called <code>binary_cross_entropy</code> for criterion. The problem is from this method.</p>
<p>It would be great if you could share the code using text instead of image and wrapping it around a pair of ``` for better interaction.</p>
<p>Bests</p>

abebual: <aside class="quote no-group" data-username="Nikronic" data-post="2" data-topic="84691">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nikronic/48/71873_2.png" class="avatar"> Nikronic:</div>
<blockquote>
<p><code>BCELossWithLogits()</code></p>
</blockquote>
</aside>
<p>Thank you so much for the swift input. Thank you this seems to address the original type error - <code>tensor object is not callable</code>, now I am getting the error - <code>RuntimeError: The size of tensor a (2) must match the size of tensor b (32) at non-singleton dimension 1</code>.</p>
<p>I used the the following class object;<br>
<span class="hashtag-raw">#code</span> source: <a href="https://github.com/pytorch/pytorch/issues/751" class="inline-onebox" rel="noopener nofollow ugc">Feature Request: Add BCELossWithLogits to replace BCELoss for numerical stability. · Issue #751 · pytorch/pytorch · GitHub</a></p>
<p>class StableBCELoss(nn.modules.Module):</p>
<pre><code>   def __init__(self):

         super(StableBCELoss, self).__init__()

   def forward(self, input, target):

         neg_abs = - input.abs()

         loss = input.clamp(min=0) - input * target + (1 + neg_abs.exp()).log()

         return loss.mean()
</code></pre>
<p>It looks like the issue is continued to be the way criterion is implemented, may not be tensor size/dimension. My codes seems to be infested with bugs, any additional help is highly appreciated. Thank you.</p>

abebual: <p>Here is the codes in text:</p>
<p>Define function to train a batch of IDC images</p>
<pre><code class="lang-auto">def train(trainloader, model, criterion, optimizer, scheduler):
    total_loss = 0.0
    size = len(trainloader.dataset)
    num_batches = size // trainloader.batch_size
    model.train()
    for i, (images, labels) in enumerate(trainloader):
        print(f"Training: {i}/{num_batches}", end="\r")
        
        scheduler.step()
        images = images.to(device)
        labels = labels.to(device)
        optimizer.zero_grad()
        outputs = model(images) # forward pass
        loss = criterion(outputs, labels)
        total_loss += loss.item() * images.size(0)
        loss.backward()  # backprogagation
        optimizer.step()
        
    return total_loss / size
</code></pre>
<p>Define function to compute the accuracy on the validation set</p>
<pre><code class="lang-auto">def validate(valloader, model, criterion):
    model.eval()
    with torch.no_grad():
        total_correct = 0
        total_loss = 0.0
        size = len(valloader.dataset)
        num_batches = size // valloader.batch_size
        for i, (images, labels) in enumerate(valloader):
            print(f"Validation: {i}/{num_batches}", end="\r")
            
            images = images.to(device)
            labels = labels.to(device)
            outputs = model(images)
            loss = criterion(outputs, labels)
            _, preds = torch.max(outputs, 1)
            total_correct += torch.sum(preds == labels.data)
            total_loss += loss.item() * images.size(0)
            
        return total_loss / size, total_correct.double() / size
</code></pre>
<p>main training loop</p>
<pre><code class="lang-auto">target_size=torch.rand((48,48), requires_grad=False)
input_size=torch.rand((48,48), requires_grad=False)

def fit(model, num_epochs, trainloader, valloader):
    #criterion = WeightedBCELoss() 
    criterion = StableBCELoss()
    optimizer = Adagrad(model.parameters(), lr=lr,lr_decay=lr/num_epochs)
    scheduler = OneCycleLR(optimizer, lr_range=(lr,1.), num_steps=1000)
    print("epoch\ttrain loss\tvalid loss\taccuracy")
    for epoch in range(num_epochs):
        train_loss = train(trainloader, model, criterion, optimizer, scheduler)
        valid_loss, valid_acc = validate(valloader, model, criterion)
        print(f"{epoch}\t{train_loss:.5f}\t\t{valid_loss:.5f}\t\t{valid_acc:.3f}")
</code></pre>

Nikronic: <p>There is a built-in function for BCELoss exactly as I written. Do not need to implement it yourself as if you look at the issue it is for 2017 and it is closed. See this thread about it <a href="https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586" class="inline-onebox">BCELoss vs BCEWithLogitsLoss</a>.</p>
<p>About the error, your model’s output has shape <code>[batch_size, 2]</code> and I don’t know about the labels but based on error, your labels have shape <code>[]batch_size, 25]</code>. Can you explain the labels?</p>