Loss object has no attribute 'backward'

Just recently I have upgraded my Torch build from 0.1.11 to 0.1.12. Since I have done so, however, I can’t perform a backward pass on a loss object. I get the error: AttributeError: ‘BCELoss’ object has no attribute ‘backward’.

Below is the code I use.

    critBCE = nn.BCEloss()

    for i, (img, lab) in enumerate(source_loader):

        # train the discriminator on source examples
        Yd = Variable(lab)
        X2 = Variable(img)
        output = dis.forward(X2)
        err_real += critBCE.forward(output.float(), Yd.float())
        t = critBCE.backward(output.float(), Yd.float())
        dis.backward(X2, t)

This is the error that is raised:

AttributeError                            Traceback (most recent call last)
<ipython-input-15-e0dbb18c7e0e> in <module>()
     48         output = dis.forward(X2)
     49         err_real += critBCE.forward(output.float(), Yd.float())
---> 50         t = critBCE.backward(output.float(), Yd.float())
     51         dis.backward(X2, t)

/home/daniel/miniconda3/envs/py3/lib/python3.5/site- packages/torch/nn/modules/module.py in __getattr__(self, name) 

AttributeError: 'BCELoss' object has no attribute 'backward'

Anyone has an idea? Does it have to do with the upgrade?

the criterion itself never had backward. If this code worked for you previously, that’s strange.

Also, critBCE.forward is wrong and your overall code is fairly wrong.

Your code should be something of this order:

        output = dis(X2)
        loss = critBCE(output.float(), Yd.float())
        err_real += loss.data[0]
1 Like