tensor¶
argmax¶
-
paddle.fluid.layers.
argmax
(x, axis=0)[source] argmax
This function computes the indices of the max elements of the input tensor’s element along the provided axis.
- Parameters
x (Variable) – The input to compute the indices of the max elements.
axis (int) – Axis to compute indices along.
- Returns
The tensor variable storing the output
- Return type
Variable
Examples
import paddle.fluid as fluid x = fluid.layers.data(name="x", shape=[3, 4], dtype="float32") out = fluid.layers.argmax(x, axis=0) out = fluid.layers.argmax(x, axis=-1)
argmin¶
-
paddle.fluid.layers.
argmin
(x, axis=0)[source] argmin
This function computes the indices of the min elements of the input tensor’s element along the provided axis.
- Parameters
x (Variable) – The input to compute the indices of the min elements.
axis (int) – Axis to compute indices along.
- Returns
The tensor variable storing the output
- Return type
Variable
Examples
import paddle.fluid as fluid x = fluid.layers.data(name="x", shape=[3, 4], dtype="float32") out = fluid.layers.argmin(x, axis=0) out = fluid.layers.argmin(x, axis=-1)
argsort¶
-
paddle.fluid.layers.
argsort
(input, axis=-1, name=None)[source] Performs sorting on the input Variable along the given axis, and outputs sorted data Varibale and its corresponding index Variable with the same shape as
input
.For example, the given axis is -1 and the input Variable input = [[0.15849551, 0.45865775, 0.8563702 ], [0.12070083, 0.28766365, 0.18776911]], after argsort, the sorted Vairable becomes out = [[0.15849551, 0.45865775, 0.8563702 ], [0.12070083, 0.18776911, 0.28766365]], and the sorted indices along the given axis turn outs to be indices = [[0, 1, 2], [0, 2, 1]]
- Parameters
input (Variable) – The input Variable for sorting.
axis (int) – The axis along which to sort the input Variable. When
axis
< 0, the actual axis will beaxis
+ rank(input
). Default -1, the last dimension.name (str|None) – (optional) A name for this layer. If set None, the layer will be named automatically.
- Returns
A tuple of sorted data Variable and the sorted indices.
- Return type
tuple
Examples
import paddle.fluid as fluid x = fluid.layers.data(name="x", shape=[3, 4], dtype="float32") out, indices = fluid.layers.argsort(input=x, axis=0)
assign¶
-
paddle.fluid.layers.
assign
(input, output=None)[source] Assign
This function copies the input Variable to the output Variable.
- Parameters
input (Variable|numpy.ndarray) – The source variable
output (Variable|None) – The destination variable
- Returns
The destination variable that was supplied as the output.
- Return type
Variable
Examples
import paddle.fluid as fluid data = fluid.layers.data(name="data", shape=[3, 32, 32], dtype="float32") out = fluid.layers.create_tensor(dtype='float32') hidden = fluid.layers.fc(input=data, size=10) fluid.layers.assign(hidden, out)
cast¶
-
paddle.fluid.layers.
cast
(x, dtype)[source] This layer takes in the Variable
x
withx.dtype
and casts it to the output withdtype
. It’s meaningless if the output dtype equals the input dtype, but it’s fine if you do so.- Parameters
x (Variable) – The input Variable for casting.
dtype (np.dtype|core.VarDesc.VarType|str) – Data type of the output Variable.
- Returns
The output Variable after casting.
- Return type
Variable
Examples
import paddle.fluid as fluid data = fluid.layers.data(name='x', shape=[13], dtype='float32') result = fluid.layers.cast(x=data, dtype='float64')
concat¶
-
paddle.fluid.layers.
concat
(input, axis=0, name=None)[source] Concat
This function concatenates the input along the axis mentioned and returns that as the output.
- Parameters
input (list) – List of tensors to be concatenated
axis (int) – Integer axis along which the tensors will be concatenated
name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
- Returns
Output variable of the concatenation
- Return type
Variable
Examples
import paddle.fluid as fluid a = fluid.layers.data(name='a', shape=[2, 13], dtype='float32') b = fluid.layers.data(name='b', shape=[2, 3], dtype='float32') c = fluid.layers.data(name='c', shape=[2, 2], dtype='float32') d = fluid.layers.data(name='d', shape=[2, 5], dtype='float32') out = fluid.layers.concat(input=[a, b, c, d], axis=2)
create_global_var¶
-
paddle.fluid.layers.
create_global_var
(shape, value, dtype, persistable=False, force_cpu=False, name=None)[source] Create a new tensor variable with value in the global block(block 0).
- Parameters
shape (list[int]) – shape of the variable
value (float) – the value of the variable. The new created variable will be filled with it.
dtype (string) – data type of the variable
persistable (bool) – if this variable is persistable. Default: False
force_cpu (bool) – force this variable to be on CPU. Default: False
name (str|None) – The name of the variable. If set to None the variable name will be generated automatically. Default: None
- Returns
the created Variable
- Return type
Variable
Examples
import paddle.fluid as fluid import paddle.fluid.layers as layers var = layers.create_global_var(shape=[2,3], value=1.0, dtype='float32', persistable=True, force_cpu=True, name='new_var')
create_parameter¶
-
paddle.fluid.layers.
create_parameter
(shape, dtype, name=None, attr=None, is_bias=False, default_initializer=None)[source] Create a parameter. The parameter is a learnable variable, which can have gradient, and can be optimized.
NOTE: this is a very low-level API. This API is useful when you create operator by your self. instead of using layers.
- Parameters
shape (list[int]) – shape of the parameter
dtype (string) – element type of the parameter
attr (ParamAttr) – attributes of the parameter
is_bias (bool) – This can affect which default initializer is chosen when default_initializer is None. If is_bias, initializer.Constant(0.0) will be used. Otherwise, Xavier() will be used.
default_initializer (Initializer) – initializer for the parameter
- Returns
the created parameter.
Examples
import paddle.fluid as fluid import paddle.fluid.layers as layers W = layers.create_parameter(shape=[784, 200], dtype='float32')
create_tensor¶
-
paddle.fluid.layers.
create_tensor
(dtype, name=None, persistable=False)[source] Create an variable, which will hold a LoDTensor with data type dtype.
- Parameters
dtype (string) – ‘float32’|’int32’|…, the data type of the created tensor.
name (string) – The name of the created tensor, if not set, the name will be a random unique one.
persistable (bool) – Set the persistable flag of the create tensor.
- Returns
The tensor variable storing the created tensor.
- Return type
Variable
Examples
import paddle.fluid as fluid tensor = fluid.layers.create_tensor(dtype='float32')
diag¶
-
paddle.fluid.layers.
diag
(diagonal)[source] diag
This function creates a square matrix which has diagonal values specified by diagonal.
- Parameters
diagonal (Variable|numpy.ndarray) – The input tensor specifying diagonal values, should be of rank 1.
- Returns
The tensor variable storing the square matrix.
- Return type
Variable
Examples
# [[3, 0, 0] # [0, 4, 0] # [0, 0, 5] data = fluid.layers.diag(np.arange(3, 6))
fill_constant¶
-
paddle.fluid.layers.
fill_constant
(shape, dtype, value, force_cpu=False, out=None)[source] fill_constant
This function creates a tensor with specified shape and dtype, and initializes it with a constant specifed by value.
The attribute stop_gradient of the created tensor is set to True.
- Parameters
shape (tuple|list|None) – Shape of the output tensor.
dtype (np.dtype|core.VarDesc.VarType|str) – Data type of the output tensor.
value (float) – The constant value used to initialize the output tensor.
out (Variable) – The output tensor.
force_cpu (True|False) – data should be on CPU if set true.
- Returns
The tensor variable storing the output.
- Return type
Variable
Examples
import paddle.fluid as fluid data = fluid.layers.fill_constant(shape=[1], value=0, dtype='int64')
fill_constant_batch_size_like¶
-
paddle.fluid.layers.
fill_constant_batch_size_like
(input, shape, dtype, value, input_dim_idx=0, output_dim_idx=0)[source] This function creates a tensor of specified shape, dtype and batch size, and initializes this with a constant supplied in value. The batch size is obtained from the input tensor.
It also sets stop_gradient to True.
- Parameters
input (Variable) – Tensor whose input_dim_idx’th dimension specifies the batch_size.
shape (INTS) – The shape of the output.
dtype (INT) – It could be numpy.dtype. Output data type. Default is float32.
value (FLOAT) – default 0. The value to be filled.
input_dim_idx (INT) – default 0. The index of input’s batch size dimension.
output_dim_idx (INT) – default 0. The index of output’s batch size dimension.
- Returns
Tensor of specified shape will be filled with the specified value.
Examples
import paddle.fluid as fluid like = fluid.layers.data(name='like', shape=[1], dtype='float32') data = fluid.lgyers.fill_constant_batch_size_like( input=like, shape=[1], value=0, dtype='int64')
has_inf¶
-
paddle.fluid.layers.
has_inf
(x)[source] Test if any of x contains an infinity number
- Parameters
x (variable) – The Tensor/LoDTensor to be checked.
- Returns
The tensor variable storing the output, only a bool value.
- Return type
Variable
Examples
import paddle.fluid as fluid data = fluid.layers.data(name="input", shape=[4, 32, 32], dtype="float32") res = fluid.layers.has_inf(data)
has_nan¶
-
paddle.fluid.layers.
has_nan
(x)[source] Test if any of x contains a NAN
- Parameters
x (variable) – The Tensor/LoDTensor to be checked.
- Returns
The tensor variable storing the output, only a bool value.
- Return type
Variable
Examples
import paddle.fluid as fluid data = fluid.layers.data(name="input", shape=[4, 32, 32], dtype="float32") res = fluid.layers.has_nan(data)
isfinite¶
-
paddle.fluid.layers.
isfinite
(x)[source] Test if any of x contains an infinity/NAN number. If all the elements are finite, returns true, else false.
- Parameters
x (variable) – The Tensor/LoDTensor to be checked.
- Returns
The tensor variable storing the output, contains a bool value.
- Return type
Variable
Examples
import paddle.fluid as fluid var = fluid.layers.data(name="data", shape=(4, 6), dtype="float32") out = fluid.layers.isfinite(v)
linspace¶
-
paddle.fluid.layers.
linspace
(start, stop, num, dtype)[source] Return fixed number of evenly spaced values within a given interval.
First entry is start, and last entry is stop. In the case when Num is 1, only Start is returned. Like linspace function of numpy.
- Parameters
start (float|Variable) – First entry in the sequence. It is a float scalar, or a tensor of shape [1] with type ‘float32’|’float64’.
stop (float|Variable) – Last entry in the sequence. It is a float scalar, or a tensor of shape [1] with type ‘float32’|’float64’.
num (int|Variable) – Number of entry in the sequence. It is an int scalar, or a tensor of shape [1] with type int32.
dtype (string) – ‘float32’|’float64’, the data type of the output tensor.
- Returns
The tensor variable storing a 1-D tensor.
- Return type
Variable
Examples
import paddle.fluid as fluid data = fluid.layers.linspace(0, 10, 5, 'float32') # [0.0, 2.5, 5.0, 7.5, 10.0] data = fluid.layers.linspace(0, 10, 1, 'float32') # [0.0]
ones¶
-
paddle.fluid.layers.
ones
(shape, dtype, force_cpu=False)[source] ones
This function creates a tensor of specified shape and dtype, and initializes this with 1.
It also sets stop_gradient to True.
- Parameters
shape (tuple|list) – Shape of output tensor
dtype (np.dtype|core.VarDesc.VarType|str) – Data type of output tensor
- Returns
The tensor variable storing the output
- Return type
Variable
Examples
import paddle.fluid as fluid data = fluid.layers.ones(shape=[1], dtype='int64')
range¶
-
paddle.fluid.layers.
range
(start, end, step, dtype)[source] Return evenly spaced values within a given interval.
Values are generated within the half-open interval [start, stop) (in other words, the interval including start but excluding stop).
- Parameters
start (int|float|Variable) – Start of interval. The interval includes this value.
end (int|float|Variable) – End of interval. The interval does not include this value, except in some cases where step is not an integer and floating point round-off affects the length of out.
step (int|float|Variable) – Spacing between values. For any output out, this is the distance between two adjacent values, out[i+1] - out[i]. The default step size is 1.
dtype (string) – ‘float32’|’int32’|…, the data type of the output tensor.
- Returns
Evenly spaced values within a given interval.
Examples
import paddle.fluid as fluid data = fluid.layers.range(0, 10, 2, 'int32')
reverse¶
-
paddle.fluid.layers.
reverse
(x, axis)[source] reverse
This function reverse the input ‘x’ along given axises.
- Parameters
x (Vairbale) – the input to be reversed.
axis (int|tuple|list) – Axis that along which order of elements is reversed. If it is a tuple or a list, reversing will be apply on each axis in the tuple or list.
- Returns
The reversed tensor.
- Return type
Variable
Examples
import paddle.fluid as fluid data = fluid.layers.data(name="data", shape=[4, 8], dtype="float32") out = fluid.layers.reverse(x=data, axis=0) # or: out = fluid.layers.reverse(x=data, axis=[0,1])
sums¶
-
paddle.fluid.layers.
sums
(input, out=None)[source] This function performs the sum operation on the input and returns the result as the output.
- Parameters
input (Variable|list) – The input tensor that has the elements that need to be summed up.
out (Variable|None) – Output parameter. The sum result. Default: None
- Returns
the sum of input. The same as the argument ‘out’
- Return type
Variable
Examples
import paddle.fluid as fluid # sum of several tensors a0 = fluid.layers.fill_constant(shape=[1], dtype='int64', value=1) a1 = fluid.layers.fill_constant(shape=[1], dtype='int64', value=2) a2 = fluid.layers.fill_constant(shape=[1], dtype='int64', value=3) sums = fluid.layers.sums(input=[a0, a1, a2]) # sum of a tensor array array = fluid.layers.create_array('int64') i = fluid.layers.zeros(shape=[1], dtype='int64', force_cpu=True) fluid.layers.array_write(a0, array=array, i=i) i = fluid.layers.increment(x=i) fluid.layers.array_write(a1, array=array, i=i) i = fluid.layers.increment(x=i) fluid.layers.array_write(a2, array=array, i=i) sums = fluid.layers.sums(input=array)
tensor_array_to_tensor¶
-
paddle.fluid.layers.
tensor_array_to_tensor
(input, axis=1, name=None)[source] This function concatenates the input LodTensorArray along the axis mentioned and returns that as the output.
A simple example as below:
Given: input.data = {[[0.6, 0.1, 0.3], [0.5, 0.3, 0.2]], [[1.3], [1.8]], [[2.3, 2.1], [2.5, 2.4]]} axis = 1 Then: output.data = [[0.6, 0.1, 0.3, 1.3, 2.3, 2.1], [0.5, 0.3, 0.2, 1.8, 2.5, 2.4]] output_index.data = [3, 1, 2]
- Parameters
input (list) – Input LodTensorArray
axis (int) – Integer axis along which the tensors will be concatenated
name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.
- Returns
Output variable of the concatenation Variable: The input LodTensorArray items’ dims along the axis
- Return type
Variable
Examples
import paddle.fluid as fluid tensor_array = fluid.layers.create_parameter(shape=[784, 200], dtype='float32') output, output_index = fluid.layers.tensor_array_to_tensor(input=tensor_array)
zeros¶
-
paddle.fluid.layers.
zeros
(shape, dtype, force_cpu=False)[source] zeros
This function creates a tensor of specified shape and dtype, and initializes this with 0.
It also sets stop_gradient to True.
- Parameters
shape (tuple|list|None) – Shape of output tensor.
dtype (np.dtype|core.VarDesc.VarType|str) – Data type of output tensor.
force_cpu (bool, default False) – Whether to make output stay on CPU.
- Returns
The tensor variable storing the output.
- Return type
Variable
Examples
import paddle.fluid as fluid data = fluid.layers.zeros(shape=[1], dtype='int64')
zeros_like¶
-
paddle.fluid.layers.
zeros_like
(x, out=None)[source] zeros_like
This function creates a zeros tensor which has identical shape and dtype with x.
- Parameters
x (Variable) – The input tensor which specifies shape and dtype.
out (Variable) – The output tensor.
- Returns
The tensor variable storing the output.
- Return type
Variable
Examples
import paddle.fluid as fluid x = fluid.layers.data(name='x', dtype='float32', shape=[3], append_batch_size=False) data = fluid.layers.zeros_like(x) # [0.0, 0.0, 0.0]