n-waves / multifit

The code to reproduce results from paper "MultiFiT: Efficient Multi-lingual Language Model Fine-tuning" https://arxiv.org/abs/1909.04761
MIT License
284 stars 56 forks source link

Get activations of a specific layer of the multifit model #72

Open francis967 opened 4 years ago

francis967 commented 4 years ago

Hello everybody, Anyone knows how to get the activations of an intermediate layer when I make a prediction, despite what I have tried so far, it gives me errors.To give you a little context:

The first thing I do is extract the model layer(for example the first embeddings layer): self.specific_layer = list(self.classifier.model.modules())[0][0].module.encoder

The second thing I do is put a hook on the layer I want to get the activations from : def hook_function(module, grad_in, grad_out): self.gradients = grad_out[0] self.specific_layer.register_backward_hook(hook_function)

Then I vectorize the text input using SentencePiece and give it as input to the model like this, similar to how I've seen it done in other fastai models: model_output = self.classifier.model(self.inputs)

But when i try to run the above code line it gives me this error: File "/home/francis/.virtualenvs/my_project/lib/python3.7/site-packages/fastai/text/learner.py", line 261, in forward bs,sl = input.size() AttributeError: 'list' object has no attribute 'size' Does anyone know what the problem might be?, or if there is a better way to get the activations of an intermediate layer in a multifit model.

Thanks