Antonio Sesto
Antonio Sesto

Reputation: 3154

AttributeError: 'Embedding' object has no attribute 'get_shape' with TensorFlow backend

I am trying to understand how Embedding layers work with masking (for sequence to sequence regression).

This simple code fails with the error: AttributeError: 'Embedding' object has no attribute 'get_shape'. It seems to be true, however I don't know how to solve it. Any hint?

import numpy as np
from keras.layers import Input, Dense, LSTM
from keras.layers.embeddings import Embedding
from keras.layers.merge import Concatenate
from keras.models import Model
from keras.utils import plot_model

trainExs = np.asarray([ [1, 2, 3], [2, 3, 1]])
trainLabels = np.asarray([[1, 1, 1], [2, 2, 2]])

print('Examples, shape:', trainExs.shape)
print('Labels, shape:', trainLabels.shape)

W = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]]
symDim = 3

# E M B E D D I N G S
# symbol_in = Input(shape=(None, 1), dtype='float32', name='symbol_input')
symbol_emb = Embedding(symDim+1, symDim,
                       weights=np.asarray(W), trainable=False, input_length=3)

symbol_dense = Dense(symDim, use_bias=True, name='symbol_dense')(symbol_emb)

output_layer = Dense(symDim, dtype='float32', name='output')(symbol_dense)

# M O D E L
model = Model(inputs=[symbol_emb], outputs=[output_layer])
model.compile(loss='mean_squared_error', optimizer='RMSprop', metrics=['accuracy'])
# print(model.summary())

The full stack trace follows:

D:\python\python.exe D:/workspace/TESTS/test/testEMb.py
Using TensorFlow backend.
Examples, shape: (2, 3)
Labels, shape: (2, 3)
Traceback (most recent call last):
  File "D:/workspace/TESTS/test/testEMb.py", line 21, in <module>
    symbol_dense = Dense(symDim, use_bias=True, name='symbol_dense')(symbol_emb)
  File "D:\python\lib\site-packages\keras\engine\topology.py", line 541, in __call__
    self.assert_input_compatibility(inputs)
  File "D:\python\lib\site-packages\keras\engine\topology.py", line 450, in assert_input_compatibility
    ndim = K.ndim(x)
  File "D:\python\lib\site-packages\keras\backend\tensorflow_backend.py", line 479, in ndim
    dims = x.get_shape()._dims
AttributeError: 'Embedding' object has no attribute 'get_shape'

Upvotes: 3

Views: 9623

Answers (1)

Michele Tonutti
Michele Tonutti

Reputation: 4348

You are feeding symbol_emb to your model as an input, but symbol_emb is the name of your Embedding layer and is not a valid input. Define an input such as:

input = Input(shape=input_shape)
symbol_emb = Embedding(symDim+1, symDim,
                       weights=np.asarray(W), trainable=False)(input)

...
...

model = Model(inputs=[input], outputs=[output_layer])

Note that you do not need to define input_length in Embedding this way.

Upvotes: 5

Related Questions