TypeError: 'int' object is not callable in loss.backward()
While trying to set up a pytorch model, I am getting the error that the loss object is not callable when trying to do Pytorch autograd. (Relevant code shown below)

optimizer = torch.optim.Adam(model.parameters(), lr=lr, 
  betas(0.0,0.9))

def train(epoch, shuffle, wisdom_model, optim, loss):
    print('train')
    accuracy = 0
    batch_num = 0
    wisdom_model.train()
    for batch in data.train_dl:

        optim.zero_grad()

        result = model(batch[0])
        loss = nn.CrossEntropyLoss()(result, batch[1].long())

        loss.backward()

        accuracy += accuracy(result, batch[1])
        print(accuracy)
        pdb.set_trace()
        batch_num += 1

    return accuracy / batch_num


TypeError                                 Traceback (most recent call last)
&lt;ipython-input-28-5b9c9fe3b320&gt; in &lt;module&gt;
----&gt; 1 run(1, False)

&lt;ipython-input-27-d0d67dbf6eb2&gt; in run(num_models, dropout)
     71 
     72     for epoch in range(10):
---&gt; 73         train_accuracy = train(epoch, False, model, optimizer, loss)
     74         accuracy.append(validate(epoch, model))
     75 

&lt;ipython-input-27-d0d67dbf6eb2&gt; in train(epoch, shuffle, model, optim, loss)
     24         pdb.set_trace()
     25 
---&gt; 26         loss.backward()
     27         optim.step()
     28 

TypeError: 'int' object is not callable


The problem is in this line:

loss = nn.CrossEntropyLoss()(result, batch[1].long())


Check out the nn.CrossEntropyLoss.

Should not look like this:

nn.CrossEntropyLoss()()


Should look like this:

nn.CrossEntropyLoss()

