API - Operations¶
Deep learning and Reinforcement learning library for Researchers and Engineers
Get the shape of tensor |
|
|
Creates a tensor with all elements set to zero. |
|
This OP returns a Tensor filled with the value 0, with the same shape and data type (use dtype if dtype is not None) as x. |
|
Creates a tensor with all elements set to ones. |
|
This OP returns a Tensor filled with the value 1, with the same shape and data type (use dtype if dtype is not None) as x. |
|
Creates a constant tensor from a tensor-like object. |
|
Outputs random values from a uniform distribution. |
|
Outputs random values from a normal distribution. |
|
Outputs random values from a truncated normal distribution. |
|
He normal initializer. |
|
Xavier normal. |
|
Xavier uniform. |
|
Creates a new variable with value initial_value. |
|
Computes the absolute value of a tensor. |
|
Computes acos of x element-wise. |
|
Computes inverse hyperbolic cosine of x element-wise. |
|
Returns x + y element-wise. |
|
Returns the element-wise argument of a complex (or real) tensor. |
|
Returns the index with the largest value across axes of a tensor. |
|
Returns the index with the smallest value across axes of a tensor. |
|
Returns the index with the smallest value across axes of a tensor. |
|
Computes inverse hyperbolic sine of x element-wise. |
|
Computes the trignometric inverse tangent of x element-wise. |
|
Computes inverse hyperbolic tangent of x element-wise. |
|
Creates a sequence of numbers. |
Return the ceiling of x as an Integral. |
|
|
Computes cos of x element-wise. |
|
Computes hyperbolic cosine of x element-wise. |
|
Computes number of nonzero elements across dimensions of a tensor. |
|
Compute the cumulative product of the tensor x along axis. |
|
Compute the cumulative sum of the tensor x along axis. |
|
Computes Python style division of x by y. |
|
Returns the truth value of (x == y) element-wise. |
|
Computes exponential of x element-wise. |
Return the floor of x as an Integral. |
|
|
Divides x / y elementwise, rounding toward the most negative integer. |
|
Returns element-wise remainder of division. |
|
Returns the truth value of (x >= y) element-wise. |
|
Returns the truth value of (x >= y) element-wise. |
|
Returns which elements of x are Inf. |
|
Returns which elements of x are NaN. |
|
Normalizes along dimension axis using an L2 norm. |
|
Returns the truth value of (x < y) element-wise. |
|
Returns the truth value of (x <= y) element-wise. |
|
Computes natural logarithm of x element-wise. |
|
Computes log sigmoid of x element-wise. |
|
Returns the max of x and y (i.e. |
|
Returns the min of x and y (i.e. |
|
Returns an element-wise x * y. |
|
Computes numerical negative value element-wise. |
|
Returns the truth value of (x != y) element-wise. |
|
Computes the power of one value to another. |
|
Computes numerical negative value element-wise. |
|
Computes the reciprocal of x element-wise. |
|
Reshapes a tensor. |
|
Concatenates tensors along one dimension. |
|
Converts the given value to a Tensor. |
|
Converts the given Tensor to a numpy. |
|
Computes the maximum of elements across dimensions of a tensor. |
|
Computes the mean of elements across dimensions of a tensor. |
|
Computes the minimum of elements across dimensions of a tensor. |
|
Computes the multiply of elements across dimensions of a tensor. |
|
Computes the standard deviation of elements across dimensions of a tensor. |
|
Computes the standard deviation of elements across dimensions of a tensor. |
|
Computes the variance of elements across dimensions of a tensor. |
|
Rounds the values of a tensor to the nearest integer, element-wise. |
|
Computes reciprocal of square root of x element-wise. |
|
Computes the maximum along segments of a tensor. |
|
Computes the mean along segments of a tensor. |
|
Computes the minimum along segments of a tensor. |
|
Computes the product along segments of a tensor. |
|
Computes the sum along segments of a tensor. |
|
Computes sigmoid of x element-wise. |
|
Computes sign of a tensor element-wise. |
|
Computes sine of a tensor element-wise. |
|
Computes hyperbolic sine of a tensor element-wise. |
|
Computes softplus of a tensor element-wise. |
|
Computes square root of a tensor element-wise. |
|
Computes square of a tensor element-wise. |
|
Computes difference and square between tensor x and tensor y. |
|
Returns x - y element-wise. |
|
Computes tan of a tensor element-wise. |
|
Computes hyperbolic tangent of a tensor element-wise. |
|
Computes logical_or of a tensor element-wise. |
|
Computes logical_and of a tensor element-wise. |
|
Returns the truth value of x AND y element-wise. |
|
Returns the truth value of NOT x element-wise. |
|
Returns the truth value of x OR y element-wise. |
|
Returns the truth value of NOT x element-wise. |
|
Returns the indices of a tensor that give its sorted order along an axis. |
|
Applies batched matrix multiplication to two tensors. |
|
Multiplies matrix a by matrix b, producing a * b. |
|
This op returns the upper triangular part of a matrix (2-D tensor) or batch of matrices x, the other elements of the result tensor are set to 0. |
|
This op returns the lower triangular part of a matrix (2-D tensor) or batch of matrices x, the other elements of the result tensor are set to 0. |
|
Constructs a tensor by tiling a given tensor. |
|
Return a tensor of elements selected from either x or y, depending on condition. |
|
Stacks a list of rank-R tensors into one rank-(R+1) tensor. |
|
Splits a tensor into sub tensors. |
|
Removes dimensions of size 1 from the shape of a tensor. |
|
Inserts a dimension of 1 into a tensor’s shape. |
|
Gather slices from params axis axis according to indices. |
|
Computes the sum along segments of a tensor. |
|
Computes the mean along segments of a tensor. |
|
Computes the min along segments of a tensor. |
|
Computes the max along segments of a tensor. |
|
|
|
|
|
|
|
Applies sparse updates to a variable |
|
|
|
|
|
|
|
Sums the product of the elements of the input operands along dimensions specified using a notation based on the Einstein summation convention. |
|
This function can specify the global device which the OP will run. |
This function can get the specified global device. |
|
|
Returns a copy of Tensor in specified device. |
|
Roll the tensor input along the given dimension(s). |
|
Applies a softmax followed by a logarithm. |
|
Returns the k largest elements of the given input tensor along a given dimension. |
|
Returns the total number of elements in the input tensor. |
TensorLayerX Tensor Operations¶
get_tensor_shape¶
-
tensorlayerx.
get_tensor_shape
(x)[source]¶ Get the shape of tensor
- Parameters:
x (tensor) – type float16, float32, float64, int32, complex64, complex128.
- Returns:
- Return type:
list.
Examples
>>> import tensorlayerx as tlx >>> x_in = tlx.layers.Input((32, 3, 3, 32)) >>> x_shape = tlx.ops.get_tensor_shape(x_in)
zeros¶
-
tensorlayerx.
zeros
(shape, dtype='float32', device=None)[source]¶ Creates a tensor with all elements set to zero.
- Parameters:
shape (A list of integers) – a tuple of integers, or a 1-D Tensor of type int32.
dtype (tensor or str) – The DType of an element in the resulting Tensor
device (str or None) – create a tensor on ‘cpu’ or ‘gpu’, defautl is None.
- Returns:
- Return type:
A Tensor with all elements set to zero.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.zeros((32, 3, 3, 32), dtype=tlx.int32) >>> y = tlx.ops.zeros((10, 25, 25, 10), dtype='float32')
ones¶
-
tensorlayerx.
ones
(shape, dtype='float32', device=None)[source]¶ Creates a tensor with all elements set to ones.
- Parameters:
shape (A list of integers) – a tuple of integers, or a 1-D Tensor of type int32.
dtype (tensor or str) – The DType of an element in the resulting Tensor
device (str or None) – create a tensor on ‘cpu’ or ‘gpu’, defautl is None.
- Returns:
- Return type:
A Tensor with all elements set to zero.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.ones((32, 3, 3, 32), dtype=tlx.int32) >>> y = tlx.ops.ones((10, 25, 25, 10), dtype='float32')
constant¶
-
tensorlayerx.
constant
(value, dtype='float32', shape=None, device=None)[source]¶ Creates a constant tensor from a tensor-like object.
- Parameters:
value (list) – A constant value (or list) of output type dtype.
dtype (tensor or str) – The type of the elements of the resulting tensor.
shape (tuple) – Optional dimensions of resulting tensor.
device (str or None) – create a tensor on ‘cpu’ or ‘gpu’, defautl is None.
- Returns:
- Return type:
A Constant Tensor.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(0.5, (32, 3, 3, 32), dtype=tlx.int32) >>> y = tlx.ops.constant(0.5, (10, 25, 25, 10), dtype='float32')
random_uniform¶
-
tensorlayerx.
random_uniform
(shape, minval=0, maxval=None, dtype='float32', seed=None)[source]¶ Outputs random values from a uniform distribution.
- Parameters:
shape (tuple) – A 1-D integer Tensor or Python array. The shape of the output tensor.
minval (float) – The lower bound on the range of random values to generate (inclusive). Defaults to 0.
maxval (float) – The upper bound on the range of random values to generate (exclusive). Defaults to 1 if dtype is floating point.
dtype (tensor or str) – The type of the output: float16, float32, float64, int32, or int64.
seed (int) – Used in combination with tf.random.set_seed to create a reproducible sequence of tensors across multiple calls.
- Returns:
- Return type:
A tensor of the specified shape filled with random uniform values.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.random_uniform((32, 3, 3, 32), maxval=1.0, dtype=tlx.int32) >>> y = tlx.ops.random_uniform((10, 25, 25, 10), maxval=1.0, dtype='float32')
random_normal¶
-
tensorlayerx.
random_normal
(shape, mean=0.0, stddev=1.0, dtype='float32', seed=None)[source]¶ Outputs random values from a normal distribution.
- Parameters:
shape (tuple) – A 1-D integer Tensor or Python array. The shape of the output tensor.
mean (float) – The mean of the normal distribution
stddev (float) – The standard deviation of the normal distribution.
dtype (tensor or str) – The type of the output.
seed (A Python integer) – Used to create a random seed for the distribution
- Returns:
- Return type:
A tensor of the specified shape filled with random normal values.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.random_normal((32, 3, 3, 32), dtype=tlx.int32) >>> y = tlx.ops.random_normal((10, 25, 25, 10), dtype='float32')
truncated_normal¶
-
tensorlayerx.
truncated_normal
(shape, mean=0.0, stddev=1.0, dtype='float32', seed=None)[source]¶ Outputs random values from a truncated normal distribution.
- Parameters:
shape (tuple) – A 1-D integer Tensor or Python array. The shape of the output tensor.
mean (float) – The mean of the normal distribution
stddev (float) – The standard deviation of the normal distribution.
dtype (tensor or str) – The type of the output.
seed (A Python integer) – Used to create a random seed for the distribution
- Returns:
- Return type:
A tensor of the specified shape filled with random truncated normal values.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.truncated_normal((32, 3, 3, 32), dtype=tlx.int32) >>> y = tlx.ops.truncated_normal((10, 25, 25, 10), dtype='float32')
he_normal¶
-
tensorlayerx.
he_normal
(shape, a=0, mode='fan_in', nonlinearity='leaky_relu', dtype='float32', seed=None)[source]¶ He normal initializer.
- Parameters:
seed (A Python integer.) – Used to seed the random generator.
shape (tuple) – A 1-D integer Tensor or Python array. The shape of the output tensor.
dtype (tensor or str) – The type of the output.
- Returns:
- Return type:
A tensor of the specified shape filled with he normal values.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.he_normal((32, 3, 3, 32), dtype=tlx.int32) >>> y = tlx.ops.he_normal((10, 25, 25, 10), dtype='float32')
xavier_normal¶
-
tensorlayerx.
xavier_normal
(shape, gain=1.0, dtype='float32', seed=None)[source]¶ Xavier normal.
- Parameters:
seed (A Python integer.) – Used to seed the random generator.
shape (tuple) – A 1-D integer Tensor or Python array. The shape of the output tensor.
dtype (tensor or str) – The type of the output.
- Returns:
- Return type:
A tensor of the specified shape filled with xavier normal values.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.xavier_normal((32, 3, 3, 32), dtype=tlx.int32) >>> y = tlx.ops.xavier_normal((10, 25, 25, 10), dtype='float32')
xavier_uniform¶
-
tensorlayerx.
xavier_uniform
(shape, gain=1.0, dtype='float32', seed=None)[source]¶ Xavier uniform.
- Parameters:
seed (A Python integer.) – Used to seed the random generator.
shape (tuple) – A 1-D integer Tensor or Python array. The shape of the output tensor.
dtype (tensor or str) – The type of the output.
- Returns:
- Return type:
A tensor of the specified shape filled with xavier uniform values.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.xavier_uniform((32, 3, 3, 32), dtype=tlx.int32) >>> y = tlx.ops.xavier_uniform((10, 25, 25, 10), dtype='float32')
Variable¶
-
tensorlayerx.
Variable
(initial_value, name, trainable=True, device=None)[source]¶ Creates a new variable with value initial_value.
- Parameters:
initial_value (tensor) – A Tensor, or Python object convertible to a Tensor
name (str) – Optional name for the variable. Defaults to ‘Variable’ and gets uniquified automatically.
device (str or None) – create a tensor on ‘cpu’ or ‘gpu’, defautl is None.
- Returns:
- Return type:
Variable
Examples
>>> import tensorlayerx as tlx >>> x = tlx.Variable(tlx.ops.ones(shape=(10, 20)), name='w')
abs¶
-
tensorlayerx.
abs
(x)[source]¶ Computes the absolute value of a tensor.
- Parameters:
x (tensor) – A Tensor or SparseTensor of type float16, float32, float64, int32, int64, complex64 or complex128.
- Returns:
- Return type:
A Tensor or SparseTensor of the same size, type and sparsity as x, with absolute values.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.layers.Input((32, 3, 3, 32)) >>> y = tlx.ops.abs(x)
acos¶
-
tensorlayerx.
acos
(x)[source]¶ Computes acos of x element-wise.
- Parameters:
x (tensor) – Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, int16, int32, int64, complex64, complex128, string.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.layers.Input((32, 3, 3, 32)) >>> y = tlx.ops.acos(x)
acosh¶
-
tensorlayerx.
acosh
(x)[source]¶ Computes inverse hyperbolic cosine of x element-wise.
- Parameters:
x (tensor) – A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.layers.Input((32, 3, 3, 32)) >>> y = tlx.ops.acosh(x)
add¶
-
tensorlayerx.
add
(value, bias)[source]¶ Returns x + y element-wise.
- Parameters:
value (tensor.) – Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, int16, int32, int64, complex64, complex128, string.
bias (tensor) – Must have the same type as a
- Returns:
- Return type:
A Tensor. Has the same type as a.
Examples
>>> import tensorlayerx as tlx >>> value = tlx.ones(shape=(10, 20)) >>> bias = tlx.ones(shape=(20)) >>> x = tlx.ops.add(value, bias)
angle¶
-
tensorlayerx.
angle
(x)[source]¶ Returns the element-wise argument of a complex (or real) tensor.
- Parameters:
x (tensor) – A Tensor. Must be one of the following types: float, double, complex64, complex128.
- Returns:
- Return type:
A Tensor of type float32 or float64.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[2.15 + 3.57j, 3.89 + 6.54j]) >>> y = tlx.ops.angle(x)
argmax¶
-
tensorlayerx.
argmax
(x, axis=None, keepdim=False, dtype='int64')[source]¶ Returns the index with the largest value across axes of a tensor.
- Parameters:
x (tensor) – A Tensor
axis (int) – An integer, the axis to reduce across. Default to 0.
dtype (tensor or str) – An optional output dtype (nt32 or int64). Defaults to int64.
- Returns:
- Return type:
A Tensor of type output_type.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[10, 20, 5, 6, 15]) >>> y = tlx.ops.argmax(x)
argmin¶
-
tensorlayerx.
argmin
(x, axis=None, dtype='int64')[source]¶ Returns the index with the smallest value across axes of a tensor.
- Parameters:
x (tensor) – A Tensor
axis (int) – An integer, the axis to reduce across. Default to 0.
dtype (tensor or str) – An optional output dtype (nt32 or int64). Defaults to int64.
- Returns:
- Return type:
A Tensor of type output_type.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[10, 20, 5, 6, 15]) >>> y = tlx.ops.argmin(x)
asin¶
-
tensorlayerx.
asin
(x)[source]¶ Returns the index with the smallest value across axes of a tensor.
- Parameters:
x (tensor) – Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[10, 20, 5, 6, 15]) >>> y = tlx.ops.asin(x)
asinh¶
-
tensorlayerx.
asinh
(x)[source]¶ Computes inverse hyperbolic sine of x element-wise.
- Parameters:
x (tensor) – Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[0.9142202 0.72091234]) >>> y = tlx.ops.asinh(x)
atan¶
-
tensorlayerx.
atan
(x)[source]¶ Computes the trignometric inverse tangent of x element-wise.
- Parameters:
x (tensor) – Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[0.9142202 0.72091234]) >>> y = tlx.ops.atan(x)
atanh¶
-
tensorlayerx.
atanh
(x)[source]¶ Computes inverse hyperbolic tangent of x element-wise.
- Parameters:
x (tensor) – Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[0.9142202 0.72091234]) >>> y = tlx.ops.atanh(x)
arange¶
-
tensorlayerx.
arange
(start, limit=None, delta=1, dtype=None)[source]¶ Creates a sequence of numbers.
- Parameters:
start (tensor) – A 0-D Tensor (scalar). Acts as first entry in the range if limit is not None; otherwise, acts as range limit and first entry defaults to 0.
limit (tensor) – A 0-D Tensor (scalar). Upper limit of sequence, exclusive. If None, defaults to the value of start while the first entry of the range defaults to 0.
delta (tensor) – A 0-D Tensor (scalar). Number that increments start. Defaults to 1.
dtype (type) – The type of the elements of the resulting tensor.
- Returns:
- Return type:
An 1-D Tensor of type dtype.
ceil¶
-
tensorlayerx.
ceil
()¶ Return the ceiling of x as an Integral.
This is the smallest integer >= x.
cos¶
-
tensorlayerx.
cos
(x)[source]¶ Computes cos of x element-wise.
- Parameters:
x (tensor) – Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[0.9142202 0.72091234]) >>> y = tlx.ops.cos(x)
cosh¶
-
tensorlayerx.
cosh
(x)[source]¶ Computes hyperbolic cosine of x element-wise.
- Parameters:
x (tensor) – Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[0.9142202 0.72091234]) >>> y = tlx.ops.cosh(x)
count_nonzero¶
-
tensorlayerx.
count_nonzero
(x, axis=None, keepdims=None, dtype='int64')[source]¶ Computes number of nonzero elements across dimensions of a tensor.
- Parameters:
x (tensor) – The tensor to reduce. Should be of numeric type, bool, or string.
axis (int) – The dimensions to reduce. If None (the default), reduces all dimensions. Must be in the range [-rank(input), rank(input)).
keepdims (bool) – If true, retains reduced dimensions with length 1.
dtype (tensor or str) – The output dtype; defaults to tf.int64.
- Returns:
- Return type:
The reduced tensor (number of nonzero values)
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=["", "a", "c", "b", " "]) >>> y = tlx.ops.count_nonzero(x)
cumprod¶
-
tensorlayerx.
cumprod
(x, axis=0, exclusive=False, reverse=False)[source]¶ Compute the cumulative product of the tensor x along axis.
- Parameters:
x (tensor) –
- Must be one of the following types: float32, float64, int64, int32, uint8, uint16, int16, int8,
complex64, complex128, qint8, quint8, qint32, half.
axis (int) – A Tensor of type int32 (default: 0). Must be in the range [-rank(x), rank(x)).
exclusive (bool) – If True, perform exclusive cumprod.
reverse (bool) – A bool (default: False).
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[3, 2, 1]) >>> y = tlx.ops.cumprod(x) >>> y = tlx.ops.cumprod(x, exclusive=True, reverse=True)
cumsum¶
-
tensorlayerx.
cumsum
(x, axis=0, exclusive=False, reverse=False)[source]¶ Compute the cumulative sum of the tensor x along axis.
- Parameters:
x (tensor) –
- Must be one of the following types: float32, float64, int64, int32, uint8, uint16, int16, int8,
complex64, complex128, qint8, quint8, qint32, half.
axis (int) – A Tensor of type int32 (default: 0). Must be in the range [-rank(x), rank(x)).
exclusive (bool) – If True, perform exclusive cumprod.
reverse (bool) – A bool (default: False).
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.cumsum(x) >>> y = tlx.ops.cumsum(x, exclusive=True, reverse=True)
divide¶
-
tensorlayerx.
divide
(x, y)[source]¶ Computes Python style division of x by y.
- Parameters:
x (tensor) – A Tensor
y (tensor) – A Tensor
- Returns:
- Return type:
A Tensor with same shape as input
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[0.9142202 0.72091234]) >>> y = tlx.ops.divide(x, x)
equal¶
-
tensorlayerx.
equal
(x, y)[source]¶ Returns the truth value of (x == y) element-wise.
- Parameters:
x (tensor) – A Tensor or SparseTensor or IndexedSlices.
y (tensor) – A Tensor or SparseTensor or IndexedSlices.
- Returns:
- Return type:
A Tensor of type bool with the same size as that of x or y.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.equal(x, x)
exp¶
-
tensorlayerx.
exp
(x)[source]¶ Computes exponential of x element-wise.
- Parameters:
x (tensor) – Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.exp(x)
floor¶
-
tensorlayerx.
floor
()¶ Return the floor of x as an Integral.
This is the largest integer <= x.
floordiv¶
-
tensorlayerx.
floordiv
(x, y)[source]¶ Divides x / y elementwise, rounding toward the most negative integer.
- Parameters:
x (tensor) – Tensor numerator of real numeric type.
y (tensor) – Tensor denominator of real numeric type.
- Returns:
- Return type:
x / y rounded toward -infinity.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.floordiv(x, x)
floormod¶
-
tensorlayerx.
floormod
(x, y)[source]¶ Returns element-wise remainder of division. When x < 0 xor y < 0 is true, this follows Python semantics in that the result here is consistent with a flooring divide. E.g. floor(x / y) * y + mod(x, y) = x.
- Parameters:
x (tensor) – Must be one of the following types: int8, int16, int32, int64, uint8, uint16, uint32, uint64, bfloat16, half, float32, float64.
y (tensor) – A Tensor. Must have the same type as x.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.floormod(x, x)
greater¶
-
tensorlayerx.
greater
(x, y)[source]¶ Returns the truth value of (x >= y) element-wise.
- Parameters:
x (tensor) – Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64.
y (tensor) – A Tensor. Must have the same type as x.
- Returns:
- Return type:
A Tensor of type bool.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.greater(x, x)
greater_equal¶
-
tensorlayerx.
greater_equal
(x, y)[source]¶ Returns the truth value of (x >= y) element-wise.
- Parameters:
x (tensor) – Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64.
y (tensor) – A Tensor. Must have the same type as x.
- Returns:
- Return type:
A Tensor of type bool.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.greater_equal(x, x)
is_inf¶
-
tensorlayerx.
is_inf
(x)[source]¶ Returns which elements of x are Inf.
- Parameters:
x (tensor) – A Tensor. Must be one of the following types: bfloat16, half, float32, float64.
- Returns:
- Return type:
A Tensor of type bool.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.constant(value=[1, 2, 3, np.inf]) >>> y = tlx.ops.is_inf(x)
is_nan¶
-
tensorlayerx.
is_nan
(x)[source]¶ Returns which elements of x are NaN.
- Parameters:
x (tensor) – A Tensor. Must be one of the following types: bfloat16, half, float32, float64.
- Returns:
- Return type:
A Tensor of type bool.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.constant(value=[1, 2, 3, np.nan]) >>> y = tlx.ops.is_nan(x)
l2_normalize¶
-
tensorlayerx.
l2_normalize
(x, axis=None, eps=1e-12)[source]¶ Normalizes along dimension axis using an L2 norm. For a 1-D tensor with axis = 0, computes output = x / sqrt(max(sum(x**2), epsilon))
- Parameters:
x (tensor) – A Tensor
axis (int) – Dimension along which to normalize. A scalar or a vector of integers.
eps (float) – A lower bound value for the norm. Will use sqrt(epsilon) as the divisor if norm < sqrt(epsilon).
- Returns:
- Return type:
A Tensor with the same shape as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.constant(value=[1, 2, 3, np.nan]) >>> y = tlx.ops.l2_normalize(x)
less¶
-
tensorlayerx.
less
(x, y)[source]¶ Returns the truth value of (x < y) element-wise.
- Parameters:
x (tensor) – Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64.
y (tensor) – A Tensor. Must have the same type as x.
- Returns:
- Return type:
A Tensor of type bool.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.less(x, x)
less_equal¶
-
tensorlayerx.
less_equal
(x, y)[source]¶ Returns the truth value of (x <= y) element-wise.
- Parameters:
x (tensor) – Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64.
y (tensor) – A Tensor. Must have the same type as x.
- Returns:
- Return type:
A Tensor of type bool.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.less_equal(x, x)
log¶
-
tensorlayerx.
log
(x)[source]¶ Computes natural logarithm of x element-wise.
- Parameters:
x (tensor) – Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.log(x)
log_sigmoid¶
-
tensorlayerx.
log_sigmoid
(x)[source]¶ Computes log sigmoid of x element-wise.
- Parameters:
x (tensor) – A Tensor with type float32 or float64.
- Returns:
- Return type:
A Tensor with the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.log_sigmoid(x)
maximum¶
-
tensorlayerx.
maximum
(x, y)[source]¶ Returns the max of x and y (i.e. x > y ? x : y) element-wise.
- Parameters:
x (tensor) – Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64.
y (tensor) – A Tensor. Must have the same type as x.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.maximum(x, x)
minimum¶
-
tensorlayerx.
minimum
(x, y)[source]¶ Returns the min of x and y (i.e. x < y ? x : y) element-wise.
- Parameters:
x (tensor.) – Must be one of the following types: bfloat16, half, float32, float64, int32, int64.
y (A Tensor.) – Must have the same type as x.
- Returns:
- Return type:
A Tensor. Has the same type as x
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant([0., 0., 0., 0.]) >>> y = tlx.ops.constant([-5., -2., 0., 3.]) >>> z = tlx.ops.minimum(x, y)
multiply¶
-
tensorlayerx.
multiply
(x, y)[source]¶ Returns an element-wise x * y.
- Parameters:
x (tensor) – Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128.
y (tensor) – A Tensor. Must have the same type as x.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[0.9142202 0.72091234]) >>> y = tlx.ops.multiply(x, x)
negative¶
-
tensorlayerx.
negative
(x)[source]¶ Computes numerical negative value element-wise.
- Parameters:
x (tensor) – Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128.
- Returns:
A Tensor. Has the same type as x.
If x is a SparseTensor, returns SparseTensor(x.indices, tf.math.negative(x.values, …), x.dense_shape)
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.negative(x)
not_equal¶
-
tensorlayerx.
not_equal
(x, y)[source]¶ Returns the truth value of (x != y) element-wise.
- Parameters:
x (tensor) – A Tensor or SparseTensor or IndexedSlices.
y (tensor) – A Tensor or SparseTensor or IndexedSlices.
- Returns:
- Return type:
A Tensor of type bool with the same size as that of x or y.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.constant(value=[1, 3, 5]) >>> x = tlx.ops.not_equal(x, y)
pow¶
-
tensorlayerx.
pow
(x, y)[source]¶ Computes the power of one value to another.
- Parameters:
x (tensor) – A Tensor of type float16, float32, float64, int32, int64, complex64, or complex128.
y (tensor) – A Tensor of type float16, float32, float64, int32, int64, complex64, or complex128.
- Returns:
- Return type:
A Tensor.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[1, 2, 3]) >>> y = tlx.ops.constant(value=[1, 3, 5]) >>> x = tlx.ops.pow(x, y)
real¶
-
tensorlayerx.
real
(x)[source]¶ Computes numerical negative value element-wise.
- Parameters:
x (tensor) – A Tensor. Must have numeric type.
- Returns:
- Return type:
A Tensor of type float32 or float64.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[-2.25 + 4.75j, 3.25 + 5.75j]) >>> y = tlx.ops.real(x)
reciprocal¶
-
tensorlayerx.
reciprocal
(x)[source]¶ Computes the reciprocal of x element-wise.
- Parameters:
x (tensor) – A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant(value=[-2.25, 3.25]) >>> y = tlx.ops.reciprocal(x)
reshape¶
-
tensorlayerx.
reshape
(tensor, shape)[source]¶ Reshapes a tensor.
- Parameters:
tensor (tensor) – A Tensor.
shape (tensor) – Defines the shape of the output tensor.
- Returns:
- Return type:
A Tensor. Has the same type as tensor
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant([0., 1., 2., 3.]) >>> z = tlx.ops.reshape(x, [2, 2])
concat¶
-
tensorlayerx.
concat
(values, axis)[source]¶ Concatenates tensors along one dimension.
- Parameters:
values (list) – A list of Tensor objects or a single Tensor
axis (int) – 0-D int32 Tensor. Dimension along which to concatenate
- Returns:
- Return type:
A Tensor resulting from concatenation of the input tensors.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant([0., 0., 0., 0.]) >>> y = tlx.ops.constant([-5., -2., 0., 3.]) >>> z = tlx.ops.concat([x, y], 0)
convert_to_tensor¶
-
tensorlayerx.
convert_to_tensor
(value, dtype=None, device=None)[source]¶ Converts the given value to a Tensor.
- Parameters:
value (object) – An object whose type has a registered Tensor conversion function.
dtype (optional) – Optional element type for the returned tensor. If missing, the type is inferred from the type of value.
device (str or None) – create a tensor on ‘cpu’ or ‘gpu’, defautl is None.
- Returns:
- Return type:
A Tensor based on value.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = np.ones(shape=(10, 10)) >>> y = tlx.ops.convert_to_tensor(x)
convert_to_numpy¶
-
tensorlayerx.
convert_to_numpy
(value)[source]¶ Converts the given Tensor to a numpy.
- Parameters:
value (object) – An object whose type has a registered Tensor conversion function.
- Returns:
- Return type:
A value based on tensor.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.ones(shape=(10, 10)) >>> y = tlx.ops.convert_to_numpy(x)
reduce_max¶
-
tensorlayerx.
reduce_max
(x, axis=None, keepdims=False)[source]¶ Computes the maximum of elements across dimensions of a tensor.
- Parameters:
x (tensor) – The tensor to reduce. Should have real numeric type.
axis (int) – The dimensions to reduce. If None (the default), reduces all dimensions. Must be in the range [-rank(x), rank(x)).
keepdims (boolean) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default : False, don’t keep these reduced dimensions.
- Returns:
- Return type:
The reduced tensor.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.random.randn(3, 4)) >>> x1 = tlx.ops.reduce_max(x, axis=1, keepdims=False) >>> x2 = tlx.ops.reduce_max(x, axis=1, keepdims=True)
reduce_mean¶
-
tensorlayerx.
reduce_mean
(input_tensor, axis=None, keepdims=False)[source]¶ Computes the mean of elements across dimensions of a tensor.
- Parameters:
x (tensor) – The tensor to reduce. Should have numeric type.
axis (list) – The dimensions to reduce. If None (the default), reduces all dimensions. Must be in the range [-rank(x), rank(x)).
keepdims (boolean) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default : False, don’t keep these reduced dimensions.
- Returns:
- Return type:
The reduced tensor.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.random.randn(3, 4)) >>> x1 = tlx.ops.reduce_mean(x, axis=1, keepdims=False) >>> x2 = tlx.ops.reduce_mean(x, axis=1, keepdims=True)
reduce_min¶
-
tensorlayerx.
reduce_min
(x, axis=None, keepdims=False)[source]¶ Computes the minimum of elements across dimensions of a tensor.
- Parameters:
x (tensor) – The tensor to reduce. Should have real numeric type.
axis (int) – The dimensions to reduce. If None (the default), reduces all dimensions. Must be in the range [-rank(x), rank(x)).
keepdims (boolean) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default : False, don’t keep these reduced dimensions.
- Returns:
- Return type:
The reduced tensor.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.random.randn(3, 4)) >>> x1 = tlx.ops.reduce_min(x, axis=1, keepdims=False) >>> x2 = tlx.ops.reduce_min(x, axis=1, keepdims=True)
reduce_prod¶
-
tensorlayerx.
reduce_prod
(x, axis=None, keepdims=False)[source]¶ Computes the multiply of elements across dimensions of a tensor.
- Parameters:
x (tensor) – The tensor to reduce. Should have real numeric type.
axis (int) – The dimensions to reduce. If None (the default), reduces all dimensions. Must be in the range [-rank(x), rank(x)).
keepdims (boolean) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default : False, don’t keep these reduced dimensions.
- Returns:
- Return type:
The reduced tensor.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.random.randn(3, 4)) >>> x1 = tlx.ops.reduce_prod(x, axis=1, keepdims=False) >>> x2 = tlx.ops.reduce_prod(x, axis=1, keepdims=True)
reduce_std¶
-
tensorlayerx.
reduce_std
(x, axis=None, keepdims=False)[source]¶ Computes the standard deviation of elements across dimensions of a tensor.
- Parameters:
x (tensor) – The tensor to reduce. Should have real numeric type.
axis (int) – The dimensions to reduce. If None (the default), reduces all dimensions. Must be in the range [-rank(x), rank(x)).
keepdims (boolean) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default : False, don’t keep these reduced dimensions.
- Returns:
- Return type:
The reduced tensor.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.random.randn(3, 4)) >>> x1 = tlx.ops.reduce_std(x, axis=1, keepdims=False) >>> x2 = tlx.ops.reduce_std(x, axis=1, keepdims=True)
reduce_sum¶
-
tensorlayerx.
reduce_sum
(x, axis=None, keepdims=False)[source]¶ Computes the standard deviation of elements across dimensions of a tensor.
- Parameters:
x (tensor) – The tensor to reduce. Should have real numeric type.
axis (int) – The dimensions to reduce. If None (the default), reduces all dimensions. Must be in the range [-rank(x), rank(x)).
keepdims (boolean) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default : False, don’t keep these reduced dimensions.
- Returns:
- Return type:
The reduced tensor.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.random.randn(3, 4)) >>> x1 = tlx.ops.reduce_sum(x, axis=1, keepdims=False) >>> x2 = tlx.ops.reduce_sum(x, axis=1, keepdims=True)
reduce_variance¶
-
tensorlayerx.
reduce_variance
(x, axis=None, keepdims=False)[source]¶ Computes the variance of elements across dimensions of a tensor.
- Parameters:
x (tensor) – The tensor to reduce. Should have real numeric type.
axis (int) – The dimensions to reduce. If None (the default), reduces all dimensions. Must be in the range [-rank(x), rank(x)).
keepdims (boolean) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default : False, don’t keep these reduced dimensions.
- Returns:
- Return type:
The reduced tensor.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.random.randn(3, 4)) >>> x1 = tlx.ops.reduce_variance(x, axis=1, keepdims=False) >>> x2 = tlx.ops.reduce_variance(x, axis=1, keepdims=True)
round¶
-
tensorlayerx.
round
(x)[source]¶ Rounds the values of a tensor to the nearest integer, element-wise.
- Parameters:
x (tensor) – The tensor to round. Should have real numeric type.
- Returns:
- Return type:
A Tensor of same shape and type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([0.9, 2.5, 2.3, 1.5, -4.5])) >>> x = tlx.ops.round(x)
rsqrt¶
-
tensorlayerx.
rsqrt
(x)[source]¶ Computes reciprocal of square root of x element-wise.
- Parameters:
x (tensor) – The tensor to rsqrt. Should have real numeric type.
- Returns:
- Return type:
A Tensor of same shape and type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([0.9, 2.5, 2.3, 1.5])) >>> x = tlx.ops.rsqrt(x)
segment_max¶
-
tensorlayerx.
segment_max
(x, segment_ids)[source]¶ Computes the maximum along segments of a tensor.
- Parameters:
x (tensor) – The tensor to segment_max. Should have real numeric type.
segment_ids (tensor) – A 1-D tensor whose size is equal to the size of data’s first dimension. Values should be sorted and can be repeated.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([[1,2,3,4], [4, 3, 2, 1], [5,6,7,8]])) >>> id = tlx.ops.convert_to_tensor([0, 0, 1]) >>> x = tlx.ops.segment_max(x, id) >>> print(x) >>> [[4, 3, 3, 4], >>> [5, 6, 7, 8]]
segment_mean¶
-
tensorlayerx.
segment_mean
(x, segment_ids)[source]¶ Computes the mean along segments of a tensor.
- Parameters:
x (tensor) – The tensor to segment_mean. Should have real numeric type.
segment_ids (tensor) – A 1-D tensor whose size is equal to the size of data’s first dimension. Values should be sorted and can be repeated.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([[1.0 , 2, 3, 4], [4, 3, 2, 1], [5, 6, 7, 8]])) >>> id = tlx.ops.convert_to_tensor([0, 0, 1]) >>> x = tlx.ops.segment_mean(x, id) >>> print(x) >>> [[2.5, 2.5, 2.5, 2.5], >>> [5, 6, 7, 8]]
segment_min¶
-
tensorlayerx.
segment_min
(x, segment_ids)[source]¶ Computes the minimum along segments of a tensor.
- Parameters:
x (tensor) – The tensor to segment_minimum. Should have real numeric type.
segment_ids (tensor) – A 1-D tensor whose size is equal to the size of data’s first dimension. Values should be sorted and can be repeated.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([[1, 2, 3, 4], [4, 3, 2, 1], [5, 6, 7, 8]])) >>> id = tlx.ops.convert_to_tensor([0, 0, 1]) >>> x = tlx.ops.segment_minimum(x, id) >>> print(x) >>> [[1, 2, 2, 1], >>> [5, 6, 7, 8]]
segment_prod¶
-
tensorlayerx.
segment_prod
(x, segment_ids)[source]¶ Computes the product along segments of a tensor.
- Parameters:
x (tensor) – The tensor to segment_prod. Should have real numeric type.
segment_ids (tensor) – A 1-D tensor whose size is equal to the size of data’s first dimension. Values should be sorted and can be repeated.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([[1, 2, 3, 4], [4, 3, 2, 1], [5, 6, 7, 8]])) >>> id = tlx.ops.convert_to_tensor([0, 0, 1]) >>> x = tlx.ops.segment_prod(x, id) >>> print(x) >>> [[4, 6, 6, 4], >>> [5, 6, 7, 8]]
segment_sum¶
-
tensorlayerx.
segment_sum
(x, segment_ids)[source]¶ Computes the sum along segments of a tensor.
- Parameters:
x (tensor) – The tensor to segment_sum. Should have real numeric type.
segment_ids (tensor) – A 1-D tensor whose size is equal to the size of data’s first dimension. Values should be sorted and can be repeated.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([[1, 2, 3, 4], [4, 3, 2, 1], [5, 6, 7, 8]])) >>> id = tlx.ops.convert_to_tensor([0, 0, 1]) >>> x = tlx.ops.segment_sum(x, id) >>> print(x) >>> [[5, 5, 5, 5], >>> [5, 6, 7, 8]]
sigmoid¶
sign¶
-
tensorlayerx.
sign
(x)[source]¶ Computes sign of a tensor element-wise.
- Parameters:
x (tensor) – The tensor to sign. y = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0.
- Returns:
- Return type:
A Tensor with the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([-128.0, 0.0, 128.0]), dtype='float32') >>> x = tlx.ops.sign(x) >>> print(x) >>> [-1., 0., 1.]
sin¶
-
tensorlayerx.
sin
(x)[source]¶ Computes sine of a tensor element-wise.
- Parameters:
x (tensor) – The tensor to sin. Input range is (-inf, inf) and output range is [-1,1].
- Returns:
- Return type:
A Tensor with the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([-1.0, 0.0, 1.0]), dtype='float32') >>> x = tlx.ops.sin(x) >>> print(x) >>> [-0.84147096, 0., 0.84147096]
sinh¶
-
tensorlayerx.
sinh
(x)[source]¶ Computes hyperbolic sine of a tensor element-wise.
- Parameters:
x (tensor) – The tensor to hyperbolic sin. Input range is (-inf, inf) and output range is [-inf,inf].
- Returns:
- Return type:
A Tensor with the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([-1.0, 0.0, 1.0]), dtype='float32') >>> x = tlx.ops.sinh(x) >>> print(x) >>> [-1.1752012, 0., 1.1752012]
softplus¶
-
tensorlayerx.
softplus
(x)[source]¶ Computes softplus of a tensor element-wise.
- Parameters:
x (tensor) – The tensor to softplus. softplus(x) = log(exp(x) + 1).
- Returns:
- Return type:
A Tensor with the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([-1.0, 0.0, 1.0]), dtype='float32') >>> x = tlx.ops.softplus(x) >>> print(x) >>> [0.3132617, 0.6931472, 1.3132616]
sqrt¶
-
tensorlayerx.
sqrt
(x)[source]¶ Computes square root of a tensor element-wise.
- Parameters:
x (tensor) – Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([0.0, 1.0, 4.0]), dtype=tlx.float32) >>> x = tlx.ops.sqrt(x) >>> print(x) >>> [0.0, 1.0, 2.0]
square¶
-
tensorlayerx.
square
(x)[source]¶ Computes square of a tensor element-wise.
- Parameters:
x (tensor) – The tensor to square.
- Returns:
- Return type:
A Tensor with the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([-1.0, 0.0, 1.0]), dtype='float32') >>> x = tlx.ops.square(x) >>> print(x) >>> [1.0, 0.0, 1.0]
squared_difference¶
-
tensorlayerx.
squared_difference
(x, y)[source]¶ Computes difference and square between tensor x and tensor y. return square(x - y)
- Parameters:
x (tensor) – A Tensor.
y (tensor) – A Tensor. Must have the same type as x.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([[1,0,1], [2,3,4]]), dtype='float32') >>> y = tlx.ops.convert_to_tensor(np.array([[-1,0,1], [2,3,4]]), dtype='float32') >>> res = tlx.ops.squared_difference(x, y) >>> print(res) >>> [[4.0, 0.0, 0.0], >>> [0.0, 0.0, 0.0]]
subtract¶
-
tensorlayerx.
subtract
(x, y)[source]¶ Returns x - y element-wise.
- Parameters:
x (tensor) – A tensor.
y (tensor) – A Tensor. Must have the same type as x.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([[1,0,1], [2,3,4]]), dtype='float32') >>> y = tlx.ops.convert_to_tensor(np.array([[-1,0,1], [2,3,4]]), dtype='float32') >>> res = tlx.ops.subtract(x, y) >>> print(res) >>> [[-2.0, 0.0, 0.0], >>> [0.0, 0.0, 0.0]]
tan¶
-
tensorlayerx.
tan
(x)[source]¶ Computes tan of a tensor element-wise.
- Parameters:
x (tensor) – The tensor to tan.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([1,0,1]), dtype='float32') >>> res = tlx.ops.tan(x) >>> print(res) >>> [-1.5574077, 0.0, 1.5574077]
tanh¶
-
tensorlayerx.
tanh
(x)[source]¶ Computes hyperbolic tangent of a tensor element-wise.
- Parameters:
x (tensor) – The tensor to tanh.
- Returns:
- Return type:
A Tensor. Has the same type as x.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([1,0,1]), dtype="float32") >>> res = tlx.ops.tanh(x) >>> print(res) >>> [-0.7615942, 0.0, 0.7615942]
any¶
-
tensorlayerx.
any
(x, axis=None, keepdims=False)[source]¶ Computes logical_or of a tensor element-wise.
- Parameters:
x (tensor) – The boolean tensor to reduce.
axis (int or None) – The dimensions to reduce. If None (the default), reduces all dimensions. Must be in the range [-rank(x),rank(x)).
keepdims (boolean) – If true, retains reduced dimensions with length 1.
- Returns:
- Return type:
The reduced tensor.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([1,0,1]), dtype='bool') >>> res = tlx.ops.any(x, axis = None, keepdims = False) >>> print(res) >>> True
all¶
-
tensorlayerx.
all
(x, axis=None, keepdims=False)[source]¶ Computes logical_and of a tensor element-wise.
- Parameters:
x (tensor) – The boolean tensor to reduce.
axis (int or None) – The dimensions to reduce. If None (the default), reduces all dimensions. Must be in the range [-rank(x),rank(x)).
keepdims (boolean) – If true, retains reduced dimensions with length 1.
- Returns:
- Return type:
The reduced tensor.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.array([1,0,1]), dtype='bool') >>> res = tlx.ops.all(x, axis = None, keepdims = False) >>> print(res) >>> False
logical_and¶
-
tensorlayerx.
logical_and
(x, y)[source]¶ Returns the truth value of x AND y element-wise.
- Parameters:
x (tensor) – A tf.Tensor of type bool.
y (tensor) – A tf.Tensor of type bool.
- Returns:
- Return type:
A Tensor of type bool.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.constant([False, False, True, True]) >>> y = tlx.constant([False, True, False, True]) >>> res = tlx.ops.logical_and(x, y) >>> print(res) >>> [False, False, False, True]
logical_not¶
-
tensorlayerx.
logical_not
(x)[source]¶ Returns the truth value of NOT x element-wise.
- Parameters:
x (tensor) – A tf.Tensor of type bool.
- Returns:
- Return type:
A Tensor of type bool.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.constant([False, False, True, True]) >>> res = tlx.ops.logical_not(x, y) >>> print(res) >>> [True, True, False, False]
logical_or¶
-
tensorlayerx.
logical_or
(x, y)[source]¶ Returns the truth value of x OR y element-wise.
- Parameters:
x (tensor) – A tf.Tensor of type bool.
y (tensor) – A tf.Tensor of type bool.
- Returns:
- Return type:
A Tensor of type bool.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.constant([False, False, True, True]) >>> y = tlx.constant([False, True, False, True]) >>> res = tlx.ops.logical_or(x, y) >>> print(res) >>> [False, True, True, True]
logical_xor¶
-
tensorlayerx.
logical_xor
(x, y)[source]¶ Returns the truth value of NOT x element-wise. x ^ y = (x | y) & ~(x & y)
- Parameters:
x (tensor) – A tf.Tensor of type bool.
- Returns:
- Return type:
A Tensor of type bool.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.constant([False, False, True, True]) >>> y = tlx.constant([False, True, False, True]) >>> res = tlx.ops.logical_xor(x, y) >>> print(res) >>> [False, True, True, False]
argsort¶
-
tensorlayerx.
argsort
(x, axis=-1, descending=False)[source]¶ Returns the indices of a tensor that give its sorted order along an axis.
- Parameters:
x (tensor) – An input N-D Tensor
axis (int or None) – The axis along which to sort. The default is -1, which sorts the last axis.
descending (boolean) – Descending is a flag, if set to true, algorithm will sort by descending order, else sort by ascending order. Default is false.
- Returns:
- Return type:
A Tensor with the same shape as values.
Examples
>>> import tensorlayerx as tlx >>> x = [1, 10, 26.9, 2.8, 166.32, 62.3] >>> y = tlx.ops.argsort(x, descending = False) >>> print(y) >>> [0, 3, 1, 2, 5, 4]
bmm¶
-
tensorlayerx.
bmm
(x, y)[source]¶ Applies batched matrix multiplication to two tensors. Both of the two input tensors must be three-dementional and share the same batch size. if x is a (b, m, k) tensor, y is a (b, k, n) tensor, the output will be a (b, m, n) tensor.
- Parameters:
x (tensor) – The input Tensor.
y (tensor) – The input Tensor.
- Returns:
- Return type:
The product Tensor.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.convert_to_tensor([[[1.0, 1.0, 1.0],[2.0, 2.0, 2.0]],[[3.0, 3.0, 3.0],[4.0, 4.0, 4.0]]]) >>> y = tlx.convert_to_tensor([[[1.0, 1.0],[2.0, 2.0],[3.0, 3.0]],[[4.0, 4.0],[5.0, 5.0],[6.0, 6.0]]]) >>> res = tlx.ops.bmm(x, y) >>> print(res) >>> [[[6. , 6. ], >>> [12., 12.]], >>> [[45., 45.], >>> [60., 60.]]]
matmul¶
-
tensorlayerx.
matmul
(a, b, transpose_a=False, transpose_b=False)[source]¶ Multiplies matrix a by matrix b, producing a * b.
- Parameters:
a (tensor) – type float16, float32, float64, int32, complex64, complex128 and rank > 1.
b (tensor) – with same type and rank as a.
transpose_a (boolean) – If True, a is transposed before multiplication.
transpose_b (boolean) – If True, b is transposed before multiplication.
- Returns:
- Return type:
A Tensor of the same type as a and b
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.convert_to_tensor(np.random.random([2,3,2]), dtype="float32") >>> y = tlx.convert_to_tensor(np.random.random([2,2,3]), dtype="float32") >>> z = tlx.ops.matmul(x, y) >>> print(z.shape) >>> [2,3,3]
triu¶
-
tensorlayerx.
triu
(x, diagonal=0)[source]¶ This op returns the upper triangular part of a matrix (2-D tensor) or batch of matrices x, the other elements of the result tensor are set to 0. The upper triangular part of the matrix is defined as the elements on and above the diagonal.
- Parameters:
x (tensor) – The tensor to triu.
diagonal (int) – The diagonal to consider, default value is 0. If diagonal = 0, all elements on and above the main diagonal are retained. A positive value excludes just as many diagonals above the main diagonal, and similarly a negative value includes just as many diagonals below the main diagonal.
- Returns:
- Return type:
Results of upper triangular operation by the specified diagonal of input tensor x, it’s data type is the same as x’s Tensor.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.convert_to_tensor(np.arange(1, 10, dtype="int32").reshape(3,-1)) >>> y = tlx.ops.triu(x, diagonal=1) >>> print(y) >>> [[0, 2, 3], >>> [ 0, 0, 6], >>> [ 0, 0, 0]]
tril¶
-
tensorlayerx.
tril
(x, diagonal=0)[source]¶ This op returns the lower triangular part of a matrix (2-D tensor) or batch of matrices x, the other elements of the result tensor are set to 0. The lower triangular part of the matrix is defined as the elements on and below the diagonal.
- Parameters:
x (tensor) – The tensor to tril.
diagonal (int) – The diagonal to consider, default value is 0. If diagonal = 0, all elements on and below the main diagonal are retained. A positive value includes just as many diagonals above the main diagonal, and similarly a negative value excludes just as many diagonals below the main diagonal.
- Returns:
- Return type:
Results of lower triangular operation by the specified diagonal of input tensor x, it’s data type is the same as x’s Tensor.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.convert_to_tensor(np.arange(1, 10, dtype="int32").reshape(3,-1)) >>> y = tlx.ops.tril(x, diagonal=1) >>> print(y) >>> [[0, 0, 0], >>> [ 4, 0, 0], >>> [ 7, 8, 0]]
tile¶
-
tensorlayerx.
tile
(input, multiples)[source]¶ Constructs a tensor by tiling a given tensor.
- Parameters:
input (tensor) – A Tensor. 1-D or higher.
multiples (tensor or tuple or list) – The number of repeating times. If repeat_times is a list or tuple, all its elements should be integers or 1-D Tensors with the data type int32. If repeat_times is a Tensor, it should be an 1-D Tensor with the data type int32. Length must be the same as the number of dimensions in input.
- Returns:
- Return type:
A Tensor. Has the same type as input.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant([[1,2,3],[1,2,3]]) >>> y = tlx.ops.tile(x, [2, 1]) >>> [[1, 2, 3], >>> [1, 2, 3], >>> [1, 2, 3], >>> [1, 2, 3]]
where¶
-
tensorlayerx.
where
(condition, x, y)[source]¶ Return a tensor of elements selected from either x or y, depending on condition.
- Parameters:
condition (tensor of bool) – When True (nonzero), yield x, otherwise yield y
x (tensor) – values selected at indices where condition is True
y (tensor) – values selected at indices where condition is False
- Returns:
- Return type:
A tensor of shape equal to the broadcasted shape of condition, x, y
Examples
>>> import tensorlayerx as tlx >>> x = tlx.convert_to_tensor([0.9, 0.1, 3.2, 1.2]) >>> y = tlx.convert_to_tensor([1.0, 1.0, 1.0, 1.0]) >>> res = tlx.ops.where(x>1, x, y) >>> print(res) >>> [1.0, 1.0, 3.2, 1.2]
ones_like¶
-
tensorlayerx.
ones_like
(x, dtype=None)[source]¶ This OP returns a Tensor filled with the value 1, with the same shape and data type (use dtype if dtype is not None) as x.
- Parameters:
x (tensor) – The input tensor which specifies shape and dtype.
dtype (str) – A type for the returned Tensor.If dtype is None, the data type is the same as x. Default is None.
- Returns:
- Return type:
A Tensor filled with the value 1, with the same shape and data type (use dtype if dtype is not None) as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.convert_to_tensor([0.9, 0.1, 3.2, 1.2]) >>> res = tlx.ops.ones_like(x, dtype="int32") >>> print(res) >>> [1, 1, 1, 1]
zeros_like¶
-
tensorlayerx.
zeros_like
(x, dtype=None)[source]¶ This OP returns a Tensor filled with the value 0, with the same shape and data type (use dtype if dtype is not None) as x.
- Parameters:
x (tensor) – The input tensor which specifies shape and dtype.
dtype (str) – A type for the returned Tensor.If dtype is None, the data type is the same as x. Default is None.
- Returns:
- Return type:
A Tensor filled with the value 0, with the same shape and data type (use dtype if dtype is not None) as x.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.convert_to_tensor([0.9, 0.1, 3.2, 1.2]) >>> res = tlx.ops.zeros_like(x, dtype="int32") >>> print(res) >>> [0, 0, 0, 0]
stack¶
-
tensorlayerx.
stack
(values, axis=0)[source]¶ Stacks a list of rank-R tensors into one rank-(R+1) tensor.
- Parameters:
values (list) – A list of Tensor objects with the same shape and type.
axis (int) – An int. The axis to stack along. Defaults to the first dimension. Negative values wrap around, so the valid range is [-(R+1), R+1).
- Returns:
- Return type:
A stacked Tensor with the same type as values.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant([1,2,3]) >>> y = tlx.ops.constant([1,2,3]) >>> res = tlx.ops.stack([x, y]) >>> [[1, 2, 3], >>> [1, 2, 3]]
split¶
-
tensorlayerx.
split
(value, num_or_size_splits, axis=0)[source]¶ Splits a tensor into sub tensors.
- Parameters:
value (tensor) – The Tensor to split.
num_or_size_splits (int or list) – Either an integer indicating the number of splits along split_dim or a 1-D integer Tensor or Python list containing the sizes of each output tensor along split_dim.
axis (int) – The dimension along which to split. Must be in the range [-rank(value), rank(value)). Defaults to 0.
- Returns:
- Return type:
Tensor objects resulting from splitting value.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.ones([3, 9, 5]) >>> y1, y2, y3 = tlx.ops.split(x, 3, axis=1) >>> y1, y2, y3 = tlx.ops.split(x, [1,3,5], axis=1)
squeeze¶
-
tensorlayerx.
squeeze
(x, axis=None)[source]¶ Removes dimensions of size 1 from the shape of a tensor.
- Parameters:
x (tensor) – The input Tensor.
axis (int or list or tuple) – An integer or list/tuple of integers, indicating the dimensions to be squeezed. Default is None. The range of axis is [−ndim(x),ndim(x)). If axis is negative, axis=axis+ndim(x). If axis is None, all the dimensions of x of size 1 will be removed.
- Returns:
- Return type:
Squeezed Tensor with the same data type as input Tensor.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.ones(shape=[1,2,3]) >>> res = tlx.ops.squeeze(x, axis=0) >>> print(res.shape) >>> [2, 3]
expand_dims¶
-
tensorlayerx.
expand_dims
(input, axis)[source]¶ Inserts a dimension of 1 into a tensor’s shape.
- Parameters:
input (tensor) – A Tensor.
axis (int) – 0-D (scalar). Specifies the dimension index at which to expand the shape of input. Must be in the range [-rank(input) - 1, rank(input)].
- Returns:
- Return type:
A Tensor with the same data as input, but its shape has an additional dimension of size 1 added.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.ones([1,2,3]) >>> res = tlx.ops.expand_dims(x, axis=0) >>> print(res.shape) >>> [1, 1, 2, 3]
unsorted_segment_sum¶
-
tensorlayerx.
unsorted_segment_sum
(x, segment_ids, num_segments)[source]¶ Computes the sum along segments of a tensor.
- Parameters:
x (tensor) – A Tensor.
segment_ids (Tensor or list or tuple) – Must be one of the following types: int32, int64.
num_segments (int or tensor) – should equal the number of distinct segment IDs.
- Returns:
- Return type:
A Tensor. Has the same type as data.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant([1,2,3]) >>> res = tlx.ops.unsorted_segment_sum(x, (0, 0, 1), num_segments=2) >>> print(res) >>> [2, 3]
unsorted_segment_mean¶
-
tensorlayerx.
unsorted_segment_mean
(x, segment_ids, num_segments)[source]¶ Computes the mean along segments of a tensor.
- Parameters:
x (tensor) – A Tensor.
segment_ids (Tensor or list or tuple) – Must be one of the following types: int32, int64.
num_segments (int or tensor) – should equal the number of distinct segment IDs.
- Returns:
- Return type:
A Tensor. Has the same type as data.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant([1.,2.,3.]) >>> res = tlx.ops.unsorted_segment_mean(x, (0, 0, 1), num_segments=2) >>> print(res) >>> [1.5, 3]
unsorted_segment_min¶
-
tensorlayerx.
unsorted_segment_min
(x, segment_ids, num_segments)[source]¶ Computes the min along segments of a tensor.
- Parameters:
x (tensor) – A Tensor.
segment_ids (Tensor or list or tuple) – Must be one of the following types: int32, int64.
num_segments (int or tensor) – should equal the number of distinct segment IDs.
- Returns:
- Return type:
A Tensor. Has the same type as data.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant([1.,2.,3.]) >>> res = tlx.ops.unsorted_segment_min(x, (0, 0, 1), num_segments=2) >>> print(res) >>> [1, 3]
unsorted_segment_max¶
-
tensorlayerx.
unsorted_segment_max
(x, segment_ids, num_segments)[source]¶ Computes the max along segments of a tensor.
- Parameters:
x (tensor) – A Tensor.
segment_ids (Tensor or list or tuple) – Must be one of the following types: int32, int64.
num_segments (int or tensor) – should equal the number of distinct segment IDs.
- Returns:
- Return type:
A Tensor. Has the same type as data.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.constant([1.,2.,3.]) >>> res = tlx.ops.unsorted_segment_max(x, (0, 0, 1), num_segments=2) >>> print(res) >>> [2, 3]
set_seed¶
is_tensor¶
tensor_scatter_nd_update¶
-
tensorlayerx.
tensor_scatter_nd_update
(tensor, indices, updates)[source]¶ - Parameters:
tensor (Tensor) – tensor to update.
indices (list) – indices to update.
updates (Tensor) – value to apply at the indices
- Returns:
- Return type:
updated Tensor.
Examples
>>> import tensorlayerx as tlx >>> tensor = tlx.ops.ones(shape=(5,3)) >>> indices = [[0],[ 4],[ 2]] >>> updates = tlx.ops.convert_to_tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> new_tensor = tlx.ops.tensor_scatter_nd_update(tensor, indices, updates) >>> [[1. 2. 3.] >>> [1. 1. 1.] >>> [7. 8. 9.] >>> [1. 1. 1.] >>> [4. 5. 6.]]
scatter_update¶
-
tensorlayerx.
scatter_update
(tensor, indices, updates)[source]¶ Applies sparse updates to a variable
- Parameters:
tensor (Tensor) – A Tensor. The dim of tensor must be 1.
indices (Tensor) – Indices into the tensor.
updates (Tensor) – Updated values
- Returns:
- Return type:
Tensor after updated.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.ones((5,)) >>> indices = tlx.ops.convert_to_tensor([0, 4, 2]) >>> updates = tlx.ops.convert_to_tensor([1., 4., 7.]) >>> res = tlx.ops.scatter_update(x, indices, updates) >>> [1. 1. 7. 1. 4.]
diag¶
-
tensorlayerx.
diag
(input, diagonal=0)[source]¶ - Parameters:
input (Tensor) – the input tensor.
diagonal (int) – the diagonal to consider. Defualt is 0.
- Returns:
- Return type:
the output tensor.
Examples
>>> import tensorlayerx as tlx >>> tensor = tlx.ops.convert_to_tensor([[1,2,3],[4,5,6],[7,8,9]])) >>> new_tensor = tlx.ops.diag(tensor) >>> [1, 5, 9]
mask_select¶
-
tensorlayerx.
mask_select
(x, mask, axis=0)[source]¶ - Parameters:
x (Tensor) – N-D Tensor.
mask (Tensor) – N-D boolean Tensor or 1-D boolean Tensor
axis – the axis in tensor to mask from. By default, axis is 0.
- Returns:
- Return type:
the output tensor.
Examples
>>> import tensorlayerx as tlx >>> tensor = tlx.ops.convert_to_tensor([[1,2,3],[4,5,6],[7,8,9]])) >>> mask = tlx.ops.convert_to_tensor(np.array([True, False, True]), dtype=tlx.bool) >>> new_tensor = tlx.ops.mask_select(tensor, mask) >>> [[1, 2, 3],[7, 8, 9]]
eye¶
-
tensorlayerx.
eye
(n, m=None, dtype=None)[source]¶ - Parameters:
n (int) – the number of rows
m (int or None) – the number of columns with default being n
dtype (str or None) – the desired data type of returned tensor. Default: if None, use float32
- Returns:
- Return type:
A 2-D tensor with ones on the diagonal and zeros elsewhere
Examples
>>> import tensorlayerx as tlx >>> tlx.ops.eye(2) >>> [[1,0], >>> [0,1]]
einsum¶
-
tensorlayerx.
einsum
(equation, *operands)[source]¶ Sums the product of the elements of the input operands along dimensions specified using a notation based on the Einstein summation convention.
- Parameters:
equation (An attribute) – represent the operation you want to do. the value can contain only letters([a-z][A-Z]), commas(,), ellipsis(…), and arrow(->). the letters represent inputs’s tensor dimension, commas(,)represent separate tensors, ellipsis(…) indicates the tensor dimension that you do not care about, the left of the arrow(->) indicates the input tensors, and the right of it indicates the desired output dimension.
operands (list) – input tensor used for calculation. the data type of the tensor must be the same.
- Returns:
- Return type:
Tensor, the shape of it can be obtained from the equation, and the data type is the same as input tensors.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.nn.Input((5,)) >>> y = tlx.nn.Input((4,)) >>> out = tlx.ops.einsum('i,j->ij', x, y) >>> cal_enisum = tlx.ops.Einsum('i,j->ij') >>> out = cal_enisum(x, y)
set_device¶
get_device¶
to_device¶
-
tensorlayerx.
to_device
(tensor, device='GPU', id=0)[source]¶ Returns a copy of Tensor in specified device.
- Parameters:
tensor (Tensor) – A tensor.
device (str) – The specified device. Support ‘GPU’ and ‘CPU’. Default is ‘GPU’.
id (int) – The id of specified device. Default is 0.
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.ones((5,)) >>> x = tlx.ops.to_device(x, device="GPU", id=0)
roll¶
-
tensorlayerx.
roll
(input, shifts, dims=None)[source]¶ Roll the tensor input along the given dimension(s). Elements that are shifted beyond the last position are re-introduced at the first position. If dims is None, the tensor will be flattened before rolling and then restored to the original shape.
- Parameters:
input (tensor) – the input tensor.
shifts (int or tuple) – The number of places by which the elements of the tensor are shifted. If shifts is a tuple, dims must be a tuple of the same size, and each dimension will be rolled by the corresponding value
dims (int or tuple) – Axis along which to roll
Examples
>>> import tensorlayerx as tlx >>> x = tlx.ops.ones((5,6)) >>> x = tlx.ops.roll(x, shifts=2)
logsoftmax¶
-
tensorlayerx.
logsoftmax
(input, dim=None)[source]¶ Applies a softmax followed by a logarithm.
- Parameters:
input (Tensor) – the input tensor.
dim (int) – A dimension along which LogSoftmax will be computed.
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.random.random((3,4))) >>> x = tlx.ops.logsoftmax(x, dim=1)
topk¶
-
tensorlayerx.
topk
(input, k, dim=None, largest=True, sorted=True)[source]¶ Returns the k largest elements of the given input tensor along a given dimension.
A namedtuple of (values, indices) is returned, where the indices are the indices of the elements in the original input tensor.
- Parameters:
input (Tensor) – the input tensor.
k (int) – the k in “top-k”
dim (int) – the dimension to sort along. If dim is not given, the last dimension of the input is chosen.
largest (bool) – controls whether to return largest or smallest elements
sorted (bool) – controls whether to return the elements in sorted order
- Returns:
out – return the values and indices.
- Return type:
tuple
Examples
>>> import tensorlayerx as tlx >>> import numpy as np >>> x = tlx.ops.convert_to_tensor(np.random.random((3,4))) >>> x = tlx.ops.topk(x, 2)