How to extract embedding vectors after m.fit()

I have fit a model to my data and I have 1 categorical variable that I created embedding vectors for (from Lesson 4 Rossman ipynb). How do I now extract those embedding vectors for each level of the factor variable?

learn.model.embs gets a list of your embeddings. In your case you do:
emb = to_np(learn.model.embs[0].weight.data)
This shall be a number of embeddings x size of embedding sized matrix. It shall be ordered by the categorical index that was passed to it

6 Likes

Thank you! p.s. how did you know that? Is there documentation on the attributes of the learner?

Just looking at the source code:

  • StructuredLearner, and from that
  • MixedInputModel
  • To looking up how to get the embedding matrix from a nn.Embedding objects
2 Likes

This is generally how you get the weights for any pytorch model. The only fastai bits are having to use learn.model to get the actual pytorch model out of the learner object and to_np which just converts the weights from a torch tensor to a numpy array.

4 Likes