|
30 | 30 | import warnings
|
31 | 31 |
|
32 | 32 | __all__ = [
|
33 |
| - 'create_tensor', 'create_parameter', 'create_global_var', 'cast', |
34 |
| - 'tensor_array_to_tensor', 'concat', 'sums', 'assign', |
35 |
| - 'fill_constant_batch_size_like', 'fill_constant', 'argmin', 'argmax', |
36 |
| - 'argsort', 'ones', 'zeros', 'reverse', 'has_inf', 'has_nan', 'isfinite', |
37 |
| - 'range', 'linspace', 'zeros_like', 'ones_like', 'diag', 'eye', 'kron', |
38 |
| - 'full_like', 'arange', 'full', 'tril', 'triu' |
| 33 | + 'create_tensor', |
| 34 | + 'create_parameter', |
| 35 | + 'create_global_var', |
| 36 | + 'cast', |
| 37 | + 'tensor_array_to_tensor', |
| 38 | + 'concat', |
| 39 | + 'sums', |
| 40 | + 'assign', |
| 41 | + 'fill_constant_batch_size_like', |
| 42 | + 'fill_constant', |
| 43 | + 'argmin', |
| 44 | + 'argmax', |
| 45 | + 'argsort', |
| 46 | + 'ones', |
| 47 | + 'zeros', |
| 48 | + 'reverse', |
| 49 | + 'has_inf', |
| 50 | + 'has_nan', |
| 51 | + 'isfinite', |
| 52 | + 'range', |
| 53 | + 'linspace', |
| 54 | + 'full_like', |
| 55 | + 'zeros_like', |
| 56 | + 'ones_like', |
| 57 | + 'diag', |
| 58 | + 'eye', |
| 59 | + 'kron', |
| 60 | + 'arange', |
| 61 | + 'full', |
| 62 | + 'tril', |
| 63 | + 'triu', |
39 | 64 | ]
|
40 | 65 |
|
41 | 66 |
|
@@ -1371,6 +1396,70 @@ def linspace(start, stop, num, dtype):
|
1371 | 1396 | return out
|
1372 | 1397 |
|
1373 | 1398 |
|
| 1399 | +def full_like(input, |
| 1400 | + fill_value, |
| 1401 | + out=None, |
| 1402 | + dtype=None, |
| 1403 | + device=None, |
| 1404 | + stop_gradient=True, |
| 1405 | + name=None): |
| 1406 | + """ |
| 1407 | + **full_like** |
| 1408 | + This function creates a tensor filled with `fill_value` which has identical shape and dtype |
| 1409 | + with `input`. |
| 1410 | +
|
| 1411 | + Args: |
| 1412 | + input(Variable): The input tensor which specifies shape and data type. The data type can be bool, float16, float32, float64, int32, int64. |
| 1413 | + fill_value(bool|float|int): The value to fill the tensor with. Default value is 0. Note: this value shouldn't exceed the range of the output data type. |
| 1414 | + out(Variable, optional): Optional output which can be any created Variable that meets the requirements to store the result of operation. If out is None, a new Varibale will be create to store the result. Default value is None. |
| 1415 | + dtype(np.dtype|core.VarDesc.VarType|str, optional): The data type of output. The default value is None, which means the output data type is the same as input. |
| 1416 | + device (string, optional): Which device to run the operator. The :attr:`device` must be None, 'cpu', 'gpu'. If :attr:`device` is None, it will be the device that the user set in the paddle program. Default value is None. |
| 1417 | + stop_gradient(bool, optional): Indicating if we stop gradient from current(out) Variable. Default value is True. |
| 1418 | + name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name` |
| 1419 | + |
| 1420 | + Returns: |
| 1421 | + out(Variable): The Tensor variable storing the output. |
| 1422 | + |
| 1423 | + Examples: |
| 1424 | + .. code-block:: python |
| 1425 | +
|
| 1426 | + import paddle |
| 1427 | + import paddle.fluid as fluid |
| 1428 | + import numpy as np |
| 1429 | + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) |
| 1430 | + output = fluid.layers.full_like(input, 2.0) |
| 1431 | + exe = fluid.Executor(fluid.CPUPlace()) |
| 1432 | + exe.run(fluid.default_startup_program()) |
| 1433 | + img=np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) |
| 1434 | + res = exe.run(fluid.default_main_program(), feed={'input':img}, fetch_list=[output]) |
| 1435 | + print(res) # [array([[2., 2., 2.], [2., 2., 2.]], dtype=float32)] |
| 1436 | + """ |
| 1437 | + helper = LayerHelper("full_like", **locals()) |
| 1438 | + |
| 1439 | + var_dtype = None |
| 1440 | + if dtype is None: |
| 1441 | + var_dtype = input.dtype |
| 1442 | + else: |
| 1443 | + check_dtype( |
| 1444 | + dtype, 'dtype', |
| 1445 | + ['bool', 'float16', 'float32', 'float64', 'int32', 'int64'], |
| 1446 | + 'full_like') |
| 1447 | + var_dtype = convert_np_dtype_to_dtype_(dtype) |
| 1448 | + |
| 1449 | + if out is None: |
| 1450 | + out = helper.create_variable_for_type_inference(dtype=dtype) |
| 1451 | + |
| 1452 | + helper.append_op( |
| 1453 | + type='fill_any_like', |
| 1454 | + inputs={'X': [input]}, |
| 1455 | + attrs={'value': fill_value, |
| 1456 | + "dtype": var_dtype}, |
| 1457 | + outputs={'Out': [out]}) |
| 1458 | + out.stop_gradient = stop_gradient |
| 1459 | + |
| 1460 | + return out |
| 1461 | + |
| 1462 | + |
1374 | 1463 | def zeros_like(x, out=None):
|
1375 | 1464 | """
|
1376 | 1465 | This OP creates a zeros tensor which has identical shape and dtype
|
@@ -1567,56 +1656,6 @@ def ones_like(x, out=None):
|
1567 | 1656 | return out
|
1568 | 1657 |
|
1569 | 1658 |
|
1570 |
| -def full_like(input, |
1571 |
| - fill_value, |
1572 |
| - out=None, |
1573 |
| - dtype=None, |
1574 |
| - device=None, |
1575 |
| - stop_gradient=True, |
1576 |
| - name=None): |
1577 |
| - """ |
1578 |
| - **full_like** |
1579 |
| - This function creates a tensor filled with `fill_value` which has identical shape and dtype |
1580 |
| - with `input`. |
1581 |
| - Args: |
1582 |
| - input(Variable): The input tensor which specifies shape and dtype. |
1583 |
| - fill_value: The value to fill the tensor with. Data type can be bool, float32, float64, int32, int64. Default value is 0. |
1584 |
| - out(Variable): The output tensor. |
1585 |
| - Returns: |
1586 |
| - out(Variable): The tensor variable storing the output. |
1587 |
| - Examples: |
1588 |
| - .. code-block:: python |
1589 |
| - import paddle.fluid as fluid |
1590 |
| - import numpy as np |
1591 |
| -
|
1592 |
| - input = fluid.data(name='input', dtype='float32', shape=[2, 3]) |
1593 |
| - output = fluid.layers.full_like(input, 2.0) |
1594 |
| - exe = fluid.Executor(fluid.CPUPlace()) |
1595 |
| - exe.run(fluid.default_startup_program()) |
1596 |
| - img=np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) |
1597 |
| - res = exe.run(fluid.default_main_program(), feed={'input':img}, fetch_list=[output]) |
1598 |
| - print(res) # [array([[2., 2., 2.], [2., 2., 2.]], dtype=float32)] |
1599 |
| - """ |
1600 |
| - helper = LayerHelper("full_like", **locals()) |
1601 |
| - |
1602 |
| - if dtype is None: |
1603 |
| - dtype = 'float32' |
1604 |
| - |
1605 |
| - check_dtype(dtype, 'dtype', |
1606 |
| - ['bool', 'float16', 'float32', 'int32', 'int64'], 'full_like') |
1607 |
| - |
1608 |
| - if out is None: |
1609 |
| - out = helper.create_variable_for_type_inference(dtype=dtype) |
1610 |
| - helper.append_op( |
1611 |
| - type='fill_any_like', |
1612 |
| - inputs={'X': [input]}, |
1613 |
| - attrs={'value': fill_value}, |
1614 |
| - outputs={'Out': [out]}) |
1615 |
| - out.stop_gradient = stop_gradient |
1616 |
| - |
1617 |
| - return out |
1618 |
| - |
1619 |
| - |
1620 | 1659 | def arange(start, end, step=1, dtype=None, name=None):
|
1621 | 1660 | """
|
1622 | 1661 | Return evenly spaced values within a given interval.
|
|
0 commit comments