Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KERAS]Embedding layer #5444

Merged
merged 1 commit into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion python/tvm/relay/frontend/keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ def _convert_permute(inexpr, keras_layer, _):
return _op.transpose(inexpr, axes=(0,) + keras_layer.dims)


def _convert_embedding(inexpr, keras_layer, etab):
indices = inexpr
weightList = keras_layer.get_weights()
weight = etab.new_const(weightList[0])
out = _op.take(weight, indices.astype('int32'), axis=0)

return out

def _convert_dense(inexpr, keras_layer, etab):
weightList = keras_layer.get_weights()
weight = etab.new_const(weightList[0].transpose([1, 0]))
Expand Down Expand Up @@ -893,7 +901,7 @@ def _default_skip(inexpr, keras_layer, _): # pylint: disable=unused-argument
'Maximum' : _convert_merge,
'Dot' : _convert_merge,
'Permute' : _convert_permute,
# 'Embedding' : _convert_embedding,
'Embedding' : _convert_embedding,
# 'RepeatVector' : _convert_repeat_vector,

'InputLayer' : _default_skip,
Expand Down
20 changes: 19 additions & 1 deletion tests/python/frontend/keras/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,24 @@ def test_forward_zero_padding3d(self, keras):
keras_model = keras.models.Model(data, x)
verify_keras_frontend(keras_model, layout='NDHWC')


def test_forward_embedding(self, keras):
data = keras.layers.Input(shape=(2, 4), dtype="int32")
x = keras.layers.Embedding(10, 3)(data)
keras_model = keras.models.Model(data, x)
verify_keras_frontend(keras_model, need_transpose=False)

data = keras.layers.Input(shape=(2, 3, 4), dtype="int32")
x = keras.layers.Embedding(4, 5)(data)
keras_model = keras.models.Model(data, x)
verify_keras_frontend(keras_model, need_transpose=False)

data = keras.layers.Input(shape=(6, 2, 3, 4), dtype="int32")
x = keras.layers.Embedding(4, 5)(data)
keras_model = keras.models.Model(data, x)
verify_keras_frontend(keras_model, need_transpose=False)


if __name__ == '__main__':
for k in [keras, tf_keras]:
sut = TestKeras()
Expand Down Expand Up @@ -497,4 +515,4 @@ def test_forward_zero_padding3d(self, keras):
sut.test_forward_pool3d(keras=k)
sut.test_forward_upsample3d(keras=k)
sut.test_forward_zero_padding3d(keras=k)

sut.test_forward_embedding(keras=k)