fastai / fastbook

The fastai book, published as Jupyter Notebooks
Other
22.03k stars 8.49k forks source link

Chapter 6: TypeError in Binary Cross Entropy calculation using both nn.BCEWithLogitsLoss() and F.binary_cross_entropy_with_logits() #627

Open abidhasan03 opened 1 year ago

abidhasan03 commented 1 year ago

In the Chapter 6 notebook: 06_multicat.ipynb, The binary cross entropy calculation using either the nn module or F function gives "TypeError"

Executing this cell,

loss_func = nn.BCEWithLogitsLoss()     
loss = loss_func(activs, y)     
loss

Gives, TypeError: no implementation found for 'torch.nn.functional.binary_cross_entropy_with_logits' on types that implement __torch_function__: [<class 'fastai.torch_core.TensorImage'>, <class 'fastai.torch_core.TensorMultiCategory'>]

Furthermore, trying to calculate the same loss using function seems to run into the same problem too.

loss = F.binary_cross_entropy_with_logits(activs, y)
loss

TypeError: no implementation found for 'torch.nn.functional.binary_cross_entropy_with_logits' on types that implement __torch_function__: [<class 'fastai.torch_core.TensorImage'>, <class 'fastai.torch_core.TensorMultiCategory'>]

The issue persisted in both CPU and GPU runtime.

Udayk02 commented 11 months ago

The issue says that the F.binary_cross_entropy_with_logits does not work on TensorImage and also it will not work on TensorMask. So, you need to change those to torch tensors to make them work. You can wrap them with TensorBase() and you are good to go.

lavafroth commented 2 months ago
loss_func = nn.BCEWithLogitsLoss()     
loss = loss_func(activs, Tensor(y))     
loss