paddle.Tensor

A Tensor is a generalization of vectors and matrices and is easily understood as a multidimensional array. For more information, you can see Introduction to Tensor.

Data types

PaddlePaddle defines the following Tensor types:

Data type

dtype

32-bit floating point

paddle.float32

64-bit floating point

paddle.float64

16-bit floating point

paddle.float16

16-bit floating point

paddle.bfloat16

64-bit complex

paddle.complex64

128-bit complex

paddle.complex128

8-bit integer (unsigned)

paddle.uint8

8-bit integer (signed)

paddle.int8

16-bit integer (signed)

paddle.int16

32-bit integer (signed)

paddle.int32

64-bit integer (signed)

paddle.int64

Boolean

paddle.bool

Tensor class reference

Properties

T

The transpose of Tensor. See paddle.transpose .

block

Tensor’s block.

dtype

Tensor’s data type.

grad

The value of Tensor’s grad.

inplace_version

The inplace version of current Tensor.

is_leaf

Whether Tensor is leaf Tensor.

name

The name of Tensor.

ndim

The dimensions of Tensor.

persistable

The value of Tensor’s persistable.

place

The place of Tensor.

shape

The shape of Tensor. See paddle.shape .

size

The size of Tensor. See paddle.numel .

stop_gradient

The value of Tensor’s stop_gradient.

type

Tensor’s type.

Methods

abs

Perform elementwise abs for input x.

acos

Acos Activation Operator.

add

Elementwise Add Operator.

add_

Inplace version of add API, the output Tensor will be inplaced with input x.

add_n

Sum one or more Tensor of the input.

addmm

addmm

all

Computes the logical and of tensor elements over the given dimension.

allclose

Check if all \(x\) and \(y\) satisfy the condition:

angle

Element-wise angle of complex numbers.

any

Computes the logical or of tensor elements over the given dimension, and return the result.

argmax

Computes the indices of the max elements of the input tensor’s element along the provided axis.

argmin

Computes the indices of the min elements of the input tensor’s element along the provided axis.

argsort

Sorts the input along the given axis, and returns the corresponding index tensor for the sorted output values.

as_complex

Transform a real tensor to a complex tensor.

as_real

Transform a complex tensor to a real tensor.

asin

Arcsine Operator.

astype

Cast a Tensor to a specified data type.

atan

Arctangent Operator.

backward

Run backward of current Graph which starts from current Tensor.

bernoulli_

This is the inplace version of api bernoulli, which returns a Tensor filled with random values sampled from a bernoulli distribution.

bitwise_and

Apply bitwise_and on Tensor X and Y .

bitwise_not

Apply bitwise_not on Tensor X.

bitwise_or

Apply bitwise_or on Tensor X and Y .

bitwise_xor

Apply bitwise_xor on Tensor X and Y .

bmm

Applies batched matrix multiplication to two tensors.

broadcast_shape

The function returns the shape of doing operation with broadcasting on tensors of x_shape and y_shape.

broadcast_tensors

Broadcast a list of tensors following broadcast semantics

broadcast_to

Broadcast the input tensor to a given shape.

bucketize

This API is used to find the index of the corresponding 1D tensor sorted_sequence in the innermost dimension based on the given x.

cast

Take in the Tensor x with x.dtype and cast it to the output with dtype.

ceil

Ceil Operator.

ceil_

Inplace version of ceil API, the output Tensor will be inplaced with input x.

cholesky

Computes the Cholesky decomposition of one symmetric positive-definite matrix or batches of symmetric positive-definite matrices.

chunk

Split the input tensor into multiple sub-Tensors.

clear_grad

The alias of clear_gradient().

clear_gradient

Only for Tensor that has gradient, normally we use this for Parameters since other temporary Tensor doesen’t has gradient.

clip

This operator clip all elements in input into the range [ min, max ] and return a resulting tensor as the following equation:

clip_

Inplace version of clip API, the output Tensor will be inplaced with input x.

clone

Returns a new Tensor, which is clone of origin Tensor, and it remains in the current graph.

concat

Concatenates the input along the axis.

cond

Computes the condition number of a matrix or batches of matrices with respect to a matrix norm p.

conj

This function computes the conjugate of the Tensor elementwisely.

copy_

cos

Cosine Operator.

cosh

Cosh Activation Operator.

count_nonzero

Counts the number of non-zero values in the tensor x along the specified axis.

cpu

cross

Computes the cross product between two tensors along an axis.

cuda

cumprod

Compute the cumulative product of the input tensor x along a given dimension dim.

cumsum

The cumulative sum of the elements along a given axis.

detach

Returns a new Tensor, detached from the current graph.

diagonal

Computes the diagonals of the input tensor x.

digamma

Calculates the digamma of the given input tensor, element-wise.

dim

dist

Returns the p-norm of (x - y).

divide

Divide two tensors element-wise.

dot

This operator calculates inner product for vectors.

diff

Computes the n-th forward difference along the given axis.

eigvals

Compute the eigenvalues of one or more general matrices.

equal

This layer returns the truth value of \(x == y\) elementwise.

equal_all

Returns the truth value of \(x == y\).

erf

The error function.

exp

Computes exp of x element-wise with a natural number e as the base.

exp_

Inplace version of exp API, the output Tensor will be inplaced with input x.

expand

Expand the input tensor to a given shape.

expand_as

Expand the input tensor x to the same shape as the input tensor y.

fill_

Notes:

fill_diagonal_

Note

This API is ONLY available in Dygraph mode.

fill_diagonal_tensor

This function fill the source Tensor y into the x Tensor’s diagonal.

fill_diagonal_tensor_

Note

This API is ONLY available in Dygraph mode.

flatten

Flattens a contiguous range of axes in a tensor according to start_axis and stop_axis.

flatten_

Inplace version of flatten API, the output Tensor will be inplaced with input x.

flip

Reverse the order of a n-D tensor along given axis in axis.

floor

Floor Activation Operator.

floor_

Inplace version of floor API, the output Tensor will be inplaced with input x.

floor_divide

Floor divide two tensors element-wise and rounds the quotinents to the nearest integer toward zero.

floor_mod

Mod two tensors element-wise.

gather

Output is obtained by gathering entries of axis of x indexed by index and concatenate them together.

gather_nd

This function is actually a high-dimensional extension of gather and supports for simultaneous indexing by multiple axes.

gradient

greater_equal

Returns the truth value of \(x >= y\) elementwise, which is equivalent function to the overloaded operator >=.

greater_than

Returns the truth value of \(x > y\) elementwise, which is equivalent function to the overloaded operator >.

histogram

Computes the histogram of a tensor.

imag

Returns a new tensor containing imaginary values of input tensor.

increment

The API is usually used for control flow to increment the data of x by an amount value.

index_sample

IndexSample Layer

index_select

Returns a new tensor which indexes the input tensor along dimension axis using the entries in index which is a Tensor.

inverse

Takes the inverse of the square matrix.

isclose

Check if all \(x\) and \(y\) satisfy the condition:

is_empty

Test whether a Tensor is empty.

is_tensor

Tests whether input object is a paddle.Tensor.

isfinite

Return whether every element of input tensor is finite number or not.

isin

Tests if each element of x is in test_x.

isinf

Return whether every element of input tensor is +/-INF or not.

isnan

Return whether every element of input tensor is NaN or not.

isneginf

Tests if each element of input is negative infinity or not.

isposinf

Tests if each element of input is positive infinity or not.

isreal

Tests if each element of input is a real number or not.

item

Convert element at specific position in Tensor into Python scalars.

kron

Compute the Kronecker product of two tensors, a composite tensor made of blocks of the second tensor scaled by the first.

less_equal

Returns the truth value of \(x <= y\) elementwise, which is equivalent function to the overloaded operator <=.

less_than

Returns the truth value of \(x < y\) elementwise, which is equivalent function to the overloaded operator <.

lgamma

Calculates the lgamma of the given input tensor, element-wise.

log

Calculates the natural log of the given input Tensor, element-wise.

log10

Calculates the log to the base 10 of the given input tensor, element-wise.

log1p

Calculates the natural log of the given input tensor, element-wise.

log2

Calculates the log to the base 2 of the given input tensor, element-wise.

logcumsumexp

The logarithm of the cumulative summation of the exponentiation of the elements along a given axis.

logical_and

Compute element-wise logical AND on x and y, and return out.

logical_not

logical_not operator computes element-wise logical NOT on x, and returns out.

logical_or

logical_or operator computes element-wise logical OR on x and y, and returns out.

logical_xor

logical_xor operator computes element-wise logical XOR on x and y, and returns out.

logsumexp

Calculates the log of the sum of exponentials of x along axis .

masked_select

Returns a new 1-D tensor which indexes the input tensor according to the mask which is a tensor with data type of bool.

matmul

Applies matrix multiplication to two tensors.

matrix_power

Computes the n-th power of a square matrix or a batch of square matrices.

max

Computes the maximum of tensor elements over the given axis.

maximum

Compare two tensors and returns a new tensor containing the element-wise maxima.

fmax

Compares the elements at the corresponding positions of the two tensors and returns a new tensor containing the maximum value of the element.

mean

Computes the mean of the input tensor’s elements along axis.

median

Compute the median along the specified axis.

nanmedian

Compute the median along the specified axis, while ignoring NaNs.

min

Computes the minimum of tensor elements over the given axis

minimum

Compare two tensors and return a new tensor containing the element-wise minima.

fmin

Compares the elements at the corresponding positions of the two tensors and returns a new tensor containing the minimum value of the element.

mm

Applies matrix multiplication to two tensors.

inner

Inner product of two input Tensor.

outer

Outer product of two Tensors.

cov

Estimate the covariance matrix of the input variables, given data and weights.

lu

Computes the LU factorization of an N-D(N>=2) matrix x.

lu_unpack

Unpack L U and P to single matrix tensor .

cholesky_solve

Solves a linear system of equations A @ X = B, given A’s Cholesky factor matrix u and matrix B.

mod

Mod two tensors element-wise.

multiplex

Based on the given index parameter, the OP selects a specific row from each input Tensor to construct the output Tensor.

multiply

multiply two tensors element-wise.

mv

Performs a matrix-vector product of the matrix x and the vector vec.

nan_to_num

Replaces NaN, positive infinity, and negative infinity values in input tensor.

ndimension

neg

This function computes the negative of the Tensor elementwisely.

nonzero

Return a tensor containing the indices of all non-zero elements of the input tensor.

norm

Returns the matrix norm (the Frobenius norm, the nuclear norm and p-norm) or vector norm (the 1-norm, the Euclidean or 2-norm, and in general the p-norm) of a given tensor.

not_equal

Returns the truth value of \(x != y\) elementwise, which is equivalent function to the overloaded operator !=.

numel

Returns the number of elements for a tensor, which is a 0-D int64 Tensor with shape [].

numpy

Returns a numpy array shows the value of current Tensor.

pin_memory

pow

Compute the power of Tensor elements.

prod

Compute the product of tensor elements over the given axis.

rank

Returns the number of dimensions for a tensor, which is a 0-D int32 Tensor.

real

Returns a new Tensor containing real values of the input Tensor.

reciprocal

Reciprocal Activation Operator.

reciprocal_

Inplace version of reciprocal API, the output Tensor will be inplaced with input x.

register_hook

Registers a backward hook for current Tensor.

remainder

Mod two tensors element-wise.

repeat_interleave

Returns a new tensor which repeats the x tensor along dimension axis using the entries in repeats which is a int or a Tensor.

reshape

Changes the shape of x without changing its data.

reshape_

Inplace version of reshape API, the output Tensor will be inplaced with input x.

reverse

Reverse the order of a n-D tensor along given axis in axis.

roll

Roll the x tensor along the given axis(axes).

round

Round the values in the input to the nearest integer value.

round_

Inplace version of round API, the output Tensor will be inplaced with input x.

rsqrt

Rsqrt Activation Operator.

rsqrt_

Inplace version of rsqrt API, the output Tensor will be inplaced with input x.

scale

Scale operator.

scale_

Inplace version of scale API, the output Tensor will be inplaced with input x.

scatter

Scatter Layer Output is obtained by updating the input on selected indices based on updates.

scatter_

Inplace version of scatter API, the output Tensor will be inplaced with input x.

scatter_nd

Scatter_nd Layer

scatter_nd_add

Output is obtained by applying sparse addition to a single value or slice in a Tensor.

set_value

Notes:

shard_index

Reset the values of input according to the shard it belongs to. Every value in input must be a non-negative integer, and the parameter index_num represents the integer above the maximum value of input. Thus, all values in input must be in the range [0, index_num) and each value can be regarded as the offset to the beginning of the range. The range is further split into multiple shards. Specifically, we first compute the shard_size according to the following formula, which represents the number of integers each shard can hold. So for the i’th shard, it can hold values in the range [i*shard_size, (i+1)*shard_size). ::.

sign

Returns sign of every element in x: 1 for positive, -1 for negative and 0 for zero.

sgn

For complex tensor, this API returns a new tensor whose elements have the same angles as the corresponding elements of input and absolute values of one.

sin

Sine Activation Operator.

sinc

Calculate the normalized sinc of x elementwise.

sinc_

Inplace version of sinc API, the output Tensor will be inplaced with input x.

sinh

Sinh Activation Operator.

slice

This operator produces a slice of input along multiple axes.

solve

Computes the solution of a square system of linear equations with a unique solution for input ‘X’ and ‘Y’.

sort

Sorts the input along the given axis, and returns the sorted output tensor.

split

Split the input tensor into multiple sub-Tensors.

vsplit

Split the input tensor into multiple sub-Tensors along the vertical axis, which is equivalent to paddle.tensor_split with axis=0.

sqrt

Sqrt Activation Operator.

sqrt_

Inplace version of sqrt API, the output Tensor will be inplaced with input x.

square

Square each elements of the inputs.

squeeze

Squeeze the dimension(s) of size 1 of input tensor x’s shape.

squeeze_

Inplace version of squeeze API, the output Tensor will be inplaced with input x.

stack

Stacks all the input tensors x along axis dimension.

stanh

stanh activation.

std

Computes the standard-deviation of x along axis .

strided_slice

This operator produces a slice of x along multiple axes.

subtract

Subtract two tensors element-wise.

subtract_

Inplace version of subtract API, the output Tensor will be inplaced with input x.

sum

Computes the sum of tensor elements over the given dimension.

t

Transpose <=2-D tensor.

take

Returns a new tensor with the elements of input tensor x at the given index.

tanh

Tanh Activation Operator.

tanh_

Inplace version of tanh API, the output Tensor will be inplaced with input x.

tile

Construct a new Tensor by repeating x the number of times given by repeat_times.

to

Performs Tensor dtype and/or device conversion.

tolist

Note

This API is ONLY available in Dygraph mode.

topk

Return values and indices of the k largest or smallest at the optional axis.

trace

Computes the sum along diagonals of the input tensor x.

transpose

Permute the data dimensions of input according to perm.

trunc

This API is used to returns a new tensor with the truncated integer values of input.

frac

This API is used to return the fractional portion of each element in input.

unbind

Removes a tensor dimension, then split the input tensor into multiple sub-Tensors.

uniform_

This is the inplace version of OP uniform, which returns a Tensor filled with random values sampled from a uniform distribution.

unique

Returns the unique elements of x in ascending order.

unique_consecutive

Eliminates all but the first element from every consecutive group of equivalent elements.

unsqueeze

Insert single-dimensional entries to the shape of input Tensor x.

unsqueeze_

Inplace version of unsqueeze API, the output Tensor will be inplaced with input x.

unstack

This layer unstacks input Tensor x into several Tensors along axis.

value

var

Computes the variance of x along axis .

where

Return a Tensor of elements selected from either x or y according to corresponding elements of condition.

zero_

Notes:

is_complex

Return whether x is a tensor of complex data type(complex64 or complex128).

is_integer

Return whether x is a tensor of integral data type.

frexp

The function used to decompose a floating point number into mantissa and exponent.

trapezoid

Integrate along the given axis using the composite trapezoidal rule.

cumulative_trapezoid

Integrate along the given axis using the composite trapezoidal rule.

vander

Generate a Vandermonde matrix.

hypot

Calculate the length of the hypotenuse of a right-angle triangle.

hypot_

Inplace version of hypot API, the output Tensor will be inplaced with input x.

diagonal_scatter

Embed the values of Tensor y into Tensor x along the diagonal elements of Tensor x, with respect to axis1 and axis2.

combinations

Compute combinations of length r of the given tensor.