API - Operations

Deep learning and Reinforcement learning library for Researchers and Engineers

get_tensor_shape(x)

Get the shape of tensor

zeros(shape[, dtype])

Creates a tensor with all elements set to zero.

zeros_like(x[, dtype])

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.

ones(shape[, dtype])

Creates a tensor with all elements set to ones.

ones_like(x[, dtype])

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.

constant(value[, dtype, shape])

Creates a constant tensor from a tensor-like object.

random_uniform(shape[, minval, maxval, …])

Outputs random values from a uniform distribution.

random_normal(shape[, mean, stddev, dtype, seed])

Outputs random values from a normal distribution.

truncated_normal(shape[, mean, stddev, …])

Outputs random values from a truncated normal distribution.

he_normal(shape[, dtype, seed])

He normal initializer.

xavier_normal(shape[, dtype, seed])

Xavier normal.

xavier_uniform(shape[, dtype, seed])

Xavier uniform.

Variable(initial_value, name[, trainable])

Creates a new variable with value initial_value.

abs(x)

Computes the absolute value of a tensor.

acos(x)

Computes acos of x element-wise.

acosh(x)

Computes inverse hyperbolic cosine of x element-wise.

add(value, bias)

Returns x + y element-wise.

angle(x)

Returns the element-wise argument of a complex (or real) tensor.

argmax(x[, axis, dtype])

Returns the index with the largest value across axes of a tensor.

argmin(x[, axis, dtype])

Returns the index with the smallest value across axes of a tensor.

asin(x)

Returns the index with the smallest value across axes of a tensor.

asinh(x)

Computes inverse hyperbolic sine of x element-wise.

atan(x)

Computes the trignometric inverse tangent of x element-wise.

atanh(x)

Computes inverse hyperbolic tangent of x element-wise.

ceil(x)

Return the ceiling of x as an Integral.

cos(x)

Computes cos of x element-wise.

cosh(x)

Computes hyperbolic cosine of x element-wise.

count_nonzero(x[, axis, keepdims, dtype])

Computes number of nonzero elements across dimensions of a tensor.

cumprod(x[, axis, exclusive, reverse])

Compute the cumulative product of the tensor x along axis.

cumsum(x[, axis, exclusive, reverse])

Compute the cumulative sum of the tensor x along axis.

divide(x, y)

Computes Python style division of x by y.

equal(x, y)

Returns the truth value of (x == y) element-wise.

exp(x)

Computes exponential of x element-wise.

floor(x)

Return the floor of x as an Integral.

floordiv(x, y)

Divides x / y elementwise, rounding toward the most negative integer.

floormod(x, y)

Returns element-wise remainder of division.

greater(x, y)

Returns the truth value of (x >= y) element-wise.

greater_equal(x, y)

Returns the truth value of (x >= y) element-wise.

is_inf(x)

Returns which elements of x are Inf.

is_nan(x)

Returns which elements of x are NaN.

l2_normalize(x[, axis, eps])

Normalizes along dimension axis using an L2 norm.

less(x, y)

Returns the truth value of (x < y) element-wise.

less_equal(x, y)

Returns the truth value of (x <= y) element-wise.

log(x)

Computes natural logarithm of x element-wise.

log_sigmoid(x)

Computes log sigmoid of x element-wise.

maximum(x, y)

Returns the max of x and y (i.e.

minimum(x, y)

Returns the min of x and y (i.e.

multiply(x, y)

Returns an element-wise x * y.

negative(x)

Computes numerical negative value element-wise.

not_equal(x, y)

Returns the truth value of (x != y) element-wise.

pow(x, y)

Computes the power of one value to another.

real(x)

Computes numerical negative value element-wise.

reciprocal(x)

Computes the reciprocal of x element-wise.

reshape(tensor, shape)

Reshapes a tensor.

concat(values, axis)

Concatenates tensors along one dimension.

convert_to_tensor(value[, dtype])

Converts the given value to a Tensor.

convert_to_numpy(value)

Converts the given Tensor to a numpy.

reduce_max(x[, axis, keepdims])

Computes the maximum of elements across dimensions of a tensor.

reduce_mean(input_tensor[, axis, keepdims])

Computes the mean of elements across dimensions of a tensor.

reduce_min(x[, axis, keepdims])

Computes the minimum of elements across dimensions of a tensor.

reduce_prod(x[, axis, keepdims])

Computes the multiply of elements across dimensions of a tensor.

reduce_std(x[, axis, keepdims])

Computes the standard deviation of elements across dimensions of a tensor.

reduce_sum(x[, axis, keepdims])

Computes the standard deviation of elements across dimensions of a tensor.

reduce_variance(x[, axis, keepdims])

Computes the variance of elements across dimensions of a tensor.

round(x)

Rounds the values of a tensor to the nearest integer, element-wise.

rsqrt(x)

Computes reciprocal of square root of x element-wise.

segment_max(x, segment_ids)

Computes the maximum along segments of a tensor.

segment_mean(x, segment_ids)

Computes the mean along segments of a tensor.

segment_min(x, segment_ids)

Computes the minimum along segments of a tensor.

segment_prod(x, segment_ids)

Computes the product along segments of a tensor.

segment_sum(x, segment_ids)

Computes the sum along segments of a tensor.

sigmoid(x)

Computes sigmoid of x element-wise.

sign(x)

Computes sign of a tensor element-wise.

sin(x)

Computes sine of a tensor element-wise.

sinh(x)

Computes hyperbolic sine of a tensor element-wise.

softplus(x)

Computes softplus of a tensor element-wise.

sqrt(x)

Computes square root of a tensor element-wise.

square(x)

Computes square of a tensor element-wise.

squared_difference(x, y)

Computes difference and square between tensor x and tensor y.

subtract(x, y)

Returns x - y element-wise.

tan(x)

Computes tan of a tensor element-wise.

tanh(x)

Computes hyperbolic tangent of a tensor element-wise.

any(x[, axis, keepdims])

Computes logical_or of a tensor element-wise.

all(x[, axis, keepdims])

Computes logical_and of a tensor element-wise.

logical_and(x, y)

Returns the truth value of x AND y element-wise.

logical_not(x)

Returns the truth value of NOT x element-wise.

logical_or(x, y)

Returns the truth value of x OR y element-wise.

logical_xor(x, y)

Returns the truth value of NOT x element-wise.

argsort(x[, axis, descending])

Returns the indices of a tensor that give its sorted order along an axis.

bmm(x, y)

Applies batched matrix multiplication to two tensors.

matmul(a, b[, transpose_a, transpose_b])

Multiplies matrix a by matrix b, producing a * b.

triu(x[, diagonal])

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.

tril(x[, diagonal])

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.

tile(input, multiples)

Constructs a tensor by tiling a given tensor.

where(condition, x, y)

Return a tensor of elements selected from either x or y, depending on condition.

stack(values[, axis])

Stacks a list of rank-R tensors into one rank-(R+1) tensor.

split(value, num_or_size_splits[, axis])

Splits a tensor into sub tensors.

squeeze(x[, axis])

Removes dimensions of size 1 from the shape of a tensor.

expand_dims(input, axis)

Inserts a dimension of 1 into a tensor’s shape.

gather(params, indices[, axis])

Gather slices from params axis axis according to indices.

unsorted_segment_sum(x, segment_ids, …)

Computes the sum along segments of a tensor.

unsorted_segment_mean(x, segment_ids, …)

Computes the mean along segments of a tensor.

unsorted_segment_min(x, segment_ids, …)

Computes the min along segments of a tensor.

unsorted_segment_max(x, segment_ids, …)

Computes the max along segments of a 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 tl
>>> x_in = tl.layers.Input((32, 3, 3, 32))
>>> x_shape = tl.ops.get_tensor_shape(x_in)

zeros

tensorlayerx.zeros(shape, dtype='float32')[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

Returns

Return type

A Tensor with all elements set to zero.

Examples

>>> import tensorlayerx as tl
>>> x = tl.ops.zeros((32, 3, 3, 32), dtype=tl.int32)
>>> y = tl.ops.zeros((10, 25, 25, 10), dtype='float32')

ones

tensorlayerx.ones(shape, dtype='float32')[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

Returns

Return type

A Tensor with all elements set to zero.

Examples

>>> import tensorlayerx as tl
>>> x = tl.ops.ones((32, 3, 3, 32), dtype=tl.int32)
>>> y = tl.ops.ones((10, 25, 25, 10), dtype='float32')

constant

tensorlayerx.constant(value, dtype='float32', shape=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.

Returns

Return type

A Constant Tensor.

Examples

>>> import tensorlayerx as tl
>>> x = tl.ops.constant(0.5, (32, 3, 3, 32), dtype=tl.int32)
>>> y = tl.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 tl
>>> x = tl.ops.random_uniform((32, 3, 3, 32), maxval=1.0, dtype=tl.int32)
>>> y = tl.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 tl
>>> x = tl.ops.random_normal((32, 3, 3, 32), dtype=tl.int32)
>>> y = tl.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 tl
>>> x = tl.ops.truncated_normal((32, 3, 3, 32), dtype=tl.int32)
>>> y = tl.ops.truncated_normal((10, 25, 25, 10), dtype='float32')

he_normal

tensorlayerx.he_normal(shape, 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 tl
>>> x = tl.ops.he_normal((32, 3, 3, 32), dtype=tl.int32)
>>> y = tl.ops.he_normal((10, 25, 25, 10), dtype='float32')

xavier_normal

tensorlayerx.xavier_normal(shape, 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 tl
>>> x = tl.ops.xavier_normal((32, 3, 3, 32), dtype=tl.int32)
>>> y = tl.ops.xavier_normal((10, 25, 25, 10), dtype='float32')

xavier_uniform

tensorlayerx.xavier_uniform(shape, 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 tl
>>> x = tl.ops.xavier_uniform((32, 3, 3, 32), dtype=tl.int32)
>>> y = tl.ops.xavier_uniform((10, 25, 25, 10), dtype='float32')

Variable

tensorlayerx.Variable(initial_value, name, trainable=True)[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.

Returns

Return type

Variable

Examples

>>> import tensorlayerx as tl
>>> x = tl.Variable(tl.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 tl
>>> x = tl.layers.Input((32, 3, 3, 32))
>>> y = tl.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 tl
>>> x = tl.layers.Input((32, 3, 3, 32))
>>> y = tl.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 tl
>>> x = tl.layers.Input((32, 3, 3, 32))
>>> y = tl.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 tl
>>> value = tl.ones(shape=(10, 20))
>>> bias = tl.ones(shape=(20))
>>> x = tl.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 tl
>>> x = tl.ops.constant(value=[2.15 + 3.57j, 3.89 + 6.54j])
>>> y = tl.ops.angle(x)

argmax

tensorlayerx.argmax(x, axis=None, 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 tl
>>> x = tl.ops.constant(value=[10, 20, 5, 6, 15])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[10, 20, 5, 6, 15])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[10, 20, 5, 6, 15])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[0.9142202  0.72091234])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[0.9142202  0.72091234])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[0.9142202  0.72091234])
>>> y = tl.ops.atanh(x)

ceil

tensorlayerx.ceil(x)

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 tl
>>> x = tl.ops.constant(value=[0.9142202  0.72091234])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[0.9142202  0.72091234])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=["", "a", "c", "b", " "])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[3, 2, 1])
>>> y = tl.ops.cumprod(x)
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.ops.cumsum(x)
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[0.9142202  0.72091234])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.ops.exp(x)

floor

tensorlayerx.floor(x)

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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.constant(value=[1, 2, 3, np.inf])
>>> y = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.constant(value=[1, 2, 3, np.nan])
>>> y = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.constant(value=[1, 2, 3, np.nan])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.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 tl
>>> x = tl.ops.constant([0., 0., 0., 0.])
>>> y = tl.ops.constant([-5., -2., 0., 3.])
>>> z = tl.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 tl
>>> x = tl.ops.constant(value=[0.9142202  0.72091234])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.ops.constant(value=[1, 3, 5])
>>> x = tl.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 tl
>>> x = tl.ops.constant(value=[1, 2, 3])
>>> y = tl.ops.constant(value=[1, 3, 5])
>>> x = tl.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 tl
>>> x = tl.ops.constant(value=[-2.25 + 4.75j, 3.25 + 5.75j])
>>> y = tl.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 tl
>>> x = tl.ops.constant(value=[-2.25, 3.25])
>>> y = tl.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 tl
>>> x = tl.ops.constant([0., 1., 2., 3.])
>>> z = tl.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 tl
>>> x = tl.ops.constant([0., 0., 0., 0.])
>>> y = tl.ops.constant([-5., -2., 0., 3.])
>>> z = tl.ops.concat([x, y], 0)

convert_to_tensor

tensorlayerx.convert_to_tensor(value, dtype=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.

Returns

Return type

A Tensor based on value.

Examples

>>> import tensorlayerx as tl
>>> import numpy as np
>>> x = np.ones(shape=(10, 10))
>>> y = tl.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 tl
>>> x = tl.ops.ones(shape=(10, 10))
>>> y = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.random.randn(3, 4))
>>> x1 = tl.ops.reduce_max(x, axis=1, keepdims=False)
>>> x2 = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.random.randn(3, 4))
>>> x1 = tl.ops.reduce_mean(x, axis=1, keepdims=False)
>>> x2 = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.random.randn(3, 4))
>>> x1 = tl.ops.reduce_min(x, axis=1, keepdims=False)
>>> x2 = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.random.randn(3, 4))
>>> x1 = tl.ops.reduce_prod(x, axis=1, keepdims=False)
>>> x2 = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.random.randn(3, 4))
>>> x1 = tl.ops.reduce_std(x, axis=1, keepdims=False)
>>> x2 = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.random.randn(3, 4))
>>> x1 = tl.ops.reduce_sum(x, axis=1, keepdims=False)
>>> x2 = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.random.randn(3, 4))
>>> x1 = tl.ops.reduce_variance(x, axis=1, keepdims=False)
>>> x2 = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([0.9, 2.5, 2.3, 1.5, -4.5]))
>>> x = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([0.9, 2.5, 2.3, 1.5]))
>>> x = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([[1,2,3,4], [4, 3, 2, 1], [5,6,7,8]]))
>>> id = tl.ops.convert_to_tensor([0, 0, 1])
>>> x = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([[1.0 , 2, 3, 4], [4, 3, 2, 1], [5, 6, 7, 8]]))
>>> id = tl.ops.convert_to_tensor([0, 0, 1])
>>> x = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([[1, 2, 3, 4], [4, 3, 2, 1], [5, 6, 7, 8]]))
>>> id = tl.ops.convert_to_tensor([0, 0, 1])
>>> x = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([[1, 2, 3, 4], [4, 3, 2, 1], [5, 6, 7, 8]]))
>>> id = tl.ops.convert_to_tensor([0, 0, 1])
>>> x = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([[1, 2, 3, 4], [4, 3, 2, 1], [5, 6, 7, 8]]))
>>> id = tl.ops.convert_to_tensor([0, 0, 1])
>>> x = tl.ops.segment_sum(x, id)
>>> print(x)
>>> [[5, 5, 5, 5],
>>> [5, 6, 7, 8]]

sigmoid

tensorlayerx.sigmoid(x)[source]

Computes sigmoid of x element-wise.

Parameters

x (tensor) – A Tensor with type float16, float32, float64, complex64, or complex128.

Returns

Return type

A Tensor with the same type as x.

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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([-128.0, 0.0, 128.0]), dtype='float32')
>>> x = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([-1.0, 0.0, 1.0]), dtype='float32')
>>> x = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([-1.0, 0.0, 1.0]), dtype='float32')
>>> x = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([-1.0, 0.0, 1.0]), dtype='float32')
>>> x = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([0.0, 1.0, 4.0]), dtype=tl.float32)
>>> x = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([-1.0, 0.0, 1.0]), dtype='float32')
>>> x = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([[1,0,1], [2,3,4]]), dtype='float32')
>>> y = tl.ops.convert_to_tensor(np.array([[-1,0,1], [2,3,4]]), dtype='float32')
>>> res = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([[1,0,1], [2,3,4]]), dtype='float32')
>>> y = tl.ops.convert_to_tensor(np.array([[-1,0,1], [2,3,4]]), dtype='float32')
>>> res = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([1,0,1]), dtype='float32')
>>> res = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([1,0,1]), dtype="float32")
>>> res = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([1,0,1]), dtype='bool')
>>> res = tl.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 tl
>>> import numpy as np
>>> x = tl.ops.convert_to_tensor(np.array([1,0,1]), dtype='bool')
>>> res = tl.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 tl
>>> x = tl.constant([False, False, True, True])
>>> y = tl.constant([False, True, False, True])
>>> res = tl.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 tl
>>> x = tl.constant([False, False, True, True])
>>> res = tl.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 tl
>>> x = tl.constant([False, False, True, True])
>>> y = tl.constant([False, True, False, True])
>>> res = tl.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 tl
>>> x = tl.constant([False, False, True, True])
>>> y = tl.constant([False, True, False, True])
>>> res = tl.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 tl
>>> x = [1, 10, 26.9, 2.8, 166.32, 62.3]
>>> y = tl.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 tl
>>> x = tl.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 = tl.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 = tl.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 tl
>>> import numpy as np
>>> x = tl.convert_to_tensor(np.random.random([2,3,2]), dtype="float32")
>>> y = tl.convert_to_tensor(np.random.random([2,2,3]), dtype="float32")
>>> z = tl.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 tl
>>> x = tl.convert_to_tensor(np.arange(1, 10, dtype="int32").reshape(3,-1))
>>> y = tl.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 tl
>>> x = tl.convert_to_tensor(np.arange(1, 10, dtype="int32").reshape(3,-1))
>>> y = tl.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 tl
>>> x = tl.ops.constant([[1,2,3],[1,2,3]])
>>> y = tl.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 tl
>>> x = tl.convert_to_tensor([0.9, 0.1, 3.2, 1.2])
>>> y = tl.convert_to_tensor([1.0, 1.0, 1.0, 1.0])
>>> res = tl.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 tl
>>> x = tl.convert_to_tensor([0.9, 0.1, 3.2, 1.2])
>>> res = tl.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 tl
>>> x = tl.convert_to_tensor([0.9, 0.1, 3.2, 1.2])
>>> res = tl.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 tl
>>> x = tl.ops.constant([1,2,3])
>>> y = tl.ops.constant([1,2,3])
>>> res = tl.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 tl
>>> x = tl.ops.ones([3, 9, 5])
>>> y1, y2, y3 = tl.ops.split(x, 3, axis=1)
>>> y1, y2, y3 = tl.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 tl
>>> x = tl.ops.ones(shape=[1,2,3])
>>> res = tl.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 tl
>>> x = tl.ops.ones([1,2,3])
>>> res = tl.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 tl
>>> x = tl.ops.constant([1,2,3])
>>> res = tl.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 tl
>>> x = tl.ops.constant([1.,2.,3.])
>>> res = tl.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 tl
>>> x = tl.ops.constant([1.,2.,3.])
>>> res = tl.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 tl
>>> x = tl.ops.constant([1.,2.,3.])
>>> res = tl.ops.unsorted_segment_max(x, (0, 0, 1), num_segments=2)
>>> print(res)
>>> [2, 3]