Skip to content

Commit f88a8ba

Browse files
author
Yibing Liu
authored
Merge pull request #12793 from kuke/wrap_squeezes
Wrap unsqueeze & squeeze ops
2 parents 3bd1d22 + 13509da commit f88a8ba

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

paddle/fluid/API.spec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ paddle.fluid.layers.smooth_l1 ArgSpec(args=['x', 'y', 'inside_weight', 'outside_
145145
paddle.fluid.layers.one_hot ArgSpec(args=['input', 'depth'], varargs=None, keywords=None, defaults=None)
146146
paddle.fluid.layers.autoincreased_step_counter ArgSpec(args=['counter_name', 'begin', 'step'], varargs=None, keywords=None, defaults=(None, 1, 1))
147147
paddle.fluid.layers.reshape ArgSpec(args=['x', 'shape', 'actual_shape', 'act', 'inplace', 'name'], varargs=None, keywords=None, defaults=(None, None, True, None))
148+
paddle.fluid.layers.squeeze ArgSpec(args=['input', 'axes', 'name'], varargs=None, keywords=None, defaults=(None,))
149+
paddle.fluid.layers.unsqueeze ArgSpec(args=['input', 'axes', 'name'], varargs=None, keywords=None, defaults=(None,))
148150
paddle.fluid.layers.lod_reset ArgSpec(args=['x', 'y', 'target_lod'], varargs=None, keywords=None, defaults=(None, None))
149151
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))
150152
paddle.fluid.layers.pad ArgSpec(args=['x', 'paddings', 'pad_value', 'name'], varargs=None, keywords=None, defaults=(0.0, None))

python/paddle/fluid/layers/nn.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
'one_hot',
8686
'autoincreased_step_counter',
8787
'reshape',
88+
'squeeze',
89+
'unsqueeze',
8890
'lod_reset',
8991
'lrn',
9092
'pad',
@@ -4532,6 +4534,89 @@ def reshape(x, shape, actual_shape=None, act=None, inplace=True, name=None):
45324534
return helper.append_activation(out)
45334535

45344536

4537+
def squeeze(input, axes, name=None):
4538+
"""
4539+
Remove single-dimensional entries from the shape of a tensor. Takes a
4540+
parameter axes with a list of axes to squeeze. If axes is not provided, all
4541+
the single dimensions will be removed from the shape. If an axis is
4542+
selected with shape entry not equal to one, an error is raised.
4543+
4544+
Examples:
4545+
Case 1:
4546+
Given
4547+
X.shape = (1, 3, 1, 5)
4548+
and
4549+
axes = [0]
4550+
we get:
4551+
Out.shape = (3, 1, 5)
4552+
Case 2:
4553+
Given
4554+
X.shape = (1, 3, 1, 5)
4555+
and
4556+
axes = []
4557+
we get:
4558+
Out.shape = (3, 5)
4559+
4560+
Args:
4561+
input (Variable): The input variable to be squeezed.
4562+
axes (list): List of integers, indicating the dimensions to be squeezed.
4563+
name (str|None): Name for this layer.
4564+
4565+
Returns:
4566+
Variable: Output squeezed variable.
4567+
4568+
Examples:
4569+
.. code-block:: python
4570+
4571+
x = layers.data(name='x', shape=[5, 1, 10])
4572+
y = layers.sequeeze(input=x, axes=[1])
4573+
"""
4574+
helper = LayerHelper("squeeze", **locals())
4575+
out = helper.create_tmp_variable(dtype=input.dtype)
4576+
helper.append_op(
4577+
type="squeeze",
4578+
inputs={"X": input},
4579+
attrs={"axes": axes},
4580+
outputs={"Out": out})
4581+
4582+
return out
4583+
4584+
4585+
def unsqueeze(input, axes, name=None):
4586+
"""
4587+
Insert single-dimensional entries to the shape of a tensor. Takes one
4588+
required argument axes, a list of dimensions that will be inserted.
4589+
Dimension indices in axes are as seen in the output tensor.
4590+
4591+
For example:
4592+
Given a tensor such that tensor with shape [3, 4, 5],
4593+
then Unsqueezed tensor with axes=[0, 4] has shape [1, 3, 4, 5, 1].
4594+
4595+
Args:
4596+
input (Variable): The input variable to be unsqueezed.
4597+
axes (list): List of integers, indicating the dimensions to be inserted.
4598+
name (str|None): Name for this layer.
4599+
4600+
Returns:
4601+
Variable: Output unsqueezed variable.
4602+
4603+
Examples:
4604+
.. code-block:: python
4605+
4606+
x = layers.data(name='x', shape=[5, 10])
4607+
y = layers.unsequeeze(input=x, axes=[1])
4608+
"""
4609+
helper = LayerHelper("unsqueeze", **locals())
4610+
out = helper.create_tmp_variable(dtype=input.dtype)
4611+
helper.append_op(
4612+
type="unsqueeze",
4613+
inputs={"X": input},
4614+
attrs={"axes": axes},
4615+
outputs={"Out": out})
4616+
4617+
return out
4618+
4619+
45354620
def lod_reset(x, y=None, target_lod=None):
45364621
"""
45374622
Set LoD of :attr:`x` to a new one specified by :attr:`y` or

python/paddle/fluid/tests/unittests/test_layers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,22 @@ def test_softmax(self):
240240
self.assertIsNotNone(layers.softmax(hid))
241241
print(str(program))
242242

243+
def test_sequence_unsqueeze(self):
244+
program = Program()
245+
with program_guard(program):
246+
x = layers.data(name='x', shape=[8, 2], dtype='float32')
247+
out = layers.unsqueeze(input=x, axes=[1])
248+
self.assertIsNotNone(out)
249+
print(str(program))
250+
251+
def test_squeeze(self):
252+
program = Program()
253+
with program_guard(program):
254+
x = layers.data(name='x', shape=[1, 1, 4], dtype='float32')
255+
out = layers.squeeze(input=x, axes=[2])
256+
self.assertIsNotNone(out)
257+
print(str(program))
258+
243259
def test_lrn(self):
244260
program = Program()
245261
with program_guard(program):

0 commit comments

Comments
 (0)