未验证 提交 42655ef7 编写于 作者: P Pei Yang 提交者: GitHub

Add full_like op. (#23364)

* add full_like op. test=develop

* add dygraph support. test=develop

* increase coverage. test=develop
上级 480530c4
......@@ -49,9 +49,10 @@ class ActivationOpConverter : public OpConverter {
layer->setAlpha(0.);
layer->setBeta(6.);
}
g
#endif
auto output_name = op_desc.Output("Out")[0];
auto output_name = op_desc.Output("Out")[0];
RreplenishLayerAndOutput(layer, op_type_, {output_name}, test_mode);
if (op_desc.HasAttr("out_scale")) {
......
......@@ -56,7 +56,7 @@ from .tensor.creation import zeros_like #DEFINE_ALIAS
# from .tensor.creation import eye #DEFINE_ALIAS
from .tensor.creation import full #DEFINE_ALIAS
# from .tensor.creation import linspace #DEFINE_ALIAS
# from .tensor.creation import full_like #DEFINE_ALIAS
from .tensor.creation import full_like #DEFINE_ALIAS
# from .tensor.creation import triu #DEFINE_ALIAS
# from .tensor.creation import tril #DEFINE_ALIAS
# from .tensor.creation import meshgrid #DEFINE_ALIAS
......
......@@ -17,6 +17,7 @@ from __future__ import print_function
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid import Program, program_guard
import paddle.compat as cpt
import unittest
import numpy as np
......@@ -96,6 +97,60 @@ class TestFillAnyLikeOpFloat16(TestFillAnyLikeOp):
self.dtype = np.float16
class TestFillAnyLikeOp_attr_out(unittest.TestCase):
""" Test fill_any_like op(whose API is full_like) for attr out. """
def test_attr_tensor_API(self):
startup_program = fluid.Program()
train_program = fluid.Program()
with fluid.program_guard(train_program, startup_program):
fill_value = 2.0
input = fluid.data(name='input', dtype='float32', shape=[2, 3])
output = paddle.full_like(input, fill_value)
place = fluid.CPUPlace()
if fluid.core.is_compiled_with_cuda():
place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)
exe.run(startup_program)
img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
res = exe.run(train_program,
feed={'input': img},
fetch_list=[output])
out_np = np.array(res[0])
self.assertTrue(
not (out_np - np.full_like(img, fill_value)).any(),
msg="full_like output is wrong, out = " + str(out_np))
class TestFillAnyLikeOpError(unittest.TestCase):
def test_errors(self):
with program_guard(Program(), Program()):
#for ci coverage
input_data = fluid.data(name='input', dtype='float32', shape=[2, 3])
output = paddle.full_like(input_data, 2.0)
def test_input_dtype():
paddle.full_like
self.assertRaises(
ValueError,
paddle.full_like,
input=input_data,
fill_value=2,
dtype='uint4')
self.assertRaises(
TypeError,
paddle.full_like,
input=input_data,
fill_value=2,
dtype='int16')
class ApiOnesLikeTest(unittest.TestCase):
def test_out(self):
with fluid.program_guard(fluid.Program()):
......
......@@ -31,7 +31,7 @@ from .creation import linspace #DEFINE_ALIAS
# from .creation import eye #DEFINE_ALIAS
from .creation import full # DEFINE_ALIAS
# from .creation import linspace #DEFINE_ALIAS
# from .creation import full_like #DEFINE_ALIAS
from .creation import full_like #DEFINE_ALIAS
from .creation import triu #DEFINE_ALIAS
from .creation import tril #DEFINE_ALIAS
# from .creation import meshgrid #DEFINE_ALIAS
......
......@@ -12,10 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
from ..fluid.framework import Variable
from ..fluid.initializer import Constant
from ..fluid.layers import core
from ..fluid.layer_helper import LayerHelper
from ..fluid.data_feeder import check_variable_and_dtype, check_type, check_dtype, convert_dtype
from ..fluid.framework import convert_np_dtype_to_dtype_, in_dygraph_mode, _varbase_creator, device_guard, OpProtoHolder
from ..fluid.layers import fill_constant
from paddle.common_ops_import import *
# TODO: define functions to get create a tensor
__all__ = [
'create_tensor',
# 'create_lod_tensor',
......@@ -33,13 +40,64 @@ __all__ = [
# 'arrange',
# 'eye',
'full',
# 'full_like',
'full_like',
'triu',
'tril',
# 'meshgrid'
# 'meshgrid',
]
def full_like(input,
fill_value,
out=None,
dtype=None,
device=None,
stop_gradient=True,
name=None):
"""
**full_like**
This function creates a tensor filled with `fill_value` which has identical shape and dtype
with `input`.
Args:
input(Variable): The input tensor which specifies shape and dtype.
fill_value: The value to fill the tensor with. Data type can be bool, float32, float64, int32, int64. Default value is 0.
out(Variable): The output tensor.
Returns:
out(Variable): The tensor variable storing the output.
Examples:
.. code-block:: python
import paddle
import paddle.fluid as fluid
import numpy as np
input = fluid.data(name='input', dtype='float32', shape=[2, 3])
output = paddle.full_like(input, 2.0)
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
img=np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
res = exe.run(fluid.default_main_program(), feed={'input':img}, fetch_list=[output])
print(res) # [array([[2., 2., 2.], [2., 2., 2.]], dtype=float32)]
"""
helper = LayerHelper("full_like", **locals())
if dtype is None:
dtype = 'float32'
check_dtype(dtype, 'dtype',
['bool', 'float16', 'float32', 'int32', 'int64'], 'full_like')
if out is None:
out = helper.create_variable_for_type_inference(dtype=dtype)
helper.append_op(
type='fill_any_like',
inputs={'X': [input]},
attrs={'value': fill_value},
outputs={'Out': [out]})
out.stop_gradient = stop_gradient
return out
def linspace(start, stop, num, dtype, out=None, device=None, name=None):
"""
This OP return fixed number of evenly spaced values within a given interval.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册