oneflow.Tensor¶

OneFlow Tensor Class¶

class oneflow.Tensor
property T

Is this Tensor with its dimensions reversed.

If n is the number of dimensions in x, x.T is equivalent to x.permute(n-1, n-2, …, 0).

abs()
acos()
acosh()

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)

arccos()
arccosh()
arcsin()
arcsinh()
arctan()
arctanh()
argmax(dim=None, keepdim=None)
argmin(dim=None, keepdim=None)
argsort(dim=None, descending=None)

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()

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

Computes the gradient of current tensor w.r.t. graph leaves.

The graph is differentiated using the chain rule. If the tensor is non-scalar (i.e. its data has more than one element) and requires gradient, the function additionally requires specifying gradient. It should be a tensor of matching type and location, that contains the gradient of the differentiated function w.r.t. self.

This function accumulates gradients in the leaves - you might need to zero .grad attributes or set them to None before calling it. See Default gradient layouts for details on the memory layout of accumulated gradients.

Note

If you run any forward ops, create gradient, and/or call backward in a user-specified CUDA stream context, see Stream semantics of backward passes.

Note

When inputs are provided and a given input is not a leaf, the current implementation will call its grad_fn (though it is not strictly needed to get this gradients). It is an implementation detail on which the user should not rely. See https://github.com/pytorch/pytorch/pull/60521#issuecomment-867061780 for more details.

Parameters
• gradient (Tensor or None) – Gradient w.r.t. the tensor. If it is a tensor, it will be automatically converted to a Tensor that does not require grad unless create_graph is True. None values can be specified for scalar Tensors or ones that don’t require grad. If a None value would be acceptable then this argument is optional.

• retain_graph (bool, optional) – If False, the graph used to compute the grads will be freed. Note that in nearly all cases setting this option to True is not needed and often can be worked around in a much more efficient way. Defaults to the value of create_graph.

• create_graph (bool, optional) – If True, graph of the derivative will be constructed, allowing to compute higher order derivative products. Defaults to False.

bmm(other)
cast(dtype)
ceil()
chunk(chunks=None, dim=None)
clamp(min=None, max=None)

See oneflow.clamp().

clamp_(min=None, max=None)

Inplace version of oneflow.Tensor.clamp().

clip(min=None, max=None)

Alias for oneflow.Tensor.clamp().

clip_(min=None, max=None)

Alias for oneflow.Tensor.clamp_().

clone(self: oneflow._oneflow_internal.Tensor)oneflow._oneflow_internal.Tensor
copy_(other: Union[oneflow._oneflow_internal.Tensor, numpy.ndarray])

The interface is consistent with PyTorch.

Tensor.copy_(src, non_blocking=False) → Tensor

Copies the elements from src into self tensor and returns self.

The src tensor must be broadcastable with the self tensor. It may be of a different data type or reside on a different device.

Parameters
• src (Tensor) – the source tensor to copy from

• non_blocking (bool) – if True and this copy is between CPU and GPU, the copy may occur asynchronously with respect to the host. For other cases, this argument has no effect.

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)
diagonal(offset=0, dim1=0, dim2=1)
dim()

Tensor.dim() → int

Returns the number of dimensions of self tensor.

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()

Tensor.element_size() → int

Returns the size in bytes of an individual element.

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()
erfinv()
erfinv_()

Inplace version of oneflow.erfinv()

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()).

Parameters

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

expm1()
fill_(value)

Tensor.fill_(value) → Tensor

Fills self tensor with the specified value.

flatten(start_dim: int = 0, end_dim: int = - 1)
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()
floor_()

In-place version of oneflow.floor()

fmod(other)
gather(dim, index)Tensor
ge(other)

See oneflow.ge()

gelu()
get_device() -> Device ordinal (Integer)

For CUDA tensors, this function returns the device ordinal of the GPU on which the tensor resides. For CPU tensors, an error is thrown.

gt(other)

See oneflow.gt()

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
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)

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)
matmul(other)
max(dim, index)Tensor
mean(dim, index)Tensor
min(dim, index)Tensor
mish()
mul(other)
narrow(dimension, start, length)
property ndim
ndimension()

Tensor.dim() → int

Returns the number of dimensions of self tensor.

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()
nelement()

Tensor.nelement() → int

Alias for numel()

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, *, generator=None)Tensor

Fills self tensor with elements samples from the normal distribution parameterized by mean and std.

numel()
numpy()

Tensor.numpy() → numpy.ndarray

Returns self tensor as a NumPy ndarray. This tensor and the returned ndarray share the same underlying storage. Changes to

self tensor will be reflected in the ndarray and vice versa.

permute(*dims)
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])
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])
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)

The interface is consistent with PyTorch.

Returns the size of the self tensor. If dim is not specified, the returned value is a oneflow.Size, a subclass of tuple. If dim is specified, returns an int holding the size of that dimension.

Parameters

idx (int, optional) – The dimension for which to retrieve the size.

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=None, dim=0)
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)
swapaxes(dim0, dim1)
t()

Tensor.t() → Tensor

See oneflow.t()

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

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.int64)
>>> 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.int64)
>>> values.shape
oneflow.Size([2, 2])
>>> indices.shape
oneflow.Size([2, 2])
transpose(dim0, dim1)
tril(diagonal=0)
triu(diagonal=0)

See oneflow.triu()

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
unfold(dimension, size, step)

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

Returns a view of the original tensor which contains all slices of size size from self tensor in the dimension dimension.

Step between two slices is given by step.

If sizedim is the size of dimension dimension for self, the size of dimension dimension in the returned tensor will be (sizedim - size) / step + 1.

An additional dimension of size size is appended in the returned tensor.

Parameters
• dimension (int) – dimension in which unfolding happens

• size (int) – the size of each slice that is unfolded

• step (int) – the step between each slice

For example:

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

>>> x = flow.arange(1., 8)
>>> x
tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.])
>>> x.unfold(0, 2, 1)
tensor([[ 1.,  2.],
[ 2.,  3.],
[ 3.,  4.],
[ 4.,  5.],
[ 5.,  6.],
[ 6.,  7.]])
>>> x.unfold(0, 2, 2)
tensor([[ 1.,  2.],
[ 3.,  4.],
[ 5.,  6.]])
uniform_(a=0, b=1)

Tensor.uniform_(from=0, to=1) → Tensor

Fills self tensor with numbers sampled from the continuous uniform distribution:

$P(x)=1/(to-from)$
unsqueeze(dim)
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)
zeros_(self: oneflow._oneflow_internal.Tensor)None