I’m following the book code in Colab for Ch13 CNN. Got errors with Learner
generated for simple_cnn
model, firstly introduced in the Creating the CNN section and onwards. The same errors appears when Run the official code provided by fast.ai: Chapter 13, Convolutions, which only proves that it’s not my bad spelling.
I’m using fastai version: 2.1.8 and didn’t help running !pip install fastbook --upgrade
.
This seems to be Ch13 specific as just managed to run the entire code for the following Chapter 14: ResNets. The TypeError
[details below] suggests that torch.nn.functional.cross_entropy
is not happy with fastai.torch_core.TensorImageBW
and/or fastai.torch_core.TensorCategory
.
# Learner for chapter 13
learn = Learner(dls, simple_cnn, loss_func=F.cross_entropy, metrics=accuracy)
learn.model
on it’s own seems to be defined as expected:
Sequential(
(0): Sequential(
(0): Conv2d(1, 4, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
(1): ReLU()
)
(1): Sequential(
(0): Conv2d(4, 8, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
(1): ReLU()
)
(2): Sequential(
(0): Conv2d(8, 16, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
(1): ReLU()
)
(3): Sequential(
(0): Conv2d(16, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
(1): ReLU()
)
(4): Conv2d(32, 2, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
(5): Flatten(full=False)
)
Error details
Error (1):
learn.summary()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-32-bc39e9e85f86> in <module>()
----> 1 learn.summary()
6 frames
/usr/local/lib/python3.6/dist-packages/fastai/callback/hook.py in _print_shapes(o, bs)
163 def _print_shapes(o, bs):
164 if isinstance(o, torch.Size): return ' x '.join([str(bs)] + [str(t) for t in o[1:]])
--> 165 else: return str([_print_shapes(x, bs) for x in o])
166
167 # Cell
TypeError: 'int' object is not iterable
Error (2):
learn.fit_one_cycle(2, 0.01)
epoch train_loss valid_loss accuracy time
0 0.000000 00:00
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-99af2a5e6729> in <module>()
----> 1 learn.fit_one_cycle(2, 0.01)
13 frames
/usr/local/lib/python3.6/dist-packages/torch/overrides.py in handle_torch_function(public_api, relevant_args, *args, **kwargs)
1069 raise TypeError("no implementation found for '{}' on types that implement "
1070 '__torch_function__: {}'
-> 1071 .format(func_name, list(map(type, overloaded_args))))
1072
1073 def has_torch_function(relevant_args: Iterable[Any]) -> bool:
TypeError: no implementation found for 'torch.nn.functional.cross_entropy' on types that implement __torch_function__: [<class 'fastai.torch_core.TensorImageBW'>, <class 'fastai.torch_core.TensorCategory'>]
Error (3):
def fit(epochs=1):
learn = Learner(dls, simple_cnn(), loss_func=F.cross_entropy,
metrics=accuracy, cbs=ActivationStats(with_hist=True))
learn.fit(epochs, 0.06)
return learn
learn = fit()
epoch train_loss valid_loss accuracy time
0 0.000000 00:00
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-50-a7897b19c59c> in <module>()
----> 1 learn = fit()
13 frames
/usr/local/lib/python3.6/dist-packages/torch/overrides.py in handle_torch_function(public_api, relevant_args, *args, **kwargs)
1069 raise TypeError("no implementation found for '{}' on types that implement "
1070 '__torch_function__: {}'
-> 1071 .format(func_name, list(map(type, overloaded_args))))
1072
1073 def has_torch_function(relevant_args: Iterable[Any]) -> bool:
TypeError: no implementation found for 'torch.nn.functional.cross_entropy' on types that implement __torch_function__: [<class 'fastai.torch_core.TensorImageBW'>, <class 'fastai.torch_core.TensorCategory'>]