azin
azin

Reputation: 23

Is it possible to get output of embedding keras layer?

I want to use time as an input feature to my deep learning model. So I need to use Embedding layer to convert it to embedded vectors. SO I used:

from keras.layers import Embedding
hours_input=Input(shape=(1,),name='hours_input')
hours_embedding=Embedding(24,64)hours_input

I need the output of Embedding layer (weights I mean). I used hours_embedding.get_weights(). But I got an error: get_weights() missing 1 required positional argument: 'self' So, How can I get embedding weight matrix?

Upvotes: 1

Views: 967

Answers (1)

Aniket Bote
Aniket Bote

Reputation: 3564

Create your model First.

hours_input=Input(shape=(1,),name='hours_input')
hours_embedding=Embedding(24,64)(hours_input)
model = keras.models.Model(inputs = hours_input, outputs = hours_embedding)

And then you can access:

model.layers[1].get_weights()

Output:

[array([[ 0.00782292, -0.03037642, -0.03229956, ..., -0.02188529,
         -0.02597365, -0.04166167],
        [-0.04877049, -0.03961046,  0.01000347, ...,  0.00204592,
          0.01949279, -0.00540505],
        [ 0.0323245 , -0.02847096, -0.0023482 , ...,  0.02859743,
         -0.04320076,  0.01578701],
        ...,
        [ 0.01989252,  0.00970422,  0.00193944, ...,  0.02689132,
         -0.00167314,  0.00353283],
        [ 0.01885528,  0.00589638, -0.03409225, ..., -0.00504225,
          0.01269731,  0.04380948],
        [-0.01756806, -0.00950485, -0.0189078 , ...,  0.023773  ,
         -0.00471363, -0.03708603]], dtype=float32)]

Upvotes: 2

Related Questions