From 1443d762fd232203de2e4e512ab7db161983c4c6 Mon Sep 17 00:00:00 2001 From: Yibing Liu Date: Sun, 19 Aug 2018 14:52:34 +0000 Subject: [PATCH 1/6] Wrap unsqueeze & squeeze ops --- python/paddle/fluid/layers/nn.py | 82 +++++++++++++++++++ .../fluid/tests/unittests/test_layers.py | 17 ++++ 2 files changed, 99 insertions(+) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 66b776c08e..64864c267d 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -84,6 +84,8 @@ __all__ = [ 'one_hot', 'autoincreased_step_counter', 'reshape', + 'squeeze', + 'unsqueeze', 'lod_reset', 'lrn', 'pad', @@ -4483,6 +4485,86 @@ def reshape(x, shape, actual_shape=None, act=None, inplace=True, name=None): return helper.append_activation(out) +def squeeze(x, axes, inplace=False, name=None): + """ + Remove single-dimensional entries from the shape of a tensor. Takes a + parameter axes with a list of axes to squeeze. If axes is not provided, all + the single dimensions will be removed from the shape. If an axis is + selected with shape entry not equal to one, an error is raised. + + Examples: + Case 1: + Given + X.shape = (1, 3, 1, 5) + and + axes = [0] + we get: + Out.shape = (3, 1, 5) + Case 2: + Given + X.shape = (1, 3, 1, 5) + and + axes = [] + we get: + Out.shape = (3, 5) + + Args: + x (Variable): The input variable to be squeezed. + axes (list): List of integers, indicating the dimensions to be squeezed. + name (str): Name for this layers. + + Returns: + Variable: Output squeezed variable. + + Examples: + .. code-block:: python + + x = layers.data(name='x', shape=[5, 1, 10]) + y = layers.sequeeze(x, axes=[1]) + """ + helper = LayerHelper("squeeze", **locals()) + out = helper.create_tmp_variable(dtype=x.dtype) + helper.append_op( + type="squeeze", + inputs={"X": x}, + attrs={"axes": axes}, + outputs={"Out": out}) + + return out + +def unsqueeze(x, axes, inplace=False, name=None): + """ + Insert single-dimensional entries to the shape of a tensor. Takes one + required argument axes, a list of dimensions that will be inserted. + Dimension indices in axes are as seen in the output tensor. + + For example: + Given a tensor such that tensor with shape [3, 4, 5], + then Unsqueezed tensor with axes=[0, 4] has shape [1, 3, 4, 5, 1]. + + Args: + x (Variable): The input variable to be unsqueezed. + axes (list): List of integers, indicating the dimensions to be inserted. + name (str): Name for this layers. + + Returns: + Variable: Output unsqueezed variable. + + Examples: + .. code-block:: python + + x = layers.data(name='x', shape=[5, 10]) + y = layers.unsequeeze(x, axes=[1]) + """ + helper = LayerHelper("unsqueeze", **locals()) + out = helper.create_tmp_variable(dtype=x.dtype) + helper.append_op( + type="unsqueeze", + inputs={"X": x}, + attrs={"axes": axes}, + outputs={"Out": out}) + + return out def lod_reset(x, y=None, target_lod=None): """ diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 8e707c8b00..248976a9ff 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -240,6 +240,22 @@ class TestBook(unittest.TestCase): self.assertIsNotNone(layers.softmax(hid)) print(str(program)) + def test_sequence_unsqueeze(self): + program = Program() + with program_guard(program): + x = layers.data(name='x', shape=[8,2], dtype='float32') + out = layers.unsqueeze(x=x, axes=[1]) + self.assertIsNotNone(out) + print(str(program)) + + def test_squeeze(self): + program = Program() + with program_guard(program): + x = layers.data(name='x', shape=[1, 1, 4], dtype='float32') + out = layers.squeeze(x=x, axes=[0]) + self.assertIsNotNone(out) + print(str(program)) + def test_lrn(self): program = Program() with program_guard(program): @@ -261,6 +277,7 @@ class TestBook(unittest.TestCase): out = layers.sequence_reshape(input=x, new_dim=16) self.assertIsNotNone(out) print(str(program)) + def test_im2sequence(self): program = Program() From b586cc2ae861adf919c43300be6dfd875acc6d92 Mon Sep 17 00:00:00 2001 From: Yibing Liu Date: Sun, 19 Aug 2018 15:45:27 +0000 Subject: [PATCH 2/6] Fix typos in unsqueeze & unsequeeze wrapper --- python/paddle/fluid/layers/nn.py | 13 ++++++++----- python/paddle/fluid/tests/unittests/test_layers.py | 7 +++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 64864c267d..bb2f2f817a 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -4485,6 +4485,7 @@ def reshape(x, shape, actual_shape=None, act=None, inplace=True, name=None): return helper.append_activation(out) + def squeeze(x, axes, inplace=False, name=None): """ Remove single-dimensional entries from the shape of a tensor. Takes a @@ -4511,7 +4512,7 @@ def squeeze(x, axes, inplace=False, name=None): Args: x (Variable): The input variable to be squeezed. axes (list): List of integers, indicating the dimensions to be squeezed. - name (str): Name for this layers. + name (str|None): Name for this layer. Returns: Variable: Output squeezed variable. @@ -4530,8 +4531,9 @@ def squeeze(x, axes, inplace=False, name=None): attrs={"axes": axes}, outputs={"Out": out}) - return out - + return out + + def unsqueeze(x, axes, inplace=False, name=None): """ Insert single-dimensional entries to the shape of a tensor. Takes one @@ -4545,7 +4547,7 @@ def unsqueeze(x, axes, inplace=False, name=None): Args: x (Variable): The input variable to be unsqueezed. axes (list): List of integers, indicating the dimensions to be inserted. - name (str): Name for this layers. + name (str|None): Name for this layer. Returns: Variable: Output unsqueezed variable. @@ -4564,7 +4566,8 @@ def unsqueeze(x, axes, inplace=False, name=None): attrs={"axes": axes}, outputs={"Out": out}) - return out + return out + def lod_reset(x, y=None, target_lod=None): """ diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 248976a9ff..9f614433d9 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -243,16 +243,16 @@ class TestBook(unittest.TestCase): def test_sequence_unsqueeze(self): program = Program() with program_guard(program): - x = layers.data(name='x', shape=[8,2], dtype='float32') + x = layers.data(name='x', shape=[8, 2], dtype='float32') out = layers.unsqueeze(x=x, axes=[1]) self.assertIsNotNone(out) print(str(program)) - + def test_squeeze(self): program = Program() with program_guard(program): x = layers.data(name='x', shape=[1, 1, 4], dtype='float32') - out = layers.squeeze(x=x, axes=[0]) + out = layers.squeeze(x=x, axes=[2]) self.assertIsNotNone(out) print(str(program)) @@ -277,7 +277,6 @@ class TestBook(unittest.TestCase): out = layers.sequence_reshape(input=x, new_dim=16) self.assertIsNotNone(out) print(str(program)) - def test_im2sequence(self): program = Program() From 5aa64827df4506dd4abad60b931eb57703b2a5fb Mon Sep 17 00:00:00 2001 From: Yibing Liu Date: Sun, 19 Aug 2018 22:58:36 +0000 Subject: [PATCH 3/6] Rename the input of squeeze's wrapper --- python/paddle/fluid/layers/nn.py | 20 +++++++++---------- .../fluid/tests/unittests/test_layers.py | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index bb2f2f817a..d9abf18d0b 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -4486,7 +4486,7 @@ def reshape(x, shape, actual_shape=None, act=None, inplace=True, name=None): return helper.append_activation(out) -def squeeze(x, axes, inplace=False, name=None): +def squeeze(input, axes, inplace=False, name=None): """ Remove single-dimensional entries from the shape of a tensor. Takes a parameter axes with a list of axes to squeeze. If axes is not provided, all @@ -4510,7 +4510,7 @@ def squeeze(x, axes, inplace=False, name=None): Out.shape = (3, 5) Args: - x (Variable): The input variable to be squeezed. + input (Variable): The input variable to be squeezed. axes (list): List of integers, indicating the dimensions to be squeezed. name (str|None): Name for this layer. @@ -4521,20 +4521,20 @@ def squeeze(x, axes, inplace=False, name=None): .. code-block:: python x = layers.data(name='x', shape=[5, 1, 10]) - y = layers.sequeeze(x, axes=[1]) + y = layers.sequeeze(input=x, axes=[1]) """ helper = LayerHelper("squeeze", **locals()) - out = helper.create_tmp_variable(dtype=x.dtype) + out = helper.create_tmp_variable(dtype=input.dtype) helper.append_op( type="squeeze", - inputs={"X": x}, + inputs={"X": input}, attrs={"axes": axes}, outputs={"Out": out}) return out -def unsqueeze(x, axes, inplace=False, name=None): +def unsqueeze(input, axes, inplace=False, name=None): """ Insert single-dimensional entries to the shape of a tensor. Takes one required argument axes, a list of dimensions that will be inserted. @@ -4545,7 +4545,7 @@ def unsqueeze(x, axes, inplace=False, name=None): then Unsqueezed tensor with axes=[0, 4] has shape [1, 3, 4, 5, 1]. Args: - x (Variable): The input variable to be unsqueezed. + input (Variable): The input variable to be unsqueezed. axes (list): List of integers, indicating the dimensions to be inserted. name (str|None): Name for this layer. @@ -4556,13 +4556,13 @@ def unsqueeze(x, axes, inplace=False, name=None): .. code-block:: python x = layers.data(name='x', shape=[5, 10]) - y = layers.unsequeeze(x, axes=[1]) + y = layers.unsequeeze(input=x, axes=[1]) """ helper = LayerHelper("unsqueeze", **locals()) - out = helper.create_tmp_variable(dtype=x.dtype) + out = helper.create_tmp_variable(dtype=input.dtype) helper.append_op( type="unsqueeze", - inputs={"X": x}, + inputs={"X": input}, attrs={"axes": axes}, outputs={"Out": out}) diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 9f614433d9..f2fccd5d76 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -244,7 +244,7 @@ class TestBook(unittest.TestCase): program = Program() with program_guard(program): x = layers.data(name='x', shape=[8, 2], dtype='float32') - out = layers.unsqueeze(x=x, axes=[1]) + out = layers.unsqueeze(input=x, axes=[1]) self.assertIsNotNone(out) print(str(program)) @@ -252,7 +252,7 @@ class TestBook(unittest.TestCase): program = Program() with program_guard(program): x = layers.data(name='x', shape=[1, 1, 4], dtype='float32') - out = layers.squeeze(x=x, axes=[2]) + out = layers.squeeze(input=x, axes=[2]) self.assertIsNotNone(out) print(str(program)) From 26710ccd08f5cb382516206484ac73a37a2d318b Mon Sep 17 00:00:00 2001 From: Yibing Liu Date: Mon, 20 Aug 2018 05:25:21 +0000 Subject: [PATCH 4/6] Update api spec accordingly --- paddle/fluid/API.spec | 2 ++ 1 file changed, 2 insertions(+) diff --git a/paddle/fluid/API.spec b/paddle/fluid/API.spec index 37c2523c9f..a53bc21c72 100644 --- a/paddle/fluid/API.spec +++ b/paddle/fluid/API.spec @@ -143,6 +143,8 @@ paddle.fluid.layers.smooth_l1 ArgSpec(args=['x', 'y', 'inside_weight', 'outside_ paddle.fluid.layers.one_hot ArgSpec(args=['input', 'depth'], varargs=None, keywords=None, defaults=None) paddle.fluid.layers.autoincreased_step_counter ArgSpec(args=['counter_name', 'begin', 'step'], varargs=None, keywords=None, defaults=(None, 1, 1)) paddle.fluid.layers.reshape ArgSpec(args=['x', 'shape', 'actual_shape', 'act', 'inplace', 'name'], varargs=None, keywords=None, defaults=(None, None, True, None)) +paddle.fluid.layers.squeeze ArgSpec(args=['input', 'axes', 'inplace', 'name'], varargs=None, keywords=None, defaults=(False, None)) +paddle.fluid.layers.unsqueeze ArgSpec(args=['input', 'axes', 'inplace', 'name'], varargs=None, keywords=None, defaults=(False, None)) paddle.fluid.layers.lod_reset ArgSpec(args=['x', 'y', 'target_lod'], varargs=None, keywords=None, defaults=(None, None)) paddle.fluid.layers.lrn ArgSpec(args=['input', 'n', 'k', 'alpha', 'beta', 'name'], varargs=None, keywords=None, defaults=(5, 1.0, 0.0001, 0.75, None)) paddle.fluid.layers.pad ArgSpec(args=['x', 'paddings', 'pad_value', 'name'], varargs=None, keywords=None, defaults=(0.0, None)) From efa6f0df4eb5f682165d7b221829776ab288b8db Mon Sep 17 00:00:00 2001 From: Yibing Liu Date: Thu, 23 Aug 2018 07:50:28 +0000 Subject: [PATCH 5/6] Remove inplace args in unsqueeze & squeeze ops --- paddle/fluid/API.spec | 4 ++-- python/paddle/fluid/layers/nn.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/API.spec b/paddle/fluid/API.spec index a53bc21c72..3c25d31855 100644 --- a/paddle/fluid/API.spec +++ b/paddle/fluid/API.spec @@ -143,8 +143,8 @@ paddle.fluid.layers.smooth_l1 ArgSpec(args=['x', 'y', 'inside_weight', 'outside_ paddle.fluid.layers.one_hot ArgSpec(args=['input', 'depth'], varargs=None, keywords=None, defaults=None) paddle.fluid.layers.autoincreased_step_counter ArgSpec(args=['counter_name', 'begin', 'step'], varargs=None, keywords=None, defaults=(None, 1, 1)) paddle.fluid.layers.reshape ArgSpec(args=['x', 'shape', 'actual_shape', 'act', 'inplace', 'name'], varargs=None, keywords=None, defaults=(None, None, True, None)) -paddle.fluid.layers.squeeze ArgSpec(args=['input', 'axes', 'inplace', 'name'], varargs=None, keywords=None, defaults=(False, None)) -paddle.fluid.layers.unsqueeze ArgSpec(args=['input', 'axes', 'inplace', 'name'], varargs=None, keywords=None, defaults=(False, None)) +paddle.fluid.layers.squeeze ArgSpec(args=['input', 'axes', 'name'], varargs=None, keywords=None, defaults=(False, None)) +paddle.fluid.layers.unsqueeze ArgSpec(args=['input', 'axes', 'name'], varargs=None, keywords=None, defaults=(False, None)) paddle.fluid.layers.lod_reset ArgSpec(args=['x', 'y', 'target_lod'], varargs=None, keywords=None, defaults=(None, None)) paddle.fluid.layers.lrn ArgSpec(args=['input', 'n', 'k', 'alpha', 'beta', 'name'], varargs=None, keywords=None, defaults=(5, 1.0, 0.0001, 0.75, None)) paddle.fluid.layers.pad ArgSpec(args=['x', 'paddings', 'pad_value', 'name'], varargs=None, keywords=None, defaults=(0.0, None)) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index d9abf18d0b..365f4191ef 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -4486,7 +4486,7 @@ def reshape(x, shape, actual_shape=None, act=None, inplace=True, name=None): return helper.append_activation(out) -def squeeze(input, axes, inplace=False, name=None): +def squeeze(input, axes, name=None): """ Remove single-dimensional entries from the shape of a tensor. Takes a parameter axes with a list of axes to squeeze. If axes is not provided, all @@ -4534,7 +4534,7 @@ def squeeze(input, axes, inplace=False, name=None): return out -def unsqueeze(input, axes, inplace=False, name=None): +def unsqueeze(input, axes, name=None): """ Insert single-dimensional entries to the shape of a tensor. Takes one required argument axes, a list of dimensions that will be inserted. From 03f6292bef4e87a2b197a2fef8e9833256176296 Mon Sep 17 00:00:00 2001 From: Yibing Liu Date: Tue, 28 Aug 2018 08:09:10 +0000 Subject: [PATCH 6/6] Update API spec --- paddle/fluid/API.spec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/API.spec b/paddle/fluid/API.spec index 3c25d31855..cfb58d2be0 100644 --- a/paddle/fluid/API.spec +++ b/paddle/fluid/API.spec @@ -143,8 +143,8 @@ paddle.fluid.layers.smooth_l1 ArgSpec(args=['x', 'y', 'inside_weight', 'outside_ paddle.fluid.layers.one_hot ArgSpec(args=['input', 'depth'], varargs=None, keywords=None, defaults=None) paddle.fluid.layers.autoincreased_step_counter ArgSpec(args=['counter_name', 'begin', 'step'], varargs=None, keywords=None, defaults=(None, 1, 1)) paddle.fluid.layers.reshape ArgSpec(args=['x', 'shape', 'actual_shape', 'act', 'inplace', 'name'], varargs=None, keywords=None, defaults=(None, None, True, None)) -paddle.fluid.layers.squeeze ArgSpec(args=['input', 'axes', 'name'], varargs=None, keywords=None, defaults=(False, None)) -paddle.fluid.layers.unsqueeze ArgSpec(args=['input', 'axes', 'name'], varargs=None, keywords=None, defaults=(False, None)) +paddle.fluid.layers.squeeze ArgSpec(args=['input', 'axes', 'name'], varargs=None, keywords=None, defaults=(None,)) +paddle.fluid.layers.unsqueeze ArgSpec(args=['input', 'axes', 'name'], varargs=None, keywords=None, defaults=(None,)) paddle.fluid.layers.lod_reset ArgSpec(args=['x', 'y', 'target_lod'], varargs=None, keywords=None, defaults=(None, None)) paddle.fluid.layers.lrn ArgSpec(args=['input', 'n', 'k', 'alpha', 'beta', 'name'], varargs=None, keywords=None, defaults=(5, 1.0, 0.0001, 0.75, None)) paddle.fluid.layers.pad ArgSpec(args=['x', 'paddings', 'pad_value', 'name'], varargs=None, keywords=None, defaults=(0.0, None))