From 40e7caf667a23880bec13922978cf05dce939a10 Mon Sep 17 00:00:00 2001 From: QI JUN Date: Mon, 23 Oct 2017 12:44:17 -0700 Subject: [PATCH] ensure ids in lookup table op must be a column vector (#4987) * ensure ids in lookup table op must be a column vector * follow comments --- paddle/operators/lookup_table_op.cc | 7 ++++++- python/paddle/v2/framework/tests/test_lookup_table_op.py | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/paddle/operators/lookup_table_op.cc b/paddle/operators/lookup_table_op.cc index b88cd14d78f61..ad86a2e5bc23b 100644 --- a/paddle/operators/lookup_table_op.cc +++ b/paddle/operators/lookup_table_op.cc @@ -32,6 +32,9 @@ class LookupTableOp : public framework::OperatorWithKernel { auto table_dims = ctx->GetInputDim("W"); auto ids_dims = ctx->GetInputDim("Ids"); + PADDLE_ENFORCE_EQ(ids_dims.size(), 2); + PADDLE_ENFORCE_EQ(ids_dims[1], 1); + ctx->SetOutputDim("Out", {ids_dims[0], table_dims[1]}); ctx->ShareLoD("Ids", /*->*/ "Out"); } @@ -53,7 +56,9 @@ class LookupTableOpMaker : public framework::OpProtoAndCheckerMaker { " which is a learnable parameter."); AddInput("Ids", "An input with type int32 or int64" - "contains the ids to be looked up in W."); + "contains the ids to be looked up in W." + "Ids must be a column vector with rank = 2." + "The 2nd dimension size must be 1"); AddOutput("Out", "The lookup results, which have the same type with W."); AddComment(R"DOC( This operator is used to perform lookups on the parameter W, diff --git a/python/paddle/v2/framework/tests/test_lookup_table_op.py b/python/paddle/v2/framework/tests/test_lookup_table_op.py index b259bb67e832a..2c48f9bf93b93 100644 --- a/python/paddle/v2/framework/tests/test_lookup_table_op.py +++ b/python/paddle/v2/framework/tests/test_lookup_table_op.py @@ -8,7 +8,8 @@ def setUp(self): self.op_type = "lookup_table" table = np.random.random((17, 31)).astype("float32") ids = np.random.randint(0, 17, 4).astype("int32") - self.inputs = {'W': table, 'Ids': ids} + ids_expand = np.expand_dims(ids, axis=1) + self.inputs = {'W': table, 'Ids': ids_expand} self.outputs = {'Out': table[ids]} def test_check_output(self):