# oneflow.Tensor¶

## OneFlow Tensor Class¶

class oneflow.Tensor
abs()
acos()
acosh()
add(other)

Computes the addition of input by other for each element, scalar and broadcast promotation are supported. The formula is:

$out = input + other$

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> x = flow.Tensor(np.random.randn(2,3))
>>> y = flow.Tensor(np.random.randn(2,3))
>>> out.shape
(2, 3)

>>> x = 5
>>> y = flow.Tensor(np.random.randn(2,3))
>>> out.shape
(2, 3)

>>> x = flow.Tensor(np.random.randn(1,1))
>>> y = flow.Tensor(np.random.randn(2,3))
>>> out.shape
(2, 3)

add_(y)

In-place version of oneflow.Tensor.add().

addmm(mat1, mat2, alpha=1, beta=1)
arccosh()
arcsin()
arcsinh()
arctan()
arctanh()
argmax(dim=None, keepdim=None)
argmin(dim=None, keepdim=None)
argsort(dim: int = - 1, descending: bool = False)

This operator sorts the input Tensor at specified dim and return the indices of the sorted Tensor.

Parameters
• input (oneflow.Tensor) – The input Tensor.

• dim (int, optional) – dimension to be sorted. Defaults to the last dim (-1).

• descending (bool, optional) – controls the sorting order (ascending or descending).

Returns

The indices of the sorted Tensor.

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow as flow
>>> x = np.array([[10, 2, 9, 3, 7],
...               [1, 9, 4, 3, 2]]).astype("float32")
>>> input = flow.Tensor(x)
>>> output = flow.argsort(input)
>>> output
tensor([[1, 3, 4, 2, 0],
[0, 4, 3, 2, 1]], dtype=oneflow.int32)
>>> output = flow.argsort(input, descending=True)
>>> output
tensor([[0, 2, 4, 3, 1],
[1, 2, 3, 4, 0]], dtype=oneflow.int32)
>>> output = flow.argsort(input, dim=0)
>>> output
tensor([[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0]], dtype=oneflow.int32)

argwhere()Tensor
asin()
asinh()
atan()
atan2(other)
atanh()
backward(gradient=None, retain_graph=False, create_graph=False)
bmm()Tensor

See oneflow.bmm()

cast(dtype)
ceil()
chunk(chunks, dim)

Splits a tensor into a specific number of chunks. Each chunk is a view of the input tensor. Last chunk will be smaller if the tensor size along the given dimension dim is not divisible by chunks.

Parameters
• input (oneflow.Tensor) – The tensor to split.

• chunks (int) – Number of chunks to return.

• dim (int) – Dimension along which to split the tensor.

Returns

List of Tensors.

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> np_arr = np.random.randn(5, 3, 6, 9).astype(np.float32)
>>> input = flow.Tensor(np_arr)
>>> of_out = []
>>> of_out = flow.chunk(input, chunks=3, dim=2)
>>> chunks = 3
>>> of_out_shape = []
>>> for i in range(0, chunks):
...     of_out_shape.append(of_out[i].numpy().shape)
>>> of_out_shape
[(5, 3, 2, 9), (5, 3, 2, 9), (5, 3, 2, 9)]

>>> np_arr = np.random.randn(5, 3, 6, 9).astype(np.float32)
>>> input = flow.Tensor(np_arr)
>>> of_out = []
>>> of_out = flow.chunk(input, chunks=4, dim=3)
>>> chunks = 4
>>> of_out_shape = []
>>> for i in range(0, chunks):
...     of_out_shape.append(of_out[i].numpy().shape)
>>> of_out_shape
[(5, 3, 6, 2), (5, 3, 6, 2), (5, 3, 6, 2), (5, 3, 6, 3)]

clamp(min=None, max=None)
clip(min=None, max=None)
clone(self: oneflow._oneflow_internal.Tensor)oneflow._oneflow_internal.Tensor
copy_(other: Union[oneflow._oneflow_internal.Tensor, numpy.ndarray])
cos()

Returns a new tensor with the cosine of the elements of input.

$\text{out}_{i} = \cos(\text{input}_{i})$
Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> import numpy as np
>>> arr = np.array([1.4309,  1.2706, -0.8562,  0.9796])
>>> input = flow.tensor(arr, dtype=flow.float32)
>>> output = flow.cos(input).numpy()

cosh()

Returns a new tensor with the hyperbolic cosine of the elements of input.

$\text{out}_{i} = \cosh(\text{input}_{i})$
Parameters

input (Tensor) – the input tensor.

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> arr = np.array([ 0.1632,  1.1835, -0.6979, -0.7325])
>>> input = flow.tensor(arr, dtype=flow.float32)
>>> output = flow.cosh(input).numpy()
>>> output
array([1.0133467, 1.7859949, 1.2535787, 1.2804903], dtype=float32)

cpu()

Returns a copy of this object in CPU memory. If this object is already in CPU memory and on the correct device, then no copy is performed and the original object is returned.

For example:

>>> import oneflow as flow

>>> input = flow.tensor([1, 2, 3, 4, 5], device=flow.device("cuda"))
>>> output = input.cpu()
>>> output.device
device(type='cpu', index=0)

cuda(device: Optional[Union[oneflow.nn.modules.tensor_ops.int, str, oneflow._oneflow_internal.device]] = None)

Returns a copy of this object in CUDA memory. If this object is already in CUDA memory and on the correct device, then no copy is performed and the original object is returned.

Parameters

device (flow.device) – The destination GPU device. Defaults to the current CUDA device.

For example:

>>> import oneflow as flow

>>> input = flow.Tensor([1, 2, 3, 4, 5])
>>> output = input.cuda()
>>> output.device
device(type='cuda', index=0)

property data
detach(self: oneflow._oneflow_internal.Tensor)oneflow._oneflow_internal.Tensor
property device
diag(diagonal=0)
dim()
div(other)

Computes the division of input by other for each element, scalar and broadcast promotation are supported. The formula is:

$out = \frac{input}{other}$
Parameters
• input (Union[int, float, flow.Tensor]) – input.

• other (Union[int, float, flow.Tensor]) – other.

For example:

>>> import numpy as np
>>> import oneflow as flow

# element-wise divide
>>> input = flow.Tensor(np.random.randn(2,3))
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.div(input,other).numpy()
>>> out.shape
(2, 3)

# scalar divide
>>> input = 5
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.div(input,other).numpy()
>>> out.shape
(2, 3)

>>> input = flow.Tensor(np.random.randn(1,1))
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.div(input,other).numpy()
>>> out.shape
(2, 3)

double()

Tensor.double() is equivalent to Tensor.to(flow.float64). See to().

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> input = flow.tensor(np.random.randn(1, 2, 3), dtype=flow.int)
>>> input = input.double()
>>> input.dtype
oneflow.float64

property dtype
element_size()
eq(other)

Computes element-wise equality. The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters
Returns

• A boolean tensor that is True where input is equal to other and False elsewhere

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> input = flow.tensor(np.array([2, 3, 4, 5]), dtype=flow.float32)
>>> other = flow.tensor(np.array([2, 3, 4, 1]), dtype=flow.float32)

>>> y = flow.eq(input, other)
>>> y
tensor([1, 1, 1, 0], dtype=oneflow.int8)

erf()
erfc()
exp()
expand(*sizes)

This operator expand the input tensor to a larger size.

Passing -1 as the size for a dimension means not changing the size of that dimension.

Tensor can be also expanded to a larger number of dimensions and the new ones will be appended at the front.

For the new dimensions, the size cannot be set to -1.

Parameters
• input (oneflow.Tensor) – The input Tensor.

• *sizes (oneflow.Size or int) – The desired expanded size.

Returns

The result Tensor.

Return type

oneflow.Tensor

For example:

>>> import oneflow as flow
>>> import numpy as np
>>> x = np.array([[[[0, 1]],
...               [[2, 3]],
...               [[4, 5]]]]).astype(np.int32)
>>> input = flow.Tensor(x)
>>> input.shape
oneflow.Size([1, 3, 1, 2])
>>> out = input.expand(1, 3, 2, 2)
>>> out.shape
oneflow.Size([1, 3, 2, 2])

expand_as(other)Tensor

Expand this tensor to the same size as other. self.expand_as(other) is equivalent to self.expand(other.size()).

Please see expand() for more information about expand.

Parameters

other (oneflow.Tensor) – The result tensor has the same size as other.

expm1()
fill_(value)
flatten(start_dim: int = 0, end_dim: int = - 1)

Flattens a contiguous range of dims into a tensor.

Parameters
• start_dim – first dim to flatten (default = 0).

• end_dim – last dim to flatten (default = -1).

For example:

>>> import numpy as np
>>> import oneflow as flow
>>> input = flow.Tensor(32, 1, 5, 5)
>>> output = input.flatten(start_dim=1)
>>> output.shape
oneflow.Size([32, 25])

flip(dims)
float()

Tensor.float() is equivalent to Tensor.to(flow.float32). See to().

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> input = flow.tensor(np.random.randn(1, 2, 3), dtype=flow.int)
>>> input = input.float()
>>> input.dtype
oneflow.float32

floor()
fmod(other)
gather(dim, index, sparse_grad=False)

Gathers values along an axis specified by dim.

For a 3-D tensor the output is specified by:

out[i][j][k] = input[index[i][j][k]][j][k]  # if dim == 0
out[i][j][k] = input[i][index[i][j][k]][k]  # if dim == 1
out[i][j][k] = input[i][j][index[i][j][k]]  # if dim == 2


input and index must have the same number of dimensions. It is also required that index.size(d) <= input.size(d) for all dimensions d != dim. out will have the same shape as index. Note that input and index do not broadcast against each other.

Parameters
• input (Tensor) – the source tensor

• dim (int) – the axis along which to index

• index (LongTensor) – the indices of elements to gather

For example:

>>> import oneflow as flow
>>> import numpy as np
>>> input = np.random.randn(3, 4, 3, 5)
>>> index = np.random.choice(np.arange(3), size=180, replace=True).reshape((3, 4, 3, 5))
>>> output = flow.gather(flow.Tensor(input), 1, flow.tensor(index, dtype=flow.int))
>>> output.shape
oneflow.Size([3, 4, 3, 5])

ge(other)
gelu()
get_device()
property grad
property grad_fn
gt(other)
in_top_k()Tensor
index_select(dim, index)Tensor
int()

Tensor.int() is equivalent to Tensor.to(flow.int32). See to().

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> input = flow.tensor(np.random.randn(1, 2, 3), dtype=flow.float32)
>>> input = input.int()
>>> input.dtype
oneflow.int32

property is_consistent
is_contiguous(self: oneflow._oneflow_internal.Tensor)bool
property is_cuda
is_floating_point()

Returns True if the data type of input is a floating point data type i.e., one of flow.float64, flow.float32, flow.float16.

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow

>>> input = flow.tensor([1, 2, 3, 4, 5], dtype=flow.int)
>>> output = flow.is_floating_point(input)
>>> output
False

property is_lazy
property is_leaf
item()

Returns the value of this tensor as a standard Python number. This only works for tensors with one element. For other cases, see tolist().

This operation is not differentiable.

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> x = flow.tensor([1.0])
>>> x.item()
1.0

kaiming_normal_(a=0, mode='fan_in', nonlinearity='leaky_relu', *, data_format='NCHW')
kaiming_uniform_(a=0, mode='fan_in', nonlinearity='leaky_relu', *, data_format='NCHW')
le(other)

Returns the truth value of $$input <= other$$ element-wise.

Parameters
Returns

A Tensor with int8 type.

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> input1 = flow.tensor(np.array([1, 2, 3]).astype(np.float32), dtype=flow.float32)
>>> input2 = flow.tensor(np.array([1, 1, 4]).astype(np.float32), dtype=flow.float32)

>>> out = flow.le(input1, input2)
>>> out
tensor([1, 0, 1], dtype=oneflow.int8)

log()

Returns a new tensor with the natural logarithm of the elements of input.

$y_{i} = \log_{e} (x_{i})$
Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> import numpy as np
>>> arr = np.random.randn(2, 3, 4, 5)
>>> input = flow.tensor(arr, dtype=flow.float32)
>>> output = flow.log(input)

log1p()
logical_and()Tensor
logical_not()Tensor
logical_or()Tensor
logical_xor()Tensor
long()

Tensor.long() is equivalent to Tensor.to(flow.int64). See to().

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> input = flow.tensor(np.random.randn(1, 2, 3), dtype=flow.float32)
>>> input = input.long()
>>> input.dtype
oneflow.int64

lt(other)

Returns the truth value of $$input < other$$ element-wise.

Parameters
Returns

A Tensor with int8 type.

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> input1 = flow.tensor(np.array([1, 2, 3]).astype(np.float32), dtype=flow.float32)
>>> input2 = flow.tensor(np.array([1, 2, 4]).astype(np.float32), dtype=flow.float32)

>>> out = flow.lt(input1, input2)
>>> out
tensor([0, 0, 1], dtype=oneflow.int8)

masked_fill(mask, value)

Fills elements of self tensor with value where mask is True. The shape of mask must be broadcastable with the shape of the underlying tensor.

Parameters

• value (float) – the value to fill in with

For example:

>>> import oneflow as flow
>>> import numpy as np
>>> in_arr = np.array(
...     [[[-0.13169311,  0.97277078,  1.23305363,  1.56752789],
...     [-1.51954275,  1.87629473, -0.53301206,  0.53006478],
...     [-1.38244183, -2.63448052,  1.30845795, -0.67144869]],
...     [[ 0.41502161,  0.14452418,  0.38968   , -1.76905653],
...     [ 0.34675095, -0.7050969 , -0.7647731 , -0.73233418],
...     [-1.90089858,  0.01262963,  0.74693893,  0.57132389]]]
... )
>>> fill_value = 8.7654321 # random value e.g. -1e9 3.1415
>>> input = flow.tensor(in_arr, dtype=flow.float32)
>>> mask = flow.tensor((in_arr > 0).astype(np.int8), dtype=flow.int)

# tensor([[[-0.1317,  8.7654,  8.7654,  8.7654],
#  [-1.5195,  8.7654, -0.533 ,  8.7654],
#  [-1.3824, -2.6345,  8.7654, -0.6714]],

# [[ 8.7654,  8.7654,  8.7654, -1.7691],
#  [ 8.7654, -0.7051, -0.7648, -0.7323],
#  [-1.9009,  8.7654,  8.7654,  8.7654]]], dtype=oneflow.float32)

masked_select(mask)
matmul(other)
max(dim, index)Tensor
mean(dim, index)Tensor
min(dim, index)Tensor
mish()
mul(other)
narrow(dim: int, start: int, length: int)

Returns a new tensor that is a narrowed version of x tensor. The dimension dim is input from start to start + length.

Parameters
• x – the tensor to narrow.

• dim – the dimension along which to narrow.

• start – the starting dimension.

• length – the distance to the ending dimension.

For example:

>>> import oneflow as flow
>>> x = flow.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> flow.narrow(x, 0, 0, 2)
tensor([[1, 2, 3],
[4, 5, 6]], dtype=oneflow.int64)
>>> flow.narrow(x, 1, 1, 2)
tensor([[2, 3],
[5, 6],
[8, 9]], dtype=oneflow.int64)

property ndim
ndimension()
ne(other)

Computes element-wise not equality. The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters
Returns

• A boolean tensor that is True where input is not equal to other and False elsewhere

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> input = flow.tensor(np.array([2, 3, 4, 5]), dtype=flow.float32)
>>> other = flow.tensor(np.array([2, 3, 4, 1]), dtype=flow.float32)

>>> y = flow.ne(input, other)
>>> y
tensor([0, 0, 0, 1], dtype=oneflow.int8)

negative()

This operator computes the negative value of Tensor.

Parameters

input (oneflow.Tensor) – A Tensor

Returns

The result Tensor

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> input = flow.tensor(
...    np.array([1.0, -1.0, 2.3]).astype(np.float32), dtype=flow.float32
... )
>>> out = flow.negative(input)
>>> out
tensor([-1.0000,  1.0000, -2.3000], dtype=oneflow.float32)

nelement()
new_ones(size=None, dtype=None, device=None, placement=None, sbp=None, requires_grad=False)

Returns a Tensor of size size filled with 1. By default, the returned Tensor has the same torch.dtype and torch.device as this tensor.

Parameters
• size (int...) – a list, tuple, or flow.Size of integers defining the shape of the output tensor.

• dtype (flow.dtype, optional) – the desired type of returned tensor. Default: if None, same flow.dtype as this tensor.

• device (flow.device, optional) – the desired device of returned tensor. Default: if None, same flow.device as this tensor.

• placement (flow.placement, optional) – the desired placement of returned consistent tensor. Default: if None, the returned tensor is local one using the argument device.

• sbp (flow.sbp.sbp or tuple of flow.sbp.sbp, optional) – the desired sbp descriptor of returned consistent tensor. Default: if None, the returned tensor is local one using the argument device.

• requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> x = flow.Tensor(np.ones((1, 2, 3)))
>>> y = x.new_ones((2, 2))
>>> y
tensor([[1., 1.],
[1., 1.]], dtype=oneflow.float32)

nonzero(input, as_tuple=False)Tensor
norm(input, ord=None, dim=None, keepdim=False, *, dtype=None, out=None)Tensor

Returns the matrix norm or vector norm of a given tensor.

This function can calculate one of eight different types of matrix norms, or one of an infinite number of vector norms, depending on both the number of reduction dimensions and the value of the ord parameter.

Parameters
• input (Tensor) – The input tensor. If dim is None, input must be 1-D or 2-D, unless ord is None. If both dim and ord are None, the 2-norm of the input flattened to 1-D will be returned. Its data type must be either a floating point or complex type. For complex inputs, the norm is calculated on of the absolute values of each element. If the input is complex and neither dtype nor out is specified, the result’s data type will be the corresponding floating point type (e.g. float if input is complexfloat).

• ord (int, inf, -inf, 'fro', 'nuc', optional) –

order of norm. Default: ‘None’ The following norms can be calculated:

ord

norm for matrices

norm for vectors

None

Frobenius norm

2-norm

’fro’

Frobenius norm

– not supported –

‘nuc’

– not supported yet –

– not supported –

inf

max(sum(abs(x), dim=1))

max(abs(x))

-inf

min(sum(abs(x), dim=1))

min(abs(x))

0

– not supported –

sum(x != 0)

1

max(sum(abs(x), dim=0))

as below

-1

min(sum(abs(x), dim=0))

as below

2

– not supported yet –

as below

-2

– not supported yet –

as below

other

– not supported –

sum(abs(x)^{ord})^{(1 / ord)}

where inf refers to float(‘inf’), NumPy’s inf object, or any equivalent object.

• dim (int, 2-tuple of ints, 2-list of ints, optional) – If dim is an int, vector norm will be calculated over the specified dimension. If dim is a 2-tuple of ints, matrix norm will be calculated over the specified dimensions. If dim is None, matrix norm will be calculated when the input tensor has two dimensions, and vector norm will be calculated when the input tensor has one dimension. Default: None

• keepdim (bool, optional) – If set to True, the reduced dimensions are retained in the result as dimensions with size one. Default: False

• out (Tensor, optional) – The output tensor.

For example:

>>> import oneflow as flow
>>> from oneflow import linalg as LA
>>> import numpy as np
>>> a = flow.tensor(np.arange(9, dtype=np.float32) - 4)
>>> a
tensor([-4., -3., -2., -1.,  0.,  1.,  2.,  3.,  4.], dtype=oneflow.float32)
>>> b = a.reshape(3, 3)
>>> b
tensor([[-4., -3., -2.],
[-1.,  0.,  1.],
[ 2.,  3.,  4.]], dtype=oneflow.float32)
>>> LA.norm(a)
tensor(7.7460, dtype=oneflow.float32)
>>> LA.norm(b)
tensor(7.7460, dtype=oneflow.float32)
>>> LA.norm(b, 'fro')
tensor(7.7460, dtype=oneflow.float32)
>>> LA.norm(a, float('inf'))
tensor(4., dtype=oneflow.float32)
>>> LA.norm(b, float('inf'))
tensor(9., dtype=oneflow.float32)
>>> LA.norm(a, -float('inf'))
tensor(0., dtype=oneflow.float32)
>>> LA.norm(b, -float('inf'))
tensor(2., dtype=oneflow.float32)
>>> LA.norm(a, 1)
tensor(20., dtype=oneflow.float32)
>>> LA.norm(b, 1)
tensor(7., dtype=oneflow.float32)
>>> LA.norm(a, -1)
tensor(0., dtype=oneflow.float32)
>>> LA.norm(b, -1)
tensor(6., dtype=oneflow.float32)
>>> LA.norm(a, 2)
tensor(7.7460, dtype=oneflow.float32)
>>> LA.norm(a, -2)
tensor(0., dtype=oneflow.float32)
>>> LA.norm(a, 3)
tensor(5.8480, dtype=oneflow.float32)
>>> LA.norm(a, -3)
tensor(0., dtype=oneflow.float32)
>>> c = flow.tensor([[1., 2., 3.],
...                   [-1, 1, 4]])
>>> LA.norm(c, dim=0)
tensor([1.4142, 2.2361, 5.0000], dtype=oneflow.float32)
>>> LA.norm(c, dim=1, keepdim = True)
tensor([[3.7417],
[4.2426]], dtype=oneflow.float32)
>>> LA.norm(c, ord=1, dim=1)
tensor([6., 6.], dtype=oneflow.float32)

normal_(mean=0, std=1)
numel()
numpy()
permute(*dims)

Returns a view of the original tensor with its dimensions permuted.

Parameters

*dims (int...) – The desired ordering of dimensions

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> input = flow.tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32)
>>> out = input.permute(1, 0, 2, 3).shape
>>> out
oneflow.Size([6, 2, 5, 3])

property placement
pow(b)
prod(dim, index)Tensor
reciprocal()

Computes the safe reciprocal of x. If x is zero, the reciprocal will be also set to zero.

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> x = flow.Tensor(np.array([[1, 2, 3], [4, 5, 6]]))
>>> out = flow.reciprocal(x)
>>> out.numpy()
array([[1.        , 0.5       , 0.33333334],
[0.25      , 0.2       , 0.16666667]], dtype=float32)

register_hook(self: oneflow._oneflow_internal.Tensor, arg0: Callable[[oneflow._oneflow_internal.Tensor], oneflow._oneflow_internal.Tensor])None
relu(inplace=False)
repeat(*sizes)

This operator repeat the input tensor to a larger size along the specified dimensions.

Parameters
• x (oneflow.Tensor) – The input Tensor.

• *size (flow.Size or int) – The number of times to repeat this tensor along each dimension

Returns

The result Tensor.

Return type

oneflow.Tensor

For example:

>>> import oneflow as flow
>>> import numpy as np
>>> x = np.array([[[[0, 1]],
...               [[2, 3]],
...               [[4, 5]]]]).astype(np.int32)

>>> input = flow.Tensor(x)
>>> out = input.repeat(1, 1, 2, 2)
>>> out.shape
oneflow.Size([1, 3, 2, 4])

property requires_grad
requires_grad_(self: oneflow._oneflow_internal.Tensor, requires_grad: bool = True)oneflow._oneflow_internal.Tensor
reshape(*shape)

This operator reshapes a Tensor.

We can set one dimension in shape as -1, the operator will infer the complete shape.

Parameters
• x – A Tensor.

• *shape – tuple of python::ints or int…

Returns

A Tensor has the same type as x.

For example:

>>> import numpy as np
>>> import oneflow as flow
>>> x = np.array(
...    [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
... ).astype(np.float32)
>>> input = flow.Tensor(x)

>>> y = input.reshape(2, 2, 2, -1).shape
>>> y
oneflow.Size([2, 2, 2, 2])

retain_grad(self: oneflow._oneflow_internal.Tensor)None
roll(shifts, dims=None)
round()
rsqrt()

Returns a new tensor with the reciprocal of the square-root of each of the elements of input.

$\text{out}_{i} = \frac{1}{\sqrt{\text{input}_{i}}}$
Parameters

input – the input tensor.

>>> import oneflow as flow
>>> import numpy as np

>>> a = flow.Tensor(np.array([1.0, 2.0, 3.0]))
>>> out = flow.rsqrt(a).numpy()
>>> out
array([1.        , 0.70710677, 0.57735026], dtype=float32)

selu()
property shape
sigmoid()
sign()
silu()
sin()Tensor
sin_()

In-place version of oneflow.sin()

sinh()
size(idx=None)
softmax(dim=None)
softplus()
softsign()
sort(dim: int = - 1, descending: bool = False)

Sorts the elements of the input tensor along a given dimension in ascending order by value.

Parameters
• input (oneflow.Tensor) – The input Tensor.

• dim (int, optional) – dimension to be sorted. Defaults to the last dim (-1).

• descending (bool, optional) – controls the sorting order (ascending or descending).

Returns

A tuple of (values, indices), where where the values are the sorted values and the indices are the indices of the elements in the original input tensor.

Return type

Tuple(oneflow.Tensor, oneflow.Tensor(dtype=int32))

For example:

>>> import oneflow as flow
>>> import numpy as np
>>> x = np.array([[1, 3, 8, 7, 2], [1, 9, 4, 3, 2]], dtype=np.float32)
>>> input = flow.Tensor(x)
>>> (values, indices) = flow.sort(input)
>>> values
tensor([[1., 2., 3., 7., 8.],
[1., 2., 3., 4., 9.]], dtype=oneflow.float32)
>>> indices
tensor([[0, 4, 1, 3, 2],
[0, 4, 3, 2, 1]], dtype=oneflow.int32)
>>> (values, indices) = flow.sort(input, descending=True)
>>> values
tensor([[8., 7., 3., 2., 1.],
[9., 4., 3., 2., 1.]], dtype=oneflow.float32)
>>> indices
tensor([[2, 3, 1, 4, 0],
[1, 2, 3, 4, 0]], dtype=oneflow.int32)
>>> (values, indices) = flow.sort(input, dim=0)
>>> values
tensor([[1., 3., 4., 3., 2.],
[1., 9., 8., 7., 2.]], dtype=oneflow.float32)
>>> indices
tensor([[0, 0, 1, 1, 0],
[1, 1, 0, 0, 1]], dtype=oneflow.int32)

split(split_size_or_sections: Union[int, List[int]], dim: int = 0)

Splits the tensor into chunks.

If split_size_or_sections is an integer type, then x will be split into equally sized chunks (if possible). Last chunk will be smaller if the tensor size along the given dimension dim is not divisible by split_size.

If split_size_or_sections is a list, then x will be split into len(split_size_or_sections) chunks with sizes in dim according to split_size_or_sections.

Parameters
• x – tensor to split.

• split_size_or_sections – size of a single chunk or list of sizes for each chunk.

• dim – dimension along which to split the tensor.

For example:

>>> import oneflow as flow
>>> a = flow.arange(10).view(5, 2)
>>> flow.split(a, 2)
(tensor([[0, 1],
[2, 3]], dtype=oneflow.int64), tensor([[4, 5],
[6, 7]], dtype=oneflow.int64), tensor([[8, 9]], dtype=oneflow.int64))
>>> flow.split(a, [1, 4])
(tensor([[0, 1]], dtype=oneflow.int64), tensor([[2, 3],
[4, 5],
[6, 7],
[8, 9]], dtype=oneflow.int64))

sqrt()

Returns a new tensor with the square-root of the elements of input.

$\text{out}_{i} = \sqrt{\text{input}_{i}}$
Parameters

input – the input tensor.

>>> import oneflow as flow
>>> import numpy as np

>>> arr = np.array([1.0, 2.0, 3.0])
>>> input = flow.Tensor(arr)
>>> output = flow.sqrt(input).numpy()
>>> output
array([1.       , 1.4142135, 1.7320508], dtype=float32)

square()

Returns a new tensor with the square of the elements of input.

$\text{out}_{i} = \sqrt{\text{input}_{i}}$
Parameters

input – the input tensor.

>>> import oneflow as flow
>>> import numpy as np

>>> arr = np.array([1.0, 2.0, 3.0])
>>> input = flow.Tensor(arr)
>>> output = flow.square(input).numpy()
>>> output
array([1., 4., 9.], dtype=float32)

squeeze(dim=None)
std(dim=None, unbiased=True, keepdim=False)
storage_offset(self: oneflow._oneflow_internal.Tensor)int
stride(self: oneflow._oneflow_internal.Tensor)tuple
sub(other)

Computes the subtraction of input by other for each element, scalar and broadcast promotation are supported. The formula is:

$out = input - other$

For example:

>>> import numpy as np
>>> import oneflow as flow

# element-wise subtract
>>> input = flow.Tensor(np.random.randn(2,3))
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.sub(input,other).numpy()
>>> out.shape
(2, 3)

# scalar subtract
>>> input = 5
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.sub(input,other).numpy()
>>> out.shape
(2, 3)

>>> input = flow.Tensor(np.random.randn(1,1))
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.sub(input,other).numpy()
>>> out.shape
(2, 3)

tan()
tanh()
tile(reps)

The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.tile.html

Constructs a tensor by repeating the elements of input. The reps argument specifies the number of repetitions in each dimension.

If reps specifies fewer dimensions than input has, then ones are prepended to reps until all dimensions are specified. For example, if input has shape (8, 6, 4, 2) and reps is (2, 2), then reps is treated as (1, 1, 2, 2).

Analogously, if input has fewer dimensions than reps specifies, then input is treated as if it were unsqueezed at dimension zero until it has as many dimensions as reps specifies. For example, if input has shape (4, 2) and reps is (3, 3, 2, 2), then input is treated as if it had the shape (1, 1, 4, 2).

Note

This function is similar to NumPy’s tile function.

Parameters
• input (oneflow.Tensor) – the tensor whose elements to repeat.

• reps (tuple) – the number of repetitions per dimension.

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> x = np.array([1, 2]).astype(np.int32)
>>> input = flow.tensor(x, dtype=flow.int32)
>>> out = input.tile(reps=(2,))
>>> out
tensor([1, 2, 1, 2], dtype=oneflow.int32)

>>> x = np.random.randn(5, 2, 1)
>>> input = flow.Tensor(x)
>>> out = input.tile(reps=(3, 4))
>>> out.size()
oneflow.Size([5, 6, 4])

to(*args, **kwargs)
Performs Tensor dtype and/or device conversion.

A flow.dtype and flow.device are inferred from the arguments of input.to(*args, **kwargs).

Note

If the input Tensor already has the correct flow.dtype and flow.device, then input is returned. Otherwise, the returned tensor is a copy of input with the desired.

Parameters
• input (oneflow.Tensor) – An input tensor.

• *args (oneflow.Tensor or oneflow.device or oneflow.dtype) – Positional arguments

• **kwargs (oneflow.device or oneflow.dtype) – Key-value arguments

Returns

A Tensor.

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> arr = np.random.randint(1, 9, size=(1, 2, 3, 4))
>>> input = flow.Tensor(arr)
>>> output = input.to(dtype=flow.float32)
>>> np.array_equal(arr.astype(np.float32), output.numpy())
True

to_consistent(placement=None, sbp=None, grad_sbp=None)

Cast a local tensor to consistent tensor or cast a consistent tensor to another consistent tensor with different sbp or placement

Parameters
• input (Tensor) – the input tensor.

• placement (flow.placement, optional) – the desired placement of returned consistent tensor. Default: if None, the input tensor must be consistent one and use its own placement.

• sbp (flow.sbp.sbp or tuple of flow.sbp.sbp, optional) – the desired sbp descriptor of returned consistent tensor. Default: if None, the input tensor must be consistent one and use its own sbp.

For example:

>>> import oneflow as flow
>>> import numpy as np
>>> np_arr = np.array([0.5, 0.6, 0.7]).astype(np.float32)
>>> input = flow.Tensor(np_arr)
>>> placement = flow.placement("cpu", {0:range(1)})
>>> output_tensor = input.to_consistent(placement, [flow.sbp.split(0)])
>>> output_tensor.is_consistent
True

to_local()

Returns the local tensor of a consistent tensor.

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> import numpy as np
>>> np_arr = np.array([0.5, 0.6, 0.7]).astype(np.float32)
>>> input = flow.tensor(np_arr, dtype=flow.float32)
>>> placement = flow.placement("cpu", {0:range(1)})
>>> consistent_tensor = input.to_consistent(placement, [flow.sbp.split(0)])
>>> consistent_tensor.to_local()
tensor([0.5000, 0.6000, 0.7000], dtype=oneflow.float32)

tolist()

Returns the tensor as a (nested) list. For scalars, a standard Python number is returned, just like with item(). Tensors are automatically moved to the CPU first if necessary.

This operation is not differentiable.

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow as flow
>>> input = flow.tensor([[1,2,3], [4,5,6]])
>>> input.tolist()
[[1, 2, 3], [4, 5, 6]]

topk(k, dim: Optional[int] = None, largest: bool = True, sorted: bool = True)

Finds the values and indices of the k largest entries at specified axis.

Parameters
• input (oneflow.Tensor) – Input Tensor

• k (int) – the k in “top-k”

• dim (int, optional) – the dimension to sort along. Defaults to the last dim (-1)

• largest (bool, optional) – controls whether to return largest or smallest elements

• sorted (bool, optional) – controls whether to return the elements in sorted order (Only Support True Now!)

Returns

A tuple of (values, indices), where the indices are the indices of the elements in the original input tensor.

Return type

Tuple(oneflow.Tensor, oneflow.Tensor(dtype=int32))

For example:

>>> import oneflow as flow
>>> import numpy as np
>>> x = np.array([[1, 3, 8, 7, 2], [1, 9, 4, 3, 2]], dtype=np.float32)
>>> (values, indices) = flow.topk(flow.Tensor(x), k=3, dim=1)
>>> values
tensor([[8., 7., 3.],
[9., 4., 3.]], dtype=oneflow.float32)
>>> indices
tensor([[2, 3, 1],
[1, 2, 3]], dtype=oneflow.int32)
>>> values.shape
oneflow.Size([2, 3])
>>> indices.shape
oneflow.Size([2, 3])
>>> (values, indices) = flow.topk(flow.Tensor(x), k=2, dim=1, largest=False)
>>> values
tensor([[1., 2.],
[1., 2.]], dtype=oneflow.float32)
>>> indices
tensor([[0, 4],
[0, 4]], dtype=oneflow.int32)
>>> values.shape
oneflow.Size([2, 2])
>>> indices.shape
oneflow.Size([2, 2])

transpose(dim0, dim1)
tril(diagonal=0)
triu(diagonal=0)
type_as(target)
Returns this tensor cast to the type of the given tensor.

This is a no-op if the tensor is already of the correct type.

Parameters
• input (Tensor) – the input tensor.

• target (Tensor) – the tensor which has the desired type.

For example:

>>> import oneflow as flow
>>> import numpy as np

>>> input = flow.tensor(np.random.randn(1, 2, 3), dtype=flow.float32)
>>> target = flow.tensor(np.random.randn(4, 5, 6), dtype = flow.int32)
>>> input = input.type_as(target)
>>> input.dtype
oneflow.int32

uniform_(a=0, b=1)
unsqueeze(dim)

Returns a new tensor with a dimension of size one inserted at the specified position.

The returned tensor shares the same underlying data with this tensor.

A dim value within the range [-input.ndimension() - 1, input.ndimension() + 1) can be used. Negative dim will correspond to unsqueeze() applied at dim = dim + input.ndimension() + 1.

Parameters
• input (Tensor) – the input tensor.

• dim (int) – the index at which to insert the singleton dimension

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> x = flow.randn(2, 3, 4)
>>> y = x.unsqueeze(2)
>>> y.shape
oneflow.Size([2, 3, 1, 4])

var(dim=None, unbiased=True, keepdim=False)
view(*shape)

The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.Tensor.view.html

Returns a new tensor with the same data as the self tensor but of a different shape.

The returned tensor shares the same data and must have the same number of elements, but may have a different size. For a tensor to be viewed, the new view size must be compatible with its original size and stride, i.e., each new view dimension must either be a subspace of an original dimension, or only span across original dimensions $$d, d+1, \dots, d+k$$ that satisfy the following contiguity-like condition that $$\forall i = d, \dots, d+k-1$$,

$\text{stride}[i] = \text{stride}[i+1] \times \text{size}[i+1]$

Otherwise, it will not be possible to view self tensor as shape without copying it (e.g., via contiguous()). When it is unclear whether a view() can be performed, it is advisable to use reshape(), which returns a view if the shapes are compatible, and copies (equivalent to calling contiguous()) otherwise.

Parameters
• input – A Tensor.

• *shape – flow.Size or int…

Returns

A Tensor has the same type as input.

For example:

>>> import numpy as np
>>> import oneflow as flow

>>> x = np.array(
...    [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
... ).astype(np.float32)
>>> input = flow.Tensor(x)

>>> y = input.view(2, 2, 2, -1).numpy().shape
>>> y
(2, 2, 2, 2)

where(x=None, y=None)

Return a tensor of elements selected from either x or y, depending on condition. If the element in condition is larger than 0,

it will take the x element, else it will take the y element

Note

If x is None and y is None, flow.where(condition) is identical to flow.nonzero(condition, as_tuple=True).

The tensors condition, x, y must be broadcastable.

Parameters
• condition (IntTensor) – When 1 (nonzero), yield x, otherwise yield y

• x (Tensor or Scalar) – value (if :attr:x is a scalar) or values selected at indices where condition is True

• y (Tensor or Scalar) – value (if :attr:x is a scalar) or values selected at indices where condition is False

Returns

A tensor of shape equal to the broadcasted shape of condition, x, y

Return type

Tensor

For example:

>>> import numpy as np
>>> import oneflow as flow
>>> x = flow.tensor(
...    np.array([[-0.4620, 0.3139], [0.3898, -0.7197], [0.0478, -0.1657]]),
...    dtype=flow.float32,
... )
>>> y = flow.tensor(np.ones(shape=(3, 2)), dtype=flow.float32)
>>> condition = flow.tensor(np.array([[0, 1], [1, 0], [1, 0]]), dtype=flow.int32)
>>> out = condition.where(x, y)
>>> out
tensor([[1.0000, 0.3139],
...
[0.0478, 1.0000]], dtype=oneflow.float32)

xavier_normal_(gain=1.0, *, data_format='NCHW')
xavier_uniform_(gain=1.0, *, data_format='NCHW')
zeros_(self: oneflow._oneflow_internal.Tensor)None