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 # element-wise add >>> x = flow.Tensor(np.random.randn(2,3)) >>> y = flow.Tensor(np.random.randn(2,3)) >>> out = flow.add(x, y).numpy() >>> out.shape (2, 3) # scalar add >>> x = 5 >>> y = flow.Tensor(np.random.randn(2,3)) >>> out = flow.add(x, y).numpy() >>> out.shape (2, 3) # broadcast add >>> x = flow.Tensor(np.random.randn(1,1)) >>> y = flow.Tensor(np.random.randn(2,3)) >>> out = flow.add(x, y).numpy() >>> out.shape (2, 3)
-
add_
(y)¶ In-place version of
oneflow.Tensor.add()
.
-
addmm
(mat1, mat2, alpha=1, beta=1)¶ See
oneflow.addmm()
-
arccosh
()¶
-
arcsin
()¶ See
oneflow.asin()
-
arcsinh
()¶ See
oneflow.asinh()
-
arctan
()¶ See
oneflow.arctan()
-
arctanh
()¶
-
argmax
(dim: Optional[int] = None, keepdim: bool = False)¶ The op computes the index with the largest value of a Tensor at specified axis.
- Parameters
input (oneflow.Tensor) – Input Tensor
dim (int, optional) – dimension to be calculated. Defaults to the last dim (-1)
keepdim (bool optional) – whether the output tensor has dim retained or not. Ignored if dim=None.
- Returns
A Tensor(dtype=int64) contains the index with the largest value of input
- Return type
For example:
>>> import oneflow as flow >>> input = flow.tensor([[1, 3, 8, 7, 2], ... [1, 9, 4, 3, 2]], dtype=flow.float32) >>> output = flow.argmax(input) >>> output tensor(6, dtype=oneflow.int64) >>> output = flow.argmax(input, dim=1) >>> output tensor([2, 1], dtype=oneflow.int64)
-
argmin
(dim: Optional[int] = None, keepdim: bool = False)¶ The op computes the index with the largest value of a Tensor at specified axis.
- Parameters
input (oneflow.Tensor) – Input Tensor
dim (int, optional) – dimension to be calculated. Defaults to the last dim (-1)
keepdim (bool optional) – whether the output tensor has dim retained or not. Ignored if dim=None.
- Returns
A Tensor(dtype=int64) contains the index with the largest value of input
- Return type
For example:
>>> import oneflow as flow >>> input = flow.tensor([[4, 3, 1, 0, 2], ... [5, 9, 7, 6, 8]], dtype=flow.float32) >>> output = flow.argmin(input) >>> output tensor(3, dtype=oneflow.int64) >>> output = flow.argmin(input, dim=1) >>> output tensor([3, 0], dtype=oneflow.int64)
-
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
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)
-
asin
()¶ See
oneflow.asin()
-
asinh
()¶ See
oneflow.asinh()
-
atan
()¶ See
oneflow.atan()
-
atan2
(other)¶ See
oneflow.atan2()
-
atanh
()¶
-
backward
(gradient=None, retain_graph=False, create_graph=False)¶
-
cast
(dtype)¶
-
ceil
()¶ See
oneflow.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)¶ See
oneflow.clamp()
-
clip
(min=None, max=None)¶ See
oneflow.clamp()
-
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) # broadcast divide >>> 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
input (oneflow.Tensor) – the tensor to compare
other (oneflow.Tensor, float or int) – the target to compare
- Returns
A boolean tensor that is True where
input
is equal toother
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
()¶ See
oneflow.erf()
-
erfc
()¶ See
oneflow.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
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 toself.expand(other.size())
.Please see
expand()
for more information aboutexpand
.- Parameters
other (
oneflow.Tensor
) – The result tensor has the same size asother
.
-
expm1
()¶ See
oneflow.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)¶ See
oneflow.flip()
-
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
()¶ See
oneflow.floor()
-
fmod
(other)¶ See
oneflow.fmod()
-
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
andindex
must have the same number of dimensions. It is also required thatindex.size(d) <= input.size(d)
for all dimensionsd != dim
.out
will have the same shape asindex
. Note thatinput
andindex
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)¶
-
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
input (oneflow.Tensor) – A Tensor
other (oneflow.Tensor) – A Tensor
- Returns
A Tensor with int8 type.
- Return type
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
()¶
-
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
input (oneflow.Tensor) – A Tensor
other (oneflow.Tensor) – A Tensor
- Returns
A Tensor with int8 type.
- Return type
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 withvalue
wheremask
is True. The shape ofmask
must be broadcastable with the shape of the underlying tensor.- Parameters
mask (BoolTensor) – the boolean mask
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) >>> output = flow.masked_fill(input, mask, fill_value) # 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¶ See
oneflow.max()
-
mean
(dim, index) → Tensor¶ See
oneflow.mean()
-
min
(dim, index) → Tensor¶ See
oneflow.min()
-
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
input (oneflow.Tensor) – the tensor to compare
other (oneflow.Tensor, float or int) – the target to compare
- Returns
A boolean tensor that is True where
input
is not equal toother
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
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)
-
norm
(ord=None, dim=None, keepdim=False)¶
-
normal_
(mean=0, std=1)¶
-
numel
()¶ See
oneflow.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¶ See
oneflow.prod()
-
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)¶ Applies the rectified linear unit function element-wise. See
relu
for more details.
-
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
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¶
-
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¶ See
oneflow.sin()
-
sin_
()¶ In-place version of
oneflow.sin()
-
sinh
()¶
-
size
(idx=None)¶
-
softmax
(dim=None)¶ Applies the Softmax function to an n-dimensional input Tensor rescaling them so that the elements of the n-dimensional output Tensor lie in the range [0,1] and sum to 1.
Softmax is defined as:
\[\text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}\]When the input Tensor is a sparse tensor then the unspecifed values are treated as
-inf
.- Shape:
Input: \((*)\) where * means, any number of additional dimensions
Output: \((*)\), same shape as the input
- Returns
a Tensor of the same dimension and shape as the input with values in the range [0, 1]
- Parameters
dim (int) – A dimension along which Softmax will be computed (so every slice along dim will sum to 1).
For example:
>>> import numpy as np >>> import oneflow as flow >>> m = flow.nn.Softmax(dim = 2) >>> x = flow.Tensor( ... np.array( ... [[[-0.46716809, 0.40112534, 0.61984003], ... [-1.31244969, -0.42528763, 1.47953856]]] ... ) ... ) >>> out = m(x) >>> out tensor([[[0.1575, 0.3754, 0.4671], [0.0507, 0.1230, 0.8263]]], dtype=oneflow.float32)
-
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: Optional[Sequence[int]] = None)¶ This operator removes the specified dimention which size is 1 of the input Tensor. If the dim is not specified, this operator will remove all the dimention which size is 1 of the input Tensor.
The amount of element in return value is the same as Tensor input.
- Parameters
input (oneflow.Tensor) – The input Tensor.
dim (Optional[Sequence[int]]) – The dim. Defaults to None.
- Returns
The result Tensor.
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.Tensor(np.array([[[[1, 1, 1]]]]).astype(np.int32)) >>> input.shape oneflow.Size([1, 1, 1, 3]) >>> out = flow.squeeze(input, dim=[1, 2]).shape >>> out oneflow.Size([1, 3])
-
stack
(dim: int = 0) → None¶ Concatenates a sequence of tensors along a new dimension. The returned tensor shares the same underlying data with input tensors.
A
dim
value within the range [-input.ndimension() - 1, input.ndimension() + 1] can be used. Negativedim
will correspond tostack()
applied atdim
=dim + input.ndimension() + 1
.- Parameters
inputs (List[oneflow.Tensor]) – the list of input tensors. Each tensor should have the same shape.
dim (int) – the index at which to insert the concatenated dimension.
- Returns
A Tensor
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.Tensor(np.random.rand(1, 3, 5)) >>> y = flow.Tensor(np.random.rand(1, 3, 5)) >>> out = flow.stack([x, y], dim = -1) >>> out.shape oneflow.Size([1, 3, 5, 2])
-
std
(dim, unbiased=False, keepdim=False)¶ Returns the standard-deviation of each row of the
input
tensor in the dimensiondim
. Ifdim
is a list of dimensions, reduce over all of them.If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed, resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).
If
unbiased
isFalse
, then the standard-deviation will be calculated via the biased estimator. Otherwise, Bessel’s correction will be used.- Parameters
input (Tensor) – the input tensor.
(int or tuple of python (dim) – ints): the dimension or dimensions to reduce.
unbiased (bool) – whether to use the unbiased estimation or not
keepdim (bool) – whether the output tensor has dim retained or not.
For example:
>>> import oneflow as flow >>> import numpy as np >>> arr = np.array([1.0, 2.0, 3.0]) >>> input = flow.Tensor(arr) >>> output = flow.std(input, dim=0).numpy() >>> output array(0.8164968, dtype=float32)
-
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) # broadcast subtract >>> 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
. Thereps
argument specifies the number of repetitions in each dimension.If
reps
specifies fewer dimensions thaninput
has, then ones are prepended toreps
until all dimensions are specified. For example, ifinput
has shape (8, 6, 4, 2) andreps
is (2, 2), thenreps
is treated as (1, 1, 2, 2).Analogously, if
input
has fewer dimensions thanreps
specifies, theninput
is treated as if it were unsqueezed at dimension zero until it has as many dimensions asreps
specifies. For example, ifinput
has shape (4, 2) andreps
is (3, 3, 2, 2), theninput
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 correctflow.dtype
andflow.device
, theninput
is returned. Otherwise, the returned tensor is a copy ofinput
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
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
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. Negativedim
will correspond tounsqueeze()
applied atdim
=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)¶ Returns the variance of each row of the input tensor in the given dimension dim.
If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see flow.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).
- Parameters
input (Tensor) – the input tensor.
(int or tuple of python (dim) – ints): the dimension or dimensions to reduce. Defaults to None.
unbiased (bool, optional) – whether to use Bessel’s correction (\(\delta N = 1\)). Defaults to True.
keepdim (bool, optional) – whether the output tensor has dim retained or not. Defaults to False.
- Returns
The result of variance on the specified axis of input Tensor
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow >>> np_arr = np.random.randn(2,3,4,5) >>> input = flow.Tensor(np_arr) >>> output = flow.var(input, 1, True)
-
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 differentshape
.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 asshape
without copying it (e.g., viacontiguous()
). When it is unclear whether aview()
can be performed, it is advisable to usereshape()
, which returns a view if the shapes are compatible, and copies (equivalent to callingcontiguous()
) 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
ory
, depending oncondition
. 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 andy
is None, flow.where(condition) is identical to flow.nonzero(condition, as_tuple=True).The tensors
condition
,x
,y
must be broadcastable.- Parameters
- Returns
A tensor of shape equal to the broadcasted shape of
condition
,x
,y
- Return type
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¶
-