Search code examples
tensorflowplotkerasembedding

Keras Embedding Layer and CNN: How do I get the trained vectors from the model


I used a neural network which contains an embedding layer.

I converted my datset, which consists out of multiple files that contains sentences, with a Tokenizer to vectors and fed them to the network as training input. After the embedding layer I got a CNN. How can I know get the trained input vectors back for plotting purposes?


Solution

  • It is easy, try:

    for layer in my_model.layers:
        weights = layer.get_weights()
        configs = layer.get_config()
    

    You'll have to do some parsing in the configs to retrieve the embedding layer. You have the 'name' that you can use to identify and retrieve your layers