Chapter 6 - error when running in Colab

When I try to run Chapter 6 on Colab: Google Colab

For the following line of code:

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

I get the following error:

TypeError                                 Traceback (most recent call last)

<ipython-input-27-38b1530800fe> in <cell line: 2>()
      1 loss_func = nn.BCEWithLogitsLoss()
----> 2 loss = loss_func(activs, y)
      3 loss

4 frames

/usr/local/lib/python3.10/dist-packages/torch/overrides.py in handle_torch_function(public_api, relevant_args, *args, **kwargs)
   1657     if _is_torch_function_mode_enabled():
   1658         msg += f" nor in mode {_get_current_function_mode()}"
-> 1659     raise TypeError(msg)
   1660 
   1661 has_torch_function = _add_docstr(

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'>]

I ran the Colab notebook and got the same error as you. activs is a TensorImage which is causing the nn.BCEWithLogitsLoss error:

Whereas in the fastbook, activs is a TensorBase object.

I installed an older version of fastai (2.6.0) which dates back to about when the 06_multicat.ipynb notebook was published and for that version, learn.model(x) returns a TensorBase object, so something must have changed since then in how learn.model returns data.

As a workaround, I converted activs to a TensorBase object so that i can pass it to the loss function:

image