|
|
|
@ -23,6 +23,7 @@ from layer_function_generator import autodoc, templatedoc
|
|
|
|
|
from tensor import concat
|
|
|
|
|
import utils
|
|
|
|
|
import random
|
|
|
|
|
from .. import unique_name
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
|
'fc',
|
|
|
|
@ -4266,14 +4267,18 @@ def reshape(x, shape, actual_shape=None, act=None, inplace=True, name=None):
|
|
|
|
|
say :attr:`actual_shape` has a higher priority
|
|
|
|
|
than :attr:`shape`.
|
|
|
|
|
act (str): The non-linear activation to be applied to output variable.
|
|
|
|
|
inplace(bool): If this flag is set true, a new output tensor is created
|
|
|
|
|
whose data is copied from input x, otherwise the output
|
|
|
|
|
shares data with input without copying.
|
|
|
|
|
inplace(bool): If this flag is set true, the output
|
|
|
|
|
shares data with input without copying, otherwise
|
|
|
|
|
a new output tensor is created
|
|
|
|
|
whose data is copied from input x.
|
|
|
|
|
name (str): The name of this layer. It is optional.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Variable: The output tensor.
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
TypeError: if actual_shape is neither Variable nor None.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
@ -4285,6 +4290,11 @@ def reshape(x, shape, actual_shape=None, act=None, inplace=True, name=None):
|
|
|
|
|
|
|
|
|
|
if not (isinstance(shape, list) or isinstance(shape, tuple)):
|
|
|
|
|
raise ValueError("Input shape must be a python lsit or tuple.")
|
|
|
|
|
inputs = {"X": x}
|
|
|
|
|
if isinstance(actual_shape, Variable):
|
|
|
|
|
inputs["Shape"] = actual_shape
|
|
|
|
|
elif actual_shape is not None:
|
|
|
|
|
raise TypeError("actual_shape should either be Variable or None")
|
|
|
|
|
|
|
|
|
|
# Validate the shape
|
|
|
|
|
unk_dim_idx = -1
|
|
|
|
@ -4305,9 +4315,7 @@ def reshape(x, shape, actual_shape=None, act=None, inplace=True, name=None):
|
|
|
|
|
reshaped = helper.create_tmp_variable(dtype=x.dtype)
|
|
|
|
|
helper.append_op(
|
|
|
|
|
type="reshape",
|
|
|
|
|
inputs={"X": x,
|
|
|
|
|
"Shape": actual_shape}
|
|
|
|
|
if isinstance(actual_shape, Variable) else {"X": x},
|
|
|
|
|
inputs=inputs,
|
|
|
|
|
attrs={"shape": shape,
|
|
|
|
|
"inplace": inplace},
|
|
|
|
|
outputs={"Out": reshaped})
|
|
|
|
@ -4889,47 +4897,39 @@ def random_crop(x, shape, seed=None):
|
|
|
|
|
>>> cropped_img = fluid.layers.random_crop(img, shape=[3, 224, 224])
|
|
|
|
|
"""
|
|
|
|
|
helper = LayerHelper("random_crop", **locals())
|
|
|
|
|
dtype = helper.input_dtype()
|
|
|
|
|
dtype = x.dtype
|
|
|
|
|
out = helper.create_tmp_variable(dtype)
|
|
|
|
|
if seed is None:
|
|
|
|
|
seed = random.randint(-65536, 65535)
|
|
|
|
|
|
|
|
|
|
op_attrs = {"shape": shape}
|
|
|
|
|
if isinstance(seed, int):
|
|
|
|
|
seed_value = seed
|
|
|
|
|
seed = helper.create_tmp_variable(dtype="int64")
|
|
|
|
|
helper.append_op(
|
|
|
|
|
type="fill_constant",
|
|
|
|
|
inputs={},
|
|
|
|
|
outputs={"Out": seed},
|
|
|
|
|
attrs={
|
|
|
|
|
"dtype": seed.dtype,
|
|
|
|
|
"shape": [1],
|
|
|
|
|
"value": float(seed_value),
|
|
|
|
|
"force_cpu": True
|
|
|
|
|
})
|
|
|
|
|
op_attrs["startup_seed"] = seed
|
|
|
|
|
seed = helper.create_variable(
|
|
|
|
|
name=unique_name.generate("random_crop_seed"),
|
|
|
|
|
dtype="int64",
|
|
|
|
|
persistable=True)
|
|
|
|
|
elif not isinstance(seed, Variable):
|
|
|
|
|
raise ValueError("'seed' must be a Variable or an int.")
|
|
|
|
|
seed_out = helper.create_tmp_variable(dtype="int64")
|
|
|
|
|
helper.append_op(
|
|
|
|
|
type="random_crop",
|
|
|
|
|
inputs={"X": x,
|
|
|
|
|
"Seed": seed},
|
|
|
|
|
outputs={"Out": out,
|
|
|
|
|
"SeedOut": seed_out},
|
|
|
|
|
attrs={"shape": shape})
|
|
|
|
|
"SeedOut": seed},
|
|
|
|
|
attrs=op_attrs)
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def log(input):
|
|
|
|
|
def log(x):
|
|
|
|
|
"""
|
|
|
|
|
Calculates the natural log of the given input tensor, element-wise.
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
|
|
Out = \\ln(input)
|
|
|
|
|
Out = \\ln(x)
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
input (Variable): Input tensor.
|
|
|
|
|
x (Variable): Input tensor.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Variable: The natural log of the input tensor computed element-wise.
|
|
|
|
@ -4938,7 +4938,7 @@ def log(input):
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
output = fluid.layers.log(input)
|
|
|
|
|
output = fluid.layers.log(x)
|
|
|
|
|
"""
|
|
|
|
|
helper = LayerHelper('log', **locals())
|
|
|
|
|
dtype = helper.input_dtype(input_param_name='x')
|
|
|
|
@ -4947,18 +4947,18 @@ def log(input):
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def relu(input):
|
|
|
|
|
def relu(x):
|
|
|
|
|
"""
|
|
|
|
|
Relu takes one input data (Tensor) and produces one output data (Tensor)
|
|
|
|
|
where the rectified linear function, y = max(0, input), is applied to
|
|
|
|
|
where the rectified linear function, y = max(0, x), is applied to
|
|
|
|
|
the tensor elementwise.
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
|
|
Out = \\max(0, input)
|
|
|
|
|
Out = \\max(0, x)
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
input (Variable): The input tensor.
|
|
|
|
|
x (Variable): The input tensor.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Variable: The output tensor with the same shape as input.
|
|
|
|
@ -4967,7 +4967,7 @@ def relu(input):
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
output = fluid.layers.relu(input)
|
|
|
|
|
output = fluid.layers.relu(x)
|
|
|
|
|
"""
|
|
|
|
|
helper = LayerHelper('relu', **locals())
|
|
|
|
|
dtype = helper.input_dtype(input_param_name='x')
|
|
|
|
|