Thanks for your example code. However it is causing a Type error when I run this code:
layer = list(learn.model.modules())[-4]
activation = []
def get_activation(name):
def hook(model, input, output):
activation[name] = output.detach()
return hook
hook = layer.register_forward_hook(get_activation('act_output'))
row = df.iloc[0]
learn.predict(row)
I get this error:
TypeError Traceback (most recent call last)
in
1 row = df.iloc[0]
----> 2 learn.predict(row)
~/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/basic_train.py in predict(self, item, **kwargs)
360 “Return predicted class, label and probabilities for item
.”
361 batch = self.data.one_item(item)
–> 362 res = self.pred_batch(batch=batch)
363 pred,x = res[0],batch[0]
364 norm = getattr(self.data,‘norm’,False)
~/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/basic_train.py in pred_batch(self, ds_type, batch, reconstruct)
340 cb_handler = CallbackHandler(self.callbacks)
341 xb,yb = cb_handler.on_batch_begin(xb,yb, train=False)
–> 342 preds = loss_batch(self.model.eval(), xb, yb, cb_handler=cb_handler)
343 res = _loss_func2activ(self.loss_func)(preds[0])
344 if not reconstruct: return res
~/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/basic_train.py in loss_batch(model, xb, yb, loss_func, opt, cb_handler)
23 if not is_listy(xb): xb = [xb]
24 if not is_listy(yb): yb = [yb]
—> 25 out = model(*xb)
26 out = cb_handler.on_loss_begin(out)
27
~/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
–> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)
~/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/tabular/models.py in forward(self, x_cat, x_cont)
35 x_cont = self.bn_cont(x_cont)
36 x = torch.cat([x, x_cont], 1) if self.n_emb != 0 else x_cont
—> 37 x = self.layers(x)
38 if self.y_range is not None:
39 x = (self.y_range[1]-self.y_range[0]) * torch.sigmoid(x) + self.y_range[0]
~/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
–> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)
~/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
90 def forward(self, input):
91 for module in self._modules.values():
—> 92 input = module(input)
93 return input
94
~/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
–> 491 hook_result = hook(self, input, result)
492 if hook_result is not None:
493 raise RuntimeError(
TypeError: ‘NoneType’ object is not callable
The last two lines do a correct prediction if I don’t run the code you suggested, so not sure where I am going wrong here.