diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 83c89fe6b49236ae059e17e949cafa632999f1e2..21cf3242f1d1f4b0c242a2161b55bc9fc1e8484c 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -93,7 +93,6 @@ __all__ = [ 'smooth_l1', 'one_hot', 'autoincreased_step_counter', - 'squeeze', 'unsqueeze', 'lod_reset', 'lod_append', @@ -4095,7 +4094,7 @@ def ctc_greedy_decoder( return ctc_out else: ctc_out_len = helper.create_variable_for_type_inference(dtype="int64") - ctc_input = squeeze(topk_indices, [2]) + ctc_input = paddle.squeeze(topk_indices, [2]) helper.append_op( type="ctc_align", @@ -4636,105 +4635,6 @@ def autoincreased_step_counter(counter_name=None, begin=1, step=1): return counter -def squeeze(input, axes, name=None): - """ - This OP will squeeze single-dimensional entries of input tensor's shape. If axes is provided, will - remove the dims by axes, the dims selected by axes should be one. If not provide axes, all dims equal - to one will be deleted. - - - .. code-block:: text - - Case1: - - Input: - X.shape = (1, 3, 1, 5) - axes = [0] - Output: - Out.shape = (3, 1, 5) - - Case2: - - Input: - X.shape = (1, 3, 1, 5) - axes = [] - Output: - Out.shape = (3, 5) - - Case3: - - Input: - X.shape = [1,3,1,5] - axes = [-2] - Output: - Out.shape = [1,3,5] - - Args: - input (Variable): The input Tensor. Supported data type: float32, float64, bool, int8, int32, int64. - axes (list): One integer or List of integers, indicating the dimensions to be squeezed. - Axes range is :math:`[-rank(input), rank(input))`. - If axes is negative, :math:`axes=axes+rank(input)`. - name (str, optional): Please refer to :ref:`api_guide_Name`, Default None. - - Returns: - Variable: Output squeezed Tensor. Data type is same as input Tensor. - - Examples: - .. code-block:: python - - import paddle.fluid as fluid - import paddle.fluid.layers as layers - # set batch size=None - x = fluid.data(name='x', shape=[None, 5, 1, 10]) - y = layers.squeeze(input=x, axes=[2]) # y.shape=[None, 5, 10] - - """ - if in_dygraph_mode(): - return _C_ops.squeeze(input, axes) - if _in_legacy_dygraph(): - out, _ = _legacy_C_ops.squeeze2(input, 'axes', axes) - return out - - helper = LayerHelper("squeeze", **locals()) - check_variable_and_dtype( - input, - 'input', - [ - 'float16', - 'float32', - 'float64', - 'bool', - 'int8', - 'int32', - 'int64', - 'complex64', - 'complex128', - ], - 'squeeze', - ) - check_type(axes, 'axis/axes', (list, tuple, Variable), 'squeeze') - - attrs = {} - if isinstance(axes, Variable): - axes.stop_gradient = True - attrs["axes"] = axes - elif isinstance(axes, (list, tuple)): - if utils._contain_var(axes): - attrs["axes"] = utils._convert_to_tensor_list(axes) - else: - attrs["axes"] = axes - out = helper.create_variable_for_type_inference(dtype=input.dtype) - x_shape = helper.create_variable_for_type_inference(dtype=input.dtype) - helper.append_op( - type="squeeze2", - inputs={"X": input}, - attrs=attrs, - outputs={"Out": out, "XShape": x_shape}, - ) - - return out - - def unsqueeze(input, axes, name=None): """ Insert single-dimensional entries to the shape of a Tensor. Takes one diff --git a/python/paddle/fluid/layers/rnn.py b/python/paddle/fluid/layers/rnn.py index 6ffa3512b7949f49c5a4854cccd88ab909643a00..024eb4208ecc24ad77f0ebf941bc634f41fd8d46 100644 --- a/python/paddle/fluid/layers/rnn.py +++ b/python/paddle/fluid/layers/rnn.py @@ -2088,11 +2088,11 @@ class TrainingHelper(DecodeHelper): def _slice(x): # TODO: use Variable.__getitem__ axes = [0 if self.time_major else 1] - return nn.squeeze( + return paddle.squeeze( nn.slice( x, axes=axes, starts=[next_time], ends=[next_time + 1] ), - axes=axes, + axis=axes, ) next_inputs = map_structure(_slice, self.inputs_) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/seq2seq_dygraph_model.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/seq2seq_dygraph_model.py index 44b977037d73c7c4f1e38e5650772125cca07a35..da9214c466fa36f11521b9245451e22dd7e05ff7 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/seq2seq_dygraph_model.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/seq2seq_dygraph_model.py @@ -295,7 +295,7 @@ class BaseModel(fluid.dygraph.Layer): loss = fluid.layers.softmax_with_cross_entropy( logits=dec_output, label=label, soft_label=False ) - loss = fluid.layers.squeeze(loss, axes=[2]) + loss = paddle.squeeze(loss, axes=[2]) max_tar_seq_len = fluid.layers.shape(tar)[1] tar_mask = fluid.layers.sequence_mask( tar_sequence_length, maxlen=max_tar_seq_len, dtype='float32' @@ -811,7 +811,7 @@ class AttentionModel(fluid.dygraph.Layer): else: step_input = new_hidden dec_att = self.attention(step_input, enc_outputs, enc_padding_mask) - dec_att = fluid.layers.squeeze(dec_att, [1]) + dec_att = paddle.squeeze(dec_att, [1]) concat_att_out = fluid.layers.concat([dec_att, step_input], 1) out = self.concat_fc(concat_att_out) input_feed = out @@ -823,7 +823,7 @@ class AttentionModel(fluid.dygraph.Layer): loss = fluid.layers.softmax_with_cross_entropy( logits=dec_output, label=label, soft_label=False ) - loss = fluid.layers.squeeze(loss, axes=[2]) + loss = paddle.squeeze(loss, axes=[2]) max_tar_seq_len = fluid.layers.shape(tar)[1] tar_mask = fluid.layers.sequence_mask( tar_sequence_length, maxlen=max_tar_seq_len, dtype='float32' diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bmn.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bmn.py index e2ee44a71219c2cd8e1b9194f1b99d50687aaf5b..e34cce5d647349289c965c164fa882c6479dbd38 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bmn.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bmn.py @@ -137,7 +137,7 @@ class Conv1D(fluid.dygraph.Layer): def forward(self, x): x = fluid.layers.unsqueeze(input=x, axes=[2]) x = self._conv2d(x) - x = fluid.layers.squeeze(input=x, axes=[2]) + x = paddle.squeeze(x, axis=[2]) return x @@ -275,10 +275,10 @@ class BMN(fluid.dygraph.Layer): # TEM xs = paddle.nn.functional.relu(self.ts_conv1(x)) xs = paddle.nn.functional.relu(self.ts_conv2(xs)) - xs = fluid.layers.squeeze(xs, axes=[1]) + xs = paddle.squeeze(xs, axis=[1]) xe = paddle.nn.functional.relu(self.te_conv1(x)) xe = paddle.nn.functional.relu(self.te_conv2(xe)) - xe = fluid.layers.squeeze(xe, axes=[1]) + xe = paddle.squeeze(xe, axis=[1]) # PEM xp = paddle.nn.functional.relu(self.p_conv1(x)) @@ -287,7 +287,7 @@ class BMN(fluid.dygraph.Layer): xp = paddle.reshape(xp, shape=[0, 0, -1, self.dscale, self.tscale]) xp = self.p_conv3d1(xp) - xp = fluid.layers.squeeze(xp, axes=[2]) + xp = paddle.squeeze(xp, axis=[2]) xp = paddle.nn.functional.relu(self.p_conv2d1(xp)) xp = paddle.nn.functional.relu(self.p_conv2d2(xp)) xp = paddle.nn.functional.relu(self.p_conv2d3(xp)) @@ -411,11 +411,11 @@ def bmn_loss_func( loss = -1 * (loss_pos + loss_neg) / num_entries return loss - pred_bm_reg = fluid.layers.squeeze( - fluid.layers.slice(pred_bm, axes=[1], starts=[0], ends=[1]), axes=[1] + pred_bm_reg = paddle.squeeze( + fluid.layers.slice(pred_bm, axes=[1], starts=[0], ends=[1]), axis=[1] ) - pred_bm_cls = fluid.layers.squeeze( - fluid.layers.slice(pred_bm, axes=[1], starts=[1], ends=[2]), axes=[1] + pred_bm_cls = paddle.squeeze( + fluid.layers.slice(pred_bm, axes=[1], starts=[1], ends=[2]), axis=[1] ) bm_mask = _get_mask(cfg) diff --git a/python/paddle/fluid/tests/unittests/ipu/test_squeeze_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_squeeze_op_ipu.py index cadac8afd4138225590a137b37cbf6fe23f001e1..f48772c5252d1cbfcd3552092c4bf8305ca23591 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_squeeze_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_squeeze_op_ipu.py @@ -47,7 +47,7 @@ class TestBase(IPUOpTest): x = paddle.static.data( name=self.feed_list[0], shape=self.feed_shape[0], dtype='float32' ) - out = paddle.fluid.layers.squeeze(x, **self.attrs) + out = paddle.squeeze(x, **self.attrs) self.fetch_list = [out.name] def run_model(self, exec_mode): diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 0bea87884e0cf5b4ef215dc00676d51f2cd6e81e..04a937f33706be558fb4f15af540d66f6c7a189a 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -4097,7 +4097,7 @@ class TestBook(LayerTest): # TODO(minqiyang): dygraph do not support layers with param now with self.static_graph(): x = layers.data(name='x', shape=[1, 1, 4], dtype='float32') - out = layers.squeeze(input=x, axes=[2]) + out = paddle.squeeze(x, axis=[2]) return out def test_flatten(self): diff --git a/python/paddle/fluid/tests/unittests/test_recurrent_op.py b/python/paddle/fluid/tests/unittests/test_recurrent_op.py index e0abda02b37f3ba4ef9e8fdbb7e165b3cce95ab1..9abfba1b70f2ea0fc8a28ef85c1e8bd874be298a 100644 --- a/python/paddle/fluid/tests/unittests/test_recurrent_op.py +++ b/python/paddle/fluid/tests/unittests/test_recurrent_op.py @@ -635,7 +635,7 @@ class RecurrentOpSubBlockTest(RecurrentOpTest1): new_h = layers.matmul(concat_in, w2) new_h = layers.unsqueeze(new_h, [1]) new_h, _ = dot_attention(new_h, y) - new_h = layers.squeeze(new_h, [1]) + new_h = paddle.squeeze(new_h, [1]) rnn.update_memory(pre_h, new_h) rnn.step_output(new_h) diff --git a/python/paddle/fluid/tests/unittests/test_rnn_decode_api.py b/python/paddle/fluid/tests/unittests/test_rnn_decode_api.py index 7b2090cd7259fb21b410693e33d11d7132b4328f..2bd5d3dd57d4f760c9eef4c3bde542f3ba42bc14 100644 --- a/python/paddle/fluid/tests/unittests/test_rnn_decode_api.py +++ b/python/paddle/fluid/tests/unittests/test_rnn_decode_api.py @@ -80,7 +80,7 @@ class DecoderCell(layers.RNNCell): attn_scores, encoder_padding_mask ) attn_scores = layers.softmax(attn_scores) - attn_out = layers.squeeze( + attn_out = paddle.squeeze( layers.matmul(attn_scores, encoder_output), [1] ) attn_out = layers.concat([attn_out, hidden], 1) diff --git a/python/paddle/fluid/tests/unittests/test_squeeze2_op.py b/python/paddle/fluid/tests/unittests/test_squeeze2_op.py index c64b58efea141baca5f224996e66c4070a537ea1..4fbb08562b9c82c61a277859f88885716d765a09 100755 --- a/python/paddle/fluid/tests/unittests/test_squeeze2_op.py +++ b/python/paddle/fluid/tests/unittests/test_squeeze2_op.py @@ -97,7 +97,7 @@ class TestSqueeze2AxesTensor(UnittestBase): # axes is a Variable axes = paddle.assign([0, 2]) out = paddle.squeeze(feat, axes) - out2 = paddle.fluid.layers.squeeze(feat, axes) + out2 = paddle.squeeze(feat, axes) sgd = paddle.optimizer.SGD() sgd.minimize(paddle.mean(out)) @@ -136,7 +136,7 @@ class TestSqueeze2AxesTensorList(UnittestBase): paddle.full([1], 2, dtype='int32'), ] out = paddle.squeeze(feat, axes) - out2 = paddle.fluid.layers.squeeze(feat, axes) + out2 = paddle.squeeze(feat, axes) sgd = paddle.optimizer.SGD() sgd.minimize(paddle.mean(out))