oneflow¶
oneflow¶
Copyright 2020 The OneFlow Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
class
oneflow.
BoolTensor
¶ Creates a Tensor with the dtype of oneflow.bool and the device on cpu, it has the same parameters as
oneflow.Tensor()
-
class
oneflow.
ByteTensor
¶ Creates a Tensor with the dtype of oneflow.uint8 and the device on cpu, it has the same parameters as
oneflow.Tensor()
-
class
oneflow.
CharTensor
¶ Creates a Tensor with the dtype of oneflow.int8 and the device on cpu, it has the same parameters as
oneflow.Tensor()
-
class
oneflow.
DoubleTensor
¶ Creates a Tensor with the dtype of oneflow.float64 and the device on cpu, it has the same parameters as
oneflow.Tensor()
-
class
oneflow.
FloatTensor
¶ Creates a Tensor with the dtype of oneflow.float32 and the device on cpu, it has the same parameters as
oneflow.Tensor()
-
class
oneflow.
HalfTensor
¶ Creates a Tensor with the dtype of oneflow.float16 and the device on cpu, it has the same parameters as
oneflow.Tensor()
-
class
oneflow.
IntTensor
¶ Creates a Tensor with the dtype of oneflow.int32 and the device on cpu, it has the same parameters as
oneflow.Tensor()
-
class
oneflow.
LongTensor
¶ Creates a Tensor with the dtype of oneflow.int64 and the device on cpu, it has the same parameters as
oneflow.Tensor()
-
oneflow.
abs
()¶ Return the absolute value of each element in input tensor:math:y = |x| element-wise.
- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.tensor(np.array([-1, 2, -3, 4]).astype(np.float32)) >>> flow.abs(x) tensor([1., 2., 3., 4.], dtype=oneflow.float32)
-
oneflow.
acos
()¶ Returns a new tensor with the inverse cosine of the elements of
input
.\[\text{out}_{i} = \arccos(\text{input}_{i})\]- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> arr = np.array([0.5, 0.6, 0.7]) >>> input = flow.tensor(arr, dtype=flow.float32) >>> output = flow.acos(input) >>> output tensor([1.0472, 0.9273, 0.7954], dtype=oneflow.float32)
-
oneflow.
acosh
()¶ Returns a new tensor with the inverse hyperbolic cosine of the elements of
input
.\[\text{out}_{i} = \cosh^{-1}(\text{input}_{i})\]- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> x1 = flow.tensor(np.array([2, 3, 4]).astype(np.float32)) >>> out1 = flow.acosh(x1) >>> out1 tensor([1.3170, 1.7627, 2.0634], dtype=oneflow.float32) >>> x2 = flow.tensor(np.array([1.5, 2.6, 3.7]).astype(np.float32),device=flow.device('cuda')) >>> out2 = flow.acosh(x2) >>> out2 tensor([0.9624, 1.6094, 1.9827], device='cuda:0', dtype=oneflow.float32)
-
oneflow.
adaptive_avg_pool1d
(input, output_size)¶ Applies a 1D adaptive average pooling over an input signal composed of several input planes.
See
oneflow.nn.AdaptiveAvgPool1d
- Parameters
input – input tensor
output_size – the target output size (single integer)
-
oneflow.
adaptive_avg_pool2d
(input, output_size)¶ Applies a 2D adaptive average pooling over an input signal composed of several input planes.
See
oneflow.nn.AdaptiveAvgPool2d
- Parameters
input – input tensor
output_size – the target output size (single integer or double-integer tuple)
-
oneflow.
adaptive_avg_pool3d
(input, output_size)¶ Applies a 3D adaptive average pooling over an input signal composed of several input planes.
See
oneflow.nn.AdaptiveAvgPool3d
- Parameters
input – input tensor
output_size – the target output size (single integer or triple-integer tuple)
-
oneflow.
add
(input, other, *, alpha=1) → Tensor¶ Adds other, scaled by alpha, to input. Scalar and broadcast promotation are supported.
\[out = input + alpha \times other\]- Parameters
input (Union[int, float, oneflow.Tensor]) – the input tensor.
other (Union[int, float, oneflow.Tensor]) – the tensor or number to add to input.
- Keyword Arguments
alpha (Number, optional) – the multiplier for other.
- Returns
the output Tensor.
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow # element-wise add >>> x = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> y = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> out = flow.add(x, y).numpy() >>> out.shape (2, 3) # scalar add >>> x = 5 >>> y = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> out = flow.add(x, y).numpy() >>> out.shape (2, 3) # broadcast add >>> x = flow.tensor(np.random.randn(1,1), dtype=flow.float32) >>> y = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> out = flow.add(x, y).numpy() >>> out.shape (2, 3) # use alpha >>> x = flow.zeros(2, 3) >>> y = flow.ones(2, 3) >>> out = flow.add(x, y, alpha=10) >>> out tensor([[10., 10., 10.], [10., 10., 10.]], dtype=oneflow.float32)
-
oneflow.
addcmul
(input, tensor1, tensor2, *, value=1) → Tensor¶ Performs the element-wise multiplication of tensor1 by tensor2, multiply the result by the scalar value and add it to input. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.addcmul.html
\[\text{out}_i = \text{input}_i + value \times\ \text{tensor1}_i \times\ \text{tensor2}_i\]- Parameters
- Keyword Arguments
value (Number, optional) – multiplier for \(tensor1 * tensor2\).
- Returns
the output Tensor.
- Return type
For example:
>>> import oneflow as flow >>> input = flow.rand(2, 3, 4) >>> tensor1 = flow.rand(2, 3, 4) >>> tensor2 = flow.rand(2, 3, 4) >>> out = flow.addcmul(input, tensor1, tensor2, value=2) >>> out.size() oneflow.Size([2, 3, 4])
-
oneflow.
addmm
(beta=1, input, alpha=1, mat1, mat2, out=None) → Tensor¶ Performs a matrix multiplication of the matrices
mat1
andmat2
. The matrixinput
is added to the final result.If
mat1
is a \((n \times m)\) tensor,mat2
is a \((m \times p)\) tensor, theninput
must be broadcastable with a \((n \times p)\) tensor andout
will be a \((n \times p)\) tensor.alpha
andbeta
are scaling factors on matrix-vector product betweenmat1
andmat2
and the added matrixinput
respectively.\[\text{out} = \beta\ \text{input} + \alpha\ (\text{mat1}_i \mathbin{@} \text{mat2}_i)\]For inputs of type float or double, arguments
beta
andalpha
must be real numbers, otherwise they should be integers.- Parameters
beta (Number, optional) – multiplier for
input
(\(\beta\))input (Tensor) – matrix to be added
alpha (Number, optional) – multiplier for \(mat1 @ mat2\) (\(\alpha\))
mat1 (Tensor) – the first matrix to be multiplied
mat2 (Tensor) – the second matrix to be multiplied
out (Tensor, optional) – the output tensor.
For example:
>>> import numpy as np >>> import oneflow as flow >>> input = flow.tensor(np.array([[1,2,4],[5,11,9.1]])) >>> mat1 = flow.tensor(np.array([[7.3,1.9,7.3],[10.2,1,5.5]])) >>> mat2 = flow.tensor(np.array([[7.3,1.9,7.3],[10.2,1,5.5],[3.7,2.2,8.1]])) >>> output = flow.addmm(input, mat1, mat2) >>> output tensor([[100.6800, 33.8300, 126.8700], [110.0100, 43.4800, 133.6100]], dtype=oneflow.float64) >>> output.shape oneflow.Size([2, 3])
>>> input2 = flow.tensor(np.array([1.7])) >>> mat1 = flow.tensor(np.array([[1,2],[5,9.1],[7.7,1.4]])) >>> mat2 = flow.tensor(np.array([[1,2,3.7],[5,9.1,6.8]])) >>> output2 = flow.addmm(input2, mat1, mat2, alpha=1, beta=2) >>> output2 tensor([[14.4000, 23.6000, 20.7000], [53.9000, 96.2100, 83.7800], [18.1000, 31.5400, 41.4100]], dtype=oneflow.float64) >>> output2.shape oneflow.Size([3, 3])
-
oneflow.
all
(input, dim=None, keepdim=False) → Tensor¶ For each row of input in the given dimension dim, returns True if all element in the row evaluate to True and False otherwise. If the dimension is None, compute if all elements in the input tensor to true.
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
oneflow.squeeze()
, resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).- Parameters
input (oneflow.Tensor) – the Input Tensor
dim (int, optional) – the dimension to reduce. Default: None
keepdim (bool, optional) – whether the output tensor has dim retained or not. Default: False
For example:
>>> import oneflow as flow >>> input = flow.Tensor([[1, 2, 3], [4, 5, 6]]) < 4 >>> input tensor([[ True, True, True], [False, False, False]], dtype=oneflow.bool) >>> flow.all(input) tensor(False, dtype=oneflow.bool) >>> flow.all(input, 1) tensor([ True, False], dtype=oneflow.bool) >>> flow.all(input, 1, True) tensor([[ True], [False]], dtype=oneflow.bool)
-
oneflow.
amax
(input, dim=None, keepdim=False) → Tensor¶ This function is equivalent to PyTorch’s amax function. It returns the maximum along a dimension.
- Parameters
input (oneflow.Tensor) – the input Tensor.
dim (int or List of int, optional) – the dimension or the dimensions to reduce. Dim is None by default.
keepdim (bool, optional) – whether to retain the dimension. keepdim is False by default.
- Returns
Maximum of the input tensor
- Return type
For example:
>>> import oneflow as flow >>> x = flow.tensor([[[0,1],[2,3]],[[4,5],[6,7]]]) >>> flow.amax(x, 1) tensor([[2, 3], [6, 7]], dtype=oneflow.int64) >>> flow.amax(x, 0) tensor([[4, 5], [6, 7]], dtype=oneflow.int64) >>> flow.amax(x) tensor(7, dtype=oneflow.int64) >>> flow.amax(x, 0, True) tensor([[[4, 5], [6, 7]]], dtype=oneflow.int64)
-
oneflow.
amin
(input, dim, keepdim=False) → Tensor¶ This function is equivalent to PyTorch’s amin function. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.amin.html.
Returns the minimum value of each slice of the input tensor in the given dimension(s) 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
oneflow.squeeze()
), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).- Parameters
input (oneflow.Tensor) – the input Tensor.
dim (int, Tuple[int]) – the dimension or dimensions to reduce.
keepdim (bool) – whether the output tensor has dim retained or not.
Example:
>>> import oneflow as flow >>> x = flow.tensor([[[0,1],[2,3]],[[4,5],[6,7]]]) >>> flow.amin(x, 1) tensor([[0, 1], [4, 5]], dtype=oneflow.int64) >>> flow.amin(x, 0) tensor([[0, 1], [2, 3]], dtype=oneflow.int64) >>> flow.amin(x) tensor(0, dtype=oneflow.int64) >>> flow.amin(x, 0, True) tensor([[[0, 1], [2, 3]]], dtype=oneflow.int64)
-
oneflow.
any
(input, dim=None, keepdim=False) → Tensor¶ For each row of input in the given dimension dim, returns True if any element in the row evaluate to True and False otherwise. If the dimension is None, compute if any elements in the input tensor to true.
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
oneflow.squeeze()
, resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).- Parameters
input (oneflow.Tensor) – the Input Tensor
dim (int, optional) – the dimension to reduce. Default: None
keepdim (bool, optional) – whether the output tensor has dim retained or not. Default: False
For example:
>>> import oneflow as flow >>> input = flow.Tensor([[1, 2, 3], [4, 5, 6]]) < 4 >>> input tensor([[ True, True, True], [False, False, False]], dtype=oneflow.bool) >>> flow.any(input) tensor(True, dtype=oneflow.bool) >>> flow.any(input, 0) tensor([True, True, True], dtype=oneflow.bool) >>> flow.any(input, 0, True) tensor([[True, True, True]], dtype=oneflow.bool)
-
oneflow.
arange
(start: int = 0, end, step: int = 1, dtype: Optional[oneflow._oneflow_internal.dtype] = None, device: Optional[Union[oneflow._oneflow_internal.device, str]] = None, placement: Optional[oneflow._oneflow_internal.placement] = None, sbp: Optional[Union[oneflow._oneflow_internal.sbp.sbp, List[oneflow._oneflow_internal.sbp.sbp]]] = None, requires_grad: bool = False)¶ Returns a 1-D tensor of size \(\left\lfloor \frac{\text{end} - \text{start}}{\text{step}} \right\rfloor + 1\) with values from
start
toend
with stepstep
. Step is the gap between two values in the tensor.\[\text{out}_{i+1} = \text{out}_i + \text{step}.\]- Parameters
start (int) – the starting value for the set of points. Default:
0
.end (int) – the ending value for the set of points
step (int) – the gap between each pair of adjacent points. Default:
1
.
- Keyword Arguments
dtype (flow.dtype, optional) – If dtype is not given, infer the dtype from the other input arguments. If any of start, end, or step are floating-point, the dtype is inferred to be the floating-point data type. Otherwise, the dtype is inferred to be flow.int64.
device (flow.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor.
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
For example:
>>> import oneflow as flow >>> y = flow.arange(0, 5) >>> y tensor([0, 1, 2, 3, 4], dtype=oneflow.int64)
-
oneflow.
arccos
()¶ Returns a new tensor with the inverse cosine of the elements of
input
.\[\text{out}_{i} = \arccos(\text{input}_{i})\]- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> arr = np.array([0.5, 0.6, 0.7]) >>> input = flow.tensor(arr, dtype=flow.float32) >>> output = flow.acos(input) >>> output tensor([1.0472, 0.9273, 0.7954], dtype=oneflow.float32)
-
oneflow.
arccosh
()¶ Returns a new tensor with the inverse hyperbolic cosine of the elements of
input
.\[\text{out}_{i} = \cosh^{-1}(\text{input}_{i})\]- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> x1 = flow.tensor(np.array([2, 3, 4]).astype(np.float32)) >>> out1 = flow.acosh(x1) >>> out1 tensor([1.3170, 1.7627, 2.0634], dtype=oneflow.float32) >>> x2 = flow.tensor(np.array([1.5, 2.6, 3.7]).astype(np.float32),device=flow.device('cuda')) >>> out2 = flow.acosh(x2) >>> out2 tensor([0.9624, 1.6094, 1.9827], device='cuda:0', dtype=oneflow.float32)
-
oneflow.
arcsin
()¶ Returns a new tensor with the arcsine of the elements of
input
.\[\text{out}_{i} = \sin^{-1}(\text{input}_{i})\]- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor(np.array([-0.5, 0.8, 1.0, -0.8]), dtype=flow.float32) >>> output = flow.asin(input) >>> output.shape oneflow.Size([4]) >>> output tensor([-0.5236, 0.9273, 1.5708, -0.9273], dtype=oneflow.float32) >>> input1 = flow.tensor(np.array([[0.8, 1.0], [-0.6, -1.0]]), dtype=flow.float32) >>> output1 = input1.asin() >>> output1.shape oneflow.Size([2, 2]) >>> output1 tensor([[ 0.9273, 1.5708], [-0.6435, -1.5708]], dtype=oneflow.float32)
-
oneflow.
arcsinh
()¶ Returns a new tensor with the inverse hyperbolic sine of the elements of
input
.\[\text{out}_{i} = \sinh^{-1}(\text{input}_{i})\]- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor(np.array([2, 3, 4]), dtype=flow.float32) >>> output = flow.asinh(input) >>> output.shape oneflow.Size([3]) >>> output tensor([1.4436, 1.8184, 2.0947], dtype=oneflow.float32) >>> input1 = flow.tensor(np.array([[-1, 0, -0.4], [5, 7, 0.8]]), dtype=flow.float32) >>> output1 = input1.asinh() >>> output1.shape oneflow.Size([2, 3]) >>> output1 tensor([[-0.8814, 0.0000, -0.3900], [ 2.3124, 2.6441, 0.7327]], dtype=oneflow.float32)
-
oneflow.
arctan
()¶ Returns a new tensor with the arctangent of the elements of
input
.\[\text{out}_{i} = \tan^{-1}(\text{input}_{i})\]- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor(np.array([0.5, 0.6, 0.7]), dtype=flow.float32) >>> output = flow.atan(input) >>> output.shape oneflow.Size([3])
-
oneflow.
arctanh
()¶ Returns a new tensor with the inverse hyperbolic tangent of the elements of
input
.\[\text{out}_{i} = \tanh^{-1}(\text{input}_{i})\]- 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) >>> output = flow.atanh(input) >>> output tensor([0.5493, 0.6931, 0.8673], dtype=oneflow.float32)
-
oneflow.
argmax
()¶ 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)
-
oneflow.
argmin
()¶ 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)
-
oneflow.
argsort
() → Tensor¶ This operator sorts the input Tensor at specified dim and returns the indices of the sorted Tensor.
- Parameters
input (oneflow.Tensor) – the input Tensor.
dim (int, optional) – the 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)
-
oneflow.
argwhere
(input, dtype: Optional[oneflow._oneflow_internal.dtype] = oneflow.int32)¶ This operator finds the indices of input Tensor input elements that are non-zero.
It returns a list in which each element is a coordinate that points to a non-zero element in the condition.
- Parameters
input (oneflow.Tensor) – the input Tensor.
dtype (Optional[flow.dtype], optional) – The data type of output. Defaults to None.
- Returns
The result Tensor.
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([[0, 1, 0], ... [2, 0, 2]]).astype(np.float32) >>> input = flow.Tensor(x) >>> output = flow.argwhere(input) >>> output tensor([[0, 1], [1, 0], [1, 2]], dtype=oneflow.int32)
-
oneflow.
as_strided
()¶ Create a view of an existing oneflow.Tensor input with specified size, stride and storage_offset. The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.as_strided.html.
- Parameters
input (Tensor) – the input tensor.
size (tuple or ints) – the shape of the output tensor.
stride (tuple or ints) – the stride of the output tensor.
storage_offset (int) – the offset in the underlying storage of the output tensor
- Returns
the output Tensor.
- Return type
For example:
>>> import oneflow as flow >>> input = flow.rand(2,3,5) >>> output = flow.as_strided(input, (2,3,3), (1,2,3), 1) >>> output.size() oneflow.Size([2, 3, 3])
-
oneflow.
as_tensor
(data, dtype=None, device=None) → Tensor¶ The interface is consistent with PyTorch.
Converts data into a tensor, sharing data and preserving autograd history if possible.
If data is already a tensor with the requeseted dtype and device then data itself is returned, but if data is a tensor with a different dtype or device then it’s copied as if using data.to(dtype=dtype, device=device).
If data is a NumPy array (an ndarray) with the same dtype and device then a tensor is constructed using oneflow.from_numpy.
- Parameters
data (array_like) – Initial data for the tensor. Can be a list, tuple, NumPy
ndarray
, scalar, and other types.dtype (oneflow.dtype, optional) – the desired data type of returned tensor. Default: if
None
, infers data type from data.device (oneflow.device, optional) – the device of the constructed tensor. If
None
and data is a tensor then the device of data is used. If None and data is not a tensor then the result tensor is constructed on the CPU.
For example:
>>> import oneflow as flow >>> import numpy as np >>> a = np.array([1, 2, 3]) >>> t = flow.as_tensor(a, device=flow.device('cuda')) >>> t tensor([1, 2, 3], device='cuda:0', dtype=oneflow.int64) >>> t[0] = -1 >>> a array([1, 2, 3])
-
oneflow.
asin
()¶ Returns a new tensor with the arcsine of the elements of
input
.\[\text{out}_{i} = \sin^{-1}(\text{input}_{i})\]- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor(np.array([-0.5, 0.8, 1.0, -0.8]), dtype=flow.float32) >>> output = flow.asin(input) >>> output.shape oneflow.Size([4]) >>> output tensor([-0.5236, 0.9273, 1.5708, -0.9273], dtype=oneflow.float32) >>> input1 = flow.tensor(np.array([[0.8, 1.0], [-0.6, -1.0]]), dtype=flow.float32) >>> output1 = input1.asin() >>> output1.shape oneflow.Size([2, 2]) >>> output1 tensor([[ 0.9273, 1.5708], [-0.6435, -1.5708]], dtype=oneflow.float32)
-
oneflow.
asinh
()¶ Returns a new tensor with the inverse hyperbolic sine of the elements of
input
.\[\text{out}_{i} = \sinh^{-1}(\text{input}_{i})\]- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor(np.array([2, 3, 4]), dtype=flow.float32) >>> output = flow.asinh(input) >>> output.shape oneflow.Size([3]) >>> output tensor([1.4436, 1.8184, 2.0947], dtype=oneflow.float32) >>> input1 = flow.tensor(np.array([[-1, 0, -0.4], [5, 7, 0.8]]), dtype=flow.float32) >>> output1 = input1.asinh() >>> output1.shape oneflow.Size([2, 3]) >>> output1 tensor([[-0.8814, 0.0000, -0.3900], [ 2.3124, 2.6441, 0.7327]], dtype=oneflow.float32)
-
oneflow.
atan
()¶ Returns a new tensor with the arctangent of the elements of
input
.\[\text{out}_{i} = \tan^{-1}(\text{input}_{i})\]- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor(np.array([0.5, 0.6, 0.7]), dtype=flow.float32) >>> output = flow.atan(input) >>> output.shape oneflow.Size([3])
-
oneflow.
atan2
()¶ Element-wise arctangent of input{i}/other{i} with consideration of the quadrant. Returns a new tensor with the signed angles in radians between vector (other{i},input{i}) and vector (1, 0).
The shapes of input and other must be broadcastable.
For example:
>>> import oneflow as flow >>> import numpy as np >>> x1 = flow.Tensor(np.array([1,2,3])) >>> y1 = flow.Tensor(np.array([3,2,1])) >>> x2 = flow.Tensor(np.array([1.53123589,0.54242598,0.15117185])) >>> y2 = flow.Tensor(np.array([-0.21906378,0.09467151,-0.75562878])) >>> x3 = flow.Tensor(np.array([1,0,-1])) >>> y3 = flow.Tensor(np.array([0,1,0])) >>> flow.atan2(x1,y1).numpy() array([0.32175055, 0.7853982 , 1.2490457 ], dtype=float32) >>> flow.atan2(x2,y2).numpy() array([1.7128955, 1.3980033, 2.9441385], dtype=float32) >>> flow.atan2(x3,y3).numpy() array([ 1.5707964, 0. , -1.5707964], dtype=float32)
-
oneflow.
atanh
()¶ Returns a new tensor with the inverse hyperbolic tangent of the elements of
input
.\[\text{out}_{i} = \tanh^{-1}(\text{input}_{i})\]- 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) >>> output = flow.atanh(input) >>> output tensor([0.5493, 0.6931, 0.8673], dtype=oneflow.float32)
-
oneflow.
batch_gather
()¶ Gather the element in batch dims.
- Parameters
For example:
Example 1:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.Tensor(np.array([[1, 2, 3], ... [4, 5, 6]])) >>> indices = flow.tensor(np.array([1, 0]).astype(np.int64)) >>> out = flow.batch_gather(x, indices) tensor([[4., 5., 6.], [1., 2., 3.]], dtype=oneflow.float32)
Example 2:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.Tensor(np.array([[[1, 2, 3], [4, 5, 6]], ... [[1, 2, 3], [4, 5, 6]]])) >>> indices = flow.tensor(np.array([[1, 0], ... [0, 1]]).astype(np.int64)) >>> out = flow.batch_gather(x, indices) tensor([[[4., 5., 6.], [1., 2., 3.]], [[1., 2., 3.], [4., 5., 6.]]], dtype=oneflow.float32)
-
oneflow.
bernoulli
(x, *, generator=None, out=None)¶ This operator returns a Tensor with binaray random numbers (0 / 1) from a Bernoulli distribution.
- Parameters
- Shape:
Input: \((*)\). Input can be of any shape
Output: \((*)\). Output is of the same shape as input
For example:
>>> import numpy as np >>> import oneflow as flow >>> arr = np.array( ... [ ... [1.0, 1.0, 1.0], ... [1.0, 1.0, 1.0], ... [1.0, 1.0, 1.0], ... ] ... ) >>> x = flow.tensor(arr, dtype=flow.float32) >>> y = flow.bernoulli(x) >>> y tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], dtype=oneflow.float32)
-
oneflow.
bmm
()¶ Performs a batch matrix-matrix product of matrices stored in input and mat2.
input and mat2 must be 3-D tensors each containing the same number of matrices.
If input is a (b x n x m) tensor, mat2 is a (b x m x p) tensor, out will be a (b x n x p) tensor.
- Parameters
input (oneflow.Tensor) – the first batch of matrices to be multiplied
mat2 (oneflow.Tensor) – the second batch of matrices to be multiplied
For example:
>>> import oneflow as flow >>> import numpy as np >>> input1 = flow.randn(10, 3, 4) >>> input2 = flow.randn(10, 4, 5) >>> of_out = flow.bmm(input1, input2) >>> of_out.shape oneflow.Size([10, 3, 5])
-
oneflow.
broadcast_like
()¶ This operator broadcast tensor x to like_tensor according to the broadcast_axes.
- Parameters
- Returns
Broadcasted input Tensor.
- Return type
[Tensor]
For example:
>>> import oneflow as flow >>> x = flow.randn(3, 1, 1) >>> like_tensor = flow.randn(3, 4, 5) >>> broadcast_tensor = flow.broadcast_like(x, like_tensor, broadcast_axes=[1, 2]) >>> broadcast_tensor.shape oneflow.Size([3, 4, 5])
-
oneflow.
cast
()¶ The operation takes input tensor x and casts it to the output with dtype
- Parameters
x (oneflow.Tensor) – A Tensor
dtype (flow.dtype) – Data type of the output tensor
- Returns
A Tensor with specific dtype.
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> np_arr = np.random.randn(2, 3, 4, 5).astype(np.float32) >>> input = flow.tensor(np_arr, dtype=flow.float32) >>> output = flow.cast(input, flow.int8) >>> np.array_equal(output.numpy(), np_arr.astype(np.int8)) True
-
oneflow.
cat
(tensors, dim=0) → Tensor¶ Concatenate two or more Tensor s at specified dim.
Analogous to numpy.concatenate
- Parameters
inputs – a list of Tensor
dim – a int.
- Returns
A Tensor
For example:
>>> import oneflow as flow >>> import numpy as np >>> input1 = flow.tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) >>> input2 = flow.tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) >>> input3 = flow.tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) >>> out = flow.cat([input1, input2, input3], dim=1) # equal to using flow.concat() >>> out.shape oneflow.Size([2, 18, 5, 3])
-
oneflow.
ceil
()¶ Returns a new tensor with the ceil of the elements of
input
, the smallest integer greater than or equal to each element.The equation is:
\[\text{out}_{i} = \left\lceil \text{input}_{i} \right\rceil = \left\lfloor \text{input}_{i} \right\rfloor + 1\]- Parameters
input (oneflow.Tensor) – A Tensor.
- Returns
The result Tensor
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.tensor(np.array([0.1, -2, 3.4]).astype(np.float32)) >>> y = flow.ceil(x) >>> y.shape oneflow.Size([3]) >>> y tensor([ 1., -2., 4.], dtype=oneflow.float32) >>> x = flow.tensor(np.array([[2.5, 4.6, 0.6],[7.8, 8.3, 9.2]]).astype(np.float32)) >>> y = x.ceil() >>> y.shape oneflow.Size([2, 3]) >>> y tensor([[ 3., 5., 1.], [ 8., 9., 10.]], dtype=oneflow.float32) >>> x = flow.tensor(np.array([[[2.2, 4.4, 6.5],[7.1, 8.2, 9.3]],[[10.6,11.2,12.2],[13.5,14.8,15.9]]]).astype(np.float32)) >>> y = flow.ceil(x) >>> y.shape oneflow.Size([2, 2, 3]) >>> y tensor([[[ 3., 5., 7.], [ 8., 9., 10.]], [[11., 12., 13.], [14., 15., 16.]]], dtype=oneflow.float32)
-
oneflow.
chunk
()¶ Splits a tensor into a specific number of chunks. Each chunk is a view of the input tensor. Last chunk will be bigger 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 >>> arr = np.random.randn(5, 3, 6, 9).astype(np.float32) >>> input = flow.tensor(arr) >>> output = [] >>> chunks = 3 >>> output = flow.chunk(input, chunks=chunks, dim=2) >>> out_shape = [] >>> for i in range(0, chunks): ... out_shape.append(output[i].numpy().shape) >>> out_shape [(5, 3, 2, 9), (5, 3, 2, 9), (5, 3, 2, 9)]
-
oneflow.
clamp
()¶ Clamp all elements in
input
into the range [min
,max
] and return a resulting tensor:\[\begin{split}y_i = \begin{cases} \text{min} & \text{if } x_i < \text{min} \\ x_i & \text{if } \text{min} \leq x_i \leq \text{max} \\ \text{max} & \text{if } x_i > \text{max} \end{cases}\end{split}\]If
input
is of type FloatTensor or DoubleTensor, argsmin
andmax
must be real numbers, otherwise they should be integers.- Parameters
For example:
>>> import oneflow as flow >>> import numpy as np >>> arr = np.array([0.2, 0.6, -1.5, -0.3]) >>> input = flow.Tensor(arr) >>> output = flow.clamp(input, min=-0.5, max=0.5) >>> output tensor([ 0.2000, 0.5000, -0.5000, -0.3000], dtype=oneflow.float32) >>> arr = np.array([0.2, 0.6, -1.5, -0.3]) >>> input = flow.Tensor(arr) >>> output = flow.clamp(input, min=None, max=0.5) >>> output tensor([ 0.2000, 0.5000, -1.5000, -0.3000], dtype=oneflow.float32) >>> arr = np.array([0.2, 0.6, -1.5, -0.3]) >>> input = flow.Tensor(arr) >>> output = flow.clamp(input, min=-0.5, max=None) >>> output tensor([ 0.2000, 0.6000, -0.5000, -0.3000], dtype=oneflow.float32)
-
oneflow.
clip
()¶ Alias for
oneflow.clamp()
.
-
oneflow.
concat
()¶ cat(tensors, dim=0) -> Tensor
Concatenate two or more Tensor s at specified dim.
Analogous to numpy.concatenate
- Parameters
inputs – a list of Tensor
dim – a int.
- Returns
A Tensor
For example:
>>> import oneflow as flow >>> import numpy as np >>> input1 = flow.tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) >>> input2 = flow.tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) >>> input3 = flow.tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) >>> out = flow.cat([input1, input2, input3], dim=1) # equal to using flow.concat() >>> out.shape oneflow.Size([2, 18, 5, 3])
-
oneflow.
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()
-
oneflow.
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)
-
oneflow.
cumprod
(input, dim) → Tensor¶ This operator computes the cumulative product of input elements in the given dimension.
The equation is:
$$ y_{i}=x_{0}*x_{1}*…*x_{i} $$
- Parameters
input (Tensor) – the input tensor.
dim (int) – the dimension to do cumsum whose valid range is [-N, N-1), and the N is tensor’s dimensions
- Returns
The result tensor with cumprod result.
- Return type
For example:
>>> import oneflow as flow >>> input=flow.tensor([1, 2, 3]) >>> flow.cumprod(input, dim=0) tensor([1, 2, 6], dtype=oneflow.int64)
-
oneflow.
cumsum
(input, dim) → Tensor¶ This operator computes the cumulative sum of input elements in the given dimension.
The equation is:
$$ y_{i}=x_{0}+x_{1}+…+x_{i} $$
- Parameters
input (Tensor) – the input ND tensor.
dim (int) – the dimension to do cumsum, valid range is [-N, N-1), N is tensor’s dimensions
- Returns
The result tensor with cumsum result.
- Return type
For example:
>>> import oneflow as flow >>> input = flow.ones(3, 3) >>> dim = 1 >>> flow.cumsum(input, dim) tensor([[1., 2., 3.], [1., 2., 3.], [1., 2., 3.]], dtype=oneflow.float32)
-
oneflow.
decode_onerec
(input: Tensor, key: str, dtype, shape, is_dynamic=False, reshape=None, batch_padding=None) → Tensor¶ Decode a tensor from input which should be generated before by oneflow.nn.OneRecReader.
- Parameters
input – (Tensor): The tensor generated by oneflow.nn.OneRecReader before.
key (str) – The field name of the tensor to be decode
shape (bool) – The shape of the tensor to be decode
is_dynamic (bool) – The tensor shape is dynamic or not
reshape (tuple) – Set it if you want to reshape the tensor
batch_padding (tuple) – Set it if batch padding is needed
For example:
import oneflow as flow files = ['file01.onerec', 'file02.onerec'] # read onerec dataset form files reader = flow.nn.OneRecReader(files, 10, True, "batch") readdata = reader() # decode labels = flow.decode_onerec(readdata, key="labels", dtype=flow.int32, shape=(1,)) dense_fields = flow.decode_onerec(readdata, key="dense_fields", dtype=flow.float, shape=(13,))
-
oneflow.
diag
()¶ If input is a vector (1-D tensor), then returns a 2-D square tensor with the elements of input as the diagonal. If input is a matrix (2-D tensor), then returns a 1-D tensor with diagonal elements of input.
- Parameters
input (Tensor) – the input tensor.
diagonal (Optional[int], 0) – The diagonal to consider. If diagonal = 0, it is the main diagonal. If diagonal > 0, it is above the main diagonal. If diagonal < 0, it is below the main diagonal. Defaults to 0.
- Returns
the output Tensor.
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> arr = np.array( ... [ ... [1.0, 2.0, 3.0], ... [4.0, 5.0, 6.0], ... [7.0, 8.0, 9.0], ... ] ... ) >>> input = flow.tensor(arr, dtype=flow.float32) >>> flow.diag(input) tensor([1., 5., 9.], dtype=oneflow.float32)
-
oneflow.
diagonal
(input, offset, dim1, dim2) → Tensor¶ Returns a partial view of input with the its diagonal elements with respect to dim1 and dim2 appended as a dimension at the end of the shape.
- Parameters
input (Tensor) – the input tensor.Must be at least 2-dimensional.
offset (Optional[int], 0) – which diagonal to consider. Default: 0 (main diagonal)
dim1 (Optional[int], 0) – first dimension with respect to which to take diagonal. Default: 0
dim2 (Optional[int], 1) – second dimension with respect to which to take diagonal. Default: 1
- Returns
the output Tensor.
- Return type
For example:
>>> import oneflow as flow >>> input = flow.randn(2, 3, 4) >>> output = flow.diagonal(input, offset=1, dim1=1, dim2=0) >>> output.shape oneflow.Size([4, 1])
-
oneflow.
div
()¶ 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, oneflow.Tensor]) – input.
other (Union[int, float, oneflow.Tensor]) – other.
For example:
>>> import numpy as np >>> import oneflow as flow # element-wise divide >>> input = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> other = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> out = flow.div(input,other).numpy() >>> out.shape (2, 3) # scalar divide >>> input = 5 >>> other = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> out = flow.div(input,other).numpy() >>> out.shape (2, 3) # broadcast divide >>> input = flow.tensor(np.random.randn(1,1), dtype=flow.float32) >>> other = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> out = flow.div(input,other).numpy() >>> out.shape (2, 3)
-
oneflow.
dot
()¶ This operator computes the dot product of tensor input and other.
The equation is:
$$ \sum_{i=1}^{n}(x[i] * y[i]) $$
- Parameters
- Shape:
input: Input must be 1D.
other: Other must be 1D.
For example:
>>> import oneflow as flow >>> flow.dot(flow.Tensor([2, 3]), flow.Tensor([2, 1])) tensor(7., dtype=oneflow.float32)
-
oneflow.
einsum
(equation, *operands) → oneflow.Tensor¶ Sums the product of the elements of the input
operands
along dimensions specified using a notation based on the Einstein summation convention.Einsum allows computing many common multi-dimensional linear algebraic array operations by representing them in a short-hand format based on the Einstein summation convention, given by
equation
. The details of this format are described below, but the general idea is to label every dimension of the inputoperands
with some subscript and define which subscripts are part of the output. The output is then computed by summing the product of the elements of theoperands
along the dimensions whose subscripts are not part of the output. For example, matrix multiplication can be computed using einsum as flow.einsum(“ij,jk->ik”, A, B). Here, j is the summation subscript and i and k the output subscripts (see section below for more details on why).Equation:
The
equation
string specifies the subscripts (letters in [a-zA-Z]) for each dimension of the inputoperands
in the same order as the dimensions, separating subcripts for each operand by a comma (‘,’), e.g. ‘ij,jk’ specify subscripts for two 2D operands. The dimensions labeled with the same subscript must be broadcastable, that is, their size must either match or be 1. The exception is if a subscript is repeated for the same input operand, in which case the dimensions labeled with this subscript for this operand must match in size and the operand will be replaced by its diagonal along these dimensions. The subscripts that appear exactly once in theequation
will be part of the output, sorted in increasing alphabetical order. The output is computed by multiplying the inputoperands
element-wise, with their dimensions aligned based on the subscripts, and then summing out the dimensions whose subscripts are not part of the output.Optionally, the output subscripts can be explicitly defined by adding an arrow (‘->’) at the end of the equation followed by the subscripts for the output. For instance, the following equation computes the transpose of a matrix multiplication: ‘ij,jk->ki’. The output subscripts must appear at least once for some input operand and at most once for the output.
Ellipsis (‘…’) can be used in place of subscripts to broadcast the dimensions covered by the ellipsis. Each input operand may contain at most one ellipsis which will cover the dimensions not covered by subscripts, e.g. for an input operand with 5 dimensions, the ellipsis in the equation ‘ab…c’ cover the third and fourth dimensions. The ellipsis does not need to cover the same number of dimensions across the
operands
but the ‘shape’ of the ellipsis (the size of the dimensions covered by them) must broadcast together. If the output is not explicitly defined with the arrow (‘->’) notation, the ellipsis will come first in the output (left-most dimensions), before the subscript labels that appear exactly once for the input operands. e.g. the following equation implements batch matrix multiplication ‘…ij,…jk’.A few final notes: the equation may contain whitespaces between the different elements (subscripts, ellipsis, arrow and comma) but something like ‘…’ is not valid. An empty string ‘’ is valid for scalar operands.
Note
flow.einsum
handles ellipsis (‘…’) differently from NumPy in that it allows dimensions covered by the ellipsis to be summed over, that is, ellipsis are not required to be part of the output.Note
This function does not optimize the given expression, so a different formula for the same computation may run faster or consume less memory. Projects like opt_einsum (https://optimized-einsum.readthedocs.io/en/stable/) can optimize the formula for you.
- Parameters
equation (String) – The subscripts for the Einstein summation.
*operands (oneflow.Tensor) – The tensors to compute the Einstein summation of.
For example:
>>> import oneflow as flow # trace >>> flow.einsum('ii', flow.arange(4*4).reshape(4,4).to(flow.float32)) tensor(30., dtype=oneflow.float32) # diagonal >>> flow.einsum('ii->i', flow.arange(4*4).reshape(4,4).to(flow.float32)) tensor([ 0., 5., 10., 15.], dtype=oneflow.float32) # outer product >>> x = flow.arange(5).to(flow.float32) >>> y = flow.arange(4).to(flow.float32) >>> flow.einsum('i,j->ij', x, y) tensor([[ 0., 0., 0., 0.], [ 0., 1., 2., 3.], [ 0., 2., 4., 6.], [ 0., 3., 6., 9.], [ 0., 4., 8., 12.]], dtype=oneflow.float32) # batch matrix multiplication >>> As = flow.arange(3*2*5).reshape(3,2,5).to(flow.float32) >>> Bs = flow.arange(3*5*4).reshape(3,5,4).to(flow.float32) >>> flow.einsum('bij,bjk->bik', As, Bs).shape oneflow.Size([3, 2, 4]) # batch permute >>> A = flow.randn(2, 3, 4, 5) >>> flow.einsum('...ij->...ji', A).shape oneflow.Size([2, 3, 5, 4]) # bilinear >>> A = flow.randn(3,5,4) >>> l = flow.randn(2,5) >>> r = flow.randn(2,4) >>> flow.einsum('bn,anm,bm->ba', l, A, r).shape oneflow.Size([2, 3])
-
oneflow.
empty
(*size, dtype: Optional[oneflow._oneflow_internal.dtype] = None, device: Optional[Union[oneflow._oneflow_internal.device, str]] = None, placement: Optional[oneflow._oneflow_internal.placement] = None, sbp: Optional[Union[oneflow._oneflow_internal.sbp.sbp, List[oneflow._oneflow_internal.sbp.sbp]]] = None, requires_grad: bool = False, pin_memory: bool = False)¶ Returns a tensor filled with uninitialized data. The shape of the tensor is defined by the variable argument
size
.- Parameters
size (int... or oneflow.Size) – Defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple or oneflow.Size.
dtype (flow.dtype, optional) – The desired data type of returned tensor. Default:
flow.float32
.device (oneflow.device, optional) – The desired device of returned local tensor. If None, uses the current device.
placement (flow.placement, optional) – The desired device of returned global tensor. If None, will construct local tensor.
sbp (flow.sbp or List[flow.sbp], optional) – The desired sbp of returned global tensor.
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
pin_memory (bool, optional) – False.
For example:
>>> import oneflow as flow >>> y = flow.empty(4, 5) # construct local empty tensor >>> y.shape oneflow.Size([4, 5]) >>> y.is_global False >>> placement = flow.placement("cpu", ranks=[0]) >>> y = flow.empty(4, 5, placement=placement, sbp=flow.sbp.broadcast) # construct consistent empty tensor >>> y.is_global True
-
class
oneflow.
enable_grad
¶ Context-manager that enabled gradient calculation.
Enables gradient calculation, if it has been disabled via no_grad.
This context manager is thread local; it will not affect computation in other threads.
Also functions as a decorator. (Make sure to instantiate with parenthesis.)
>>> import oneflow as flow >>> x = flow.ones(2, 3, requires_grad=True) >>> with flow.no_grad(): ... with flow.enable_grad(): ... y = x * x >>> y.requires_grad True >>> @flow.enable_grad() ... def no_grad_func(x): ... return x * x >>> with flow.no_grad(): ... y = no_grad_func(x) >>> y.requires_grad True
-
oneflow.
eq
(input, other) → Tensor¶ 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([ True, True, True, False], dtype=oneflow.bool)
-
oneflow.
equal
()¶ eq(input, other) -> Tensor
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([ True, True, True, False], dtype=oneflow.bool)
-
oneflow.
erf
()¶ Computes the error function of each element. The error function is defined as follows:
\[\operatorname{erf}(x)=\frac{2}{\sqrt{\pi}} \int_{0}^{x} e^{-t^{2}} d t\]- Parameters
x (oneflow.Tensor) – A Tensor
- Returns
The result Tensor
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.tensor(np.array([0, -1., 10.]), dtype=flow.float32) >>> out = flow.erf(x) >>> out.shape oneflow.Size([3]) >>> out.numpy() array([ 0. , -0.8427008, 1. ], dtype=float32) >>> x = flow.tensor(np.array([[0, -1., 10.], [5, 7, 0.8]]), dtype=flow.float32) >>> out = flow.erf(x) >>> out.shape oneflow.Size([2, 3]) >>> out.numpy() array([[ 0. , -0.8427008 , 1. ], [ 1. , 1. , 0.74210095]], dtype=float32) >>> x = flow.tensor(np.array([[0, -1., 10.], [5, 7, 0.8], [2, 3, 4]]), dtype=flow.float32) >>> out = x.erf() >>> out.shape oneflow.Size([3, 3]) >>> out.numpy() array([[ 0. , -0.8427008 , 1. ], [ 1. , 1. , 0.74210095], [ 0.9953223 , 0.9999779 , 1. ]], dtype=float32)
-
oneflow.
erfc
()¶ Computes the complementary error function of each element of input. The complementary error function is defined as follows:
\[\operatorname{erfc}(x)=1-\frac{2}{\sqrt{\pi}} \int_{0}^{x} e^{-t^{2}} d t\]- Parameters
x (oneflow.Tensor) – A Tensor
- Returns
The result Tensor
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.tensor(np.array([0, -1., 10.]), dtype=flow.float32) >>> out = flow.erfc(x) >>> out tensor([1.0000e+00, 1.8427e+00, 2.8026e-45], dtype=oneflow.float32) >>> x = flow.tensor(np.array([[0, -1., 10.], [5, 7, 0.8]]), dtype=flow.float32) >>> out = flow.erfc(x) >>> out tensor([[1.0000e+00, 1.8427e+00, 2.8026e-45], [1.5375e-12, 4.1838e-23, 2.5790e-01]], dtype=oneflow.float32)
-
oneflow.
erfinv
()¶ Computes the inverse error function of
input
. The inverse error function is defined in the range \((-1, 1)\) as:\[\mathrm{erfinv}(\mathrm{erf}(x)) = x\]- Parameters
input (oneflow.Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> input=flow.tensor(np.random.randn(3,3).astype(np.float32)) >>> of_out=flow.erfinv(input) >>> of_out.shape oneflow.Size([3, 3])
-
oneflow.
exp
()¶ This operator computes the exponential of Tensor.
The equation is:
\[out = e^x\]- Parameters
x (oneflow.Tensor) – A Tensor
- Returns
The result Tensor
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = flow.tensor(np.array([1, 2, 3]).astype(np.float32), dtype=flow.float32) >>> y = flow.exp(x) >>> y tensor([ 2.7183, 7.3891, 20.0855], dtype=oneflow.float32)
-
oneflow.
expand
(input, *sizes) → Tensor¶ 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])
-
oneflow.
expm1
()¶ Returns a new tensor with the exponential of the elements minus 1 of
input
.The equation is:
\[y_{i} = e^{x_{i}} - 1\]- Parameters
input (oneflow.Tensor) – A Tensor.
- Returns
The result Tensor
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.tensor(np.array([1, 2, 3]).astype(np.float32)) >>> y = flow.expm1(x) >>> y.shape oneflow.Size([3]) >>> y tensor([ 1.7183, 6.3891, 19.0855], dtype=oneflow.float32) >>> x = flow.tensor(np.array([[[2, 4, 6],[7, 8, 9]],[[10,11,12],[13,14,15]]]).astype(np.float32)) >>> y = flow.expm1(x) >>> print(y.shape) oneflow.Size([2, 2, 3]) >>> print(y.numpy()) [[[6.3890562e+00 5.3598152e+01 4.0242880e+02] [1.0956332e+03 2.9799580e+03 8.1020840e+03]] [[2.2025465e+04 5.9873141e+04 1.6275380e+05] [4.4241238e+05 1.2026032e+06 3.2690165e+06]]]
-
oneflow.
eye
(n, m, *, device=None, requires_grad=False, placement=None, sbp) → Tensor¶ This operator creates a 2-D Tensor with ones on the diagonal and zeros elsewhere.
- Parameters
n (int) – the number of rows.
m (int, optional) – the number of colums with default being n. Defaults to None.
- Keyword Arguments
device (Union[flow.device, str], optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor.
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
placement (oneflow._oneflow_internal.placement, optional) – The placement attribute allows you to specify which physical device the tensor is stored on.
sbp (Union[oneflow._oneflow_internal.sbp.sbp, List[oneflow._oneflow_internal.sbp.sbp]], optional) – When creating a global tensor, specify the SBP of the tensor.
- Returns
The result tensor with ones on the diagonal and zeros elsewhere.
- Return type
For example:
>>> import oneflow as flow >>> out = flow.eye(3, 3) >>> out tensor([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]], dtype=oneflow.float32) >>> out = flow.eye(3, 3, device="cuda") >>> out tensor([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]], device='cuda:0', dtype=oneflow.float32)
-
oneflow.
flatten
()¶ 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.randn(32, 1, 5, 5) >>> output = flow.flatten(input, start_dim=1) >>> output.shape oneflow.Size([32, 25])
-
oneflow.
flip
(input, dims) → Tensor¶ Reverse the order of a n-D tensor along given axis in dims.
Note
flow.flip makes a copy of
input
’s data. This is different from NumPy’s np.flip, which returns a view in constant time. Since copying a tensor’s data is more work than viewing that data, flow.flip is expected to be slower than np.flip.- Parameters
input (Tensor) – the input tensor
dims (a list or tuple) – axis to flip on
For example:
>>> import oneflow as flow >>> import numpy as np >>> np_arr = np.arange(0, 8).reshape((2, 2, 2)).astype(np.float32) >>> input = flow.Tensor(np_arr) >>> input.shape oneflow.Size([2, 2, 2]) >>> out = flow.flip(input, [0, 1]) >>> out tensor([[[6., 7.], [4., 5.]], [[2., 3.], [0., 1.]]], dtype=oneflow.float32)
-
oneflow.
floor
()¶ Returns a new tensor with the arcsine of the elements of
input
.\[\text{out}_{i} = \lfloor \text{input}_{i} \rfloor\]- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor(np.array([-0.5, 1.5, 0, 0.8]), dtype=flow.float32) >>> output = flow.floor(input) >>> output.shape oneflow.Size([4]) >>> output.numpy() array([-1., 1., 0., 0.], dtype=float32) >>> input1 = flow.tensor(np.array([[0.8, 1.0], [-0.6, 2.5]]), dtype=flow.float32) >>> output1 = input1.floor() >>> output1.shape oneflow.Size([2, 2]) >>> output1.numpy() array([[ 0., 1.], [-1., 2.]], dtype=float32)
-
oneflow.
floor_
()¶ In-place version of
oneflow.floor()
-
oneflow.
fmod
(input, other, *, out=None) → Tensor¶ Computes the element-wise remainder of division.
The dividend and divisor may contain both for integer and floating point numbers. The remainder has the same sign as the dividend
input
.Supports broadcasting to a common shape, integer and float inputs.
- Parameters
- Keyword Arguments
out (Tensor, optional) – the output tensor.
Example:
>>> import oneflow as flow >>> flow.fmod(flow.tensor([-3., -2, -1, 1, 2, 3], dtype=flow.float32), 2.) tensor([-1., -0., -1., 1., 0., 1.], dtype=oneflow.float32) >>> flow.fmod(flow.tensor([1, 2, 3, 4, 5.], dtype=flow.float32), 1.5) tensor([1.0000, 0.5000, 0.0000, 1.0000, 0.5000], dtype=oneflow.float32) >>> flow.fmod(flow.tensor([1, 2, 3, 4., -5]), flow.tensor([4, 2, 1, 3., 1])) tensor([1., 0., 0., 1., -0.], dtype=oneflow.float32)
-
oneflow.
from_numpy
()¶ Creates a
Tensor
from anumpy.ndarray
.The returned tensor and ndarray share the same memory. Modifications to the tensor will be reflected in the ndarray and vice versa.
It currently accepts ndarray with dtypes of numpy.float64, numpy.float32, numpy.float16, numpy.int64, numpy.int32, numpy.int8, numpy.uint8.
For example:
>>> import oneflow as flow >>> import numpy as np >>> np_arr = np.arange(6).reshape(2, 3) >>> t = flow.from_numpy(np_arr) >>> t tensor([[0, 1, 2], [3, 4, 5]], dtype=oneflow.int64) >>> np_arr[0, 0] = -1 >>> t tensor([[-1, 1, 2], [ 3, 4, 5]], dtype=oneflow.int64)
-
oneflow.
full
(size: Union[int, Tuple[int, …], oneflow.Size], fill_value: Union[float, int], dtype: Optional[oneflow._oneflow_internal.dtype] = None, device: Optional[Union[str, oneflow._oneflow_internal.device]] = None, placement: Optional[oneflow._oneflow_internal.placement] = None, sbp: Optional[oneflow._oneflow_internal.sbp.sbp] = None, requires_grad: bool = False)¶ Creates a tensor of size size filled with fill_value. The tensor’s dtype is inferred from value.
- Parameters
size (int...) – a list, tuple, or oneflow.Size of integers defining the shape of the output tensor.
fill_value (Scalar) – the value to fill the output tensor with.
dtype (oneflow.dtype, optional) – the desired data type of returned tensor.
device (oneflow.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type
placement (oneflow.placement, optional) – the desired placement of returned global tensor. Default: if None, the returned tensor is local one using the argument device.
sbp (oneflow.sbp.sbp or tuple of oneflow.sbp.sbp, optional) – the desired sbp descriptor of returned global 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 oneflow as flow >>> y = flow.full((5,),5) >>> y tensor([5, 5, 5, 5, 5], dtype=oneflow.int64) >>> y = flow.full((2,3),5.0) # construct local tensor >>> y tensor([[5., 5., 5.], [5., 5., 5.]], dtype=oneflow.float32) >>> placement = flow.placement("cpu", ranks=[0]) >>> y = flow.full((2,3), 5.0, placement=placement, sbp=flow.sbp.broadcast) # construct global tensor >>> y.is_global True
-
oneflow.
gather
(input, dim, index, sparse_grad=False) → Tensor¶ 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.int64)) >>> output.shape oneflow.Size([3, 4, 3, 5])
-
oneflow.
gather_nd
(input, index) → Tensor¶ This operator is a high-dimensional extension of gather, index is a K-dimensional tensor, which is regarded as a index of input Tensor input.
Each element defines a slice of input:
\[output[i_{0},i_{1},...,i_{K-2}] = input[index(i_{0},i_{1},...,i_{K-2})]\]- Parameters
input – The input Tensor.
index – The slice indices.
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor(np.array([[1, 2,3], [4, 5,6],[7,8,9]]), dtype=flow.float) >>> index_1 = flow.tensor(np.array([[0], [2]]), dtype=flow.int) >>> out_1 = flow.gather_nd(input,index_1) >>> print(out_1.shape) oneflow.Size([2, 3]) >>> out_1 tensor([[1., 2., 3.], [7., 8., 9.]], dtype=oneflow.float32) >>> index_2 = flow.tensor(np.array([[0,2], [2,1]]), dtype=flow.int) >>> out_2 = flow.gather_nd(input,index_2) >>> out_2 tensor([3., 8.], dtype=oneflow.float32)
-
oneflow.
gelu
(x: Tensor) → Tensor¶ The equation is:
\[out = 0.5 * x * (1 + tanh(\sqrt{\frac{2}{\pi}} * (x + 0.044715x^{3})))\]For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([-0.5, 0, 0.5]).astype(np.float32) >>> input = flow.tensor(x) >>> out = flow.gelu(input) >>> out tensor([-0.1543, 0.0000, 0.3457], dtype=oneflow.float32)
See
GELU
for more details.
-
oneflow.
get_rng_state
()¶ The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.get_rng_state.html.
Sets the random number generator state.
- Parameters
new_state (oneflow.ByteTensor) – The desired state
-
oneflow.
greater
()¶ Returns the truth value of \(input > other\) element-wise.
- Parameters
input (oneflow.Tensor) – A Tensor
other (oneflow.Tensor) – A Tensor
- Returns
A Tensor with bool type.
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow >>> input1 = flow.tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) >>> input2 = flow.tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) >>> out = flow.gt(input1, input2).shape >>> out oneflow.Size([2, 6, 5, 3])
-
oneflow.
gt
()¶ Returns the truth value of \(input > other\) element-wise.
- Parameters
input (oneflow.Tensor) – A Tensor
other (oneflow.Tensor) – A Tensor
- Returns
A Tensor with bool type.
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow >>> input1 = flow.tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) >>> input2 = flow.tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) >>> out = flow.gt(input1, input2).shape >>> out oneflow.Size([2, 6, 5, 3])
-
oneflow.
hsplit
(input, indices_or_sections) → List of Tensors¶ The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.hsplit.html.
Splits input, a tensor with one or more dimensions, into multiple tensors horizontally according to indices_or_sections. Each split is a view of input.
If input is one dimensional this is equivalent to calling oneflow.tensor_split(input, indices_or_sections, dim=0) (the split dimension is zero), and if input has two or more dimensions it’s equivalent to calling oneflow.tensor_split(input, indices_or_sections, dim=1) (the split dimension is 1), except that if indices_or_sections is an integer it must evenly divide the split dimension or a runtime error will be thrown.
- Parameters
input (Tensor) – the input tensor.
indices_or_sections (int or a list) – See argument in
oneflow.tensor_split()
.
- Returns
the output TensorTuple.
- Return type
oneflow.TensorTuple
For example:
>>> import oneflow as flow >>> input = flow.rand(3,4,5,6) >>> output = flow.hsplit(input,(1,3)) >>> output[0].size() oneflow.Size([3, 1, 5, 6]) >>> output[1].size() oneflow.Size([3, 2, 5, 6]) >>> output[2].size() oneflow.Size([3, 1, 5, 6])
-
oneflow.
in_top_k
(targets, predictions, k) → Tensor¶ Says whether the targets are in the top K predictions.
- Parameters
- Returns
A Tensor of type bool. Computed Precision at k as a bool Tensor.
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> targets1 = flow.tensor(np.array([3, 1]), dtype=flow.int32) >>> predictions1 = flow.tensor(np.array([[0.0, 1.0, 2.0, 3.0], [3.0, 2.0, 1.0, 0.0],]), dtype=flow.float32) >>> out1 = flow.in_top_k(targets1, predictions1, k=1) >>> out1 tensor([ True, False], dtype=oneflow.bool) >>> out2 = flow.in_top_k(targets1, predictions1, k=2) >>> out2 tensor([True, True], dtype=oneflow.bool) >>> targets2 = flow.tensor(np.array([3, 1]), dtype=flow.int32, device=flow.device('cuda')) >>> predictions2 = flow.tensor(np.array([[0.0, 1.0, 2.0, 3.0], [3.0, 2.0, 1.0, 0.0],]), dtype=flow.float32, device=flow.device('cuda')) >>> out3 = flow.in_top_k(targets2, predictions2, k=1) >>> out3 tensor([ True, False], device='cuda:0', dtype=oneflow.bool)
-
oneflow.
index_select
(dim, index) → Tensor¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.index_select.html.
Select values along an axis specified by dim.
index
must be an Int32 Tensor with 1-D.dim
must be in the range of input Dimensions. value ofindex
must be in the range of the dim-th of input. Note thatinput
andindex
do not broadcast against each other.- Parameters
For example:
>>> import oneflow as flow >>> input = flow.tensor([[1,2,3],[4,5,6]], dtype=flow.int32) >>> input tensor([[1, 2, 3], [4, 5, 6]], dtype=oneflow.int32) >>> index = flow.tensor([0,1], dtype=flow.int64) >>> output = flow.index_select(input, 1, index) >>> output tensor([[1, 2], [4, 5]], dtype=oneflow.int32) >>> output = input.index_select(1, index) >>> output tensor([[1, 2], [4, 5]], dtype=oneflow.int32)
-
class
oneflow.
inference_mode
(mode=True)¶ Context-manager that enables or disables inference mode
InferenceMode is a new context manager analogous to no_grad to be used when you arecertain your operations will have no interactions with autograd (e.g., model training). Code run under this mode gets better performance by disabling view tracking and version counter bumps.
This context manager is thread local; it will not affect computation in other threads.
Also functions as a decorator. (Make sure to instantiate with parenthesis.)
- Parameters
mode (bool) – Flag whether to enable or disable inference mode. (default: True)
>>> import oneflow as flow >>> x = flow.ones(2, 3, requires_grad=True) >>> with flow.inference_mode(): ... y = x * x >>> y.requires_grad False >>> @flow.inference_mode() ... def no_grad_func(x): ... return x * x >>> y = no_grad_func(x) >>> y.requires_grad False
-
oneflow.
initial_seed
() → int¶ The documentation is referenced from: https://pytorch.org/docs/1.10/_modules/torch/random.html.
Returns the initial seed for generating random numbers as a Python long.
-
oneflow.
is_floating_point
(input)¶ 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
-
oneflow.
is_grad_enabled
()¶ Returns True if grad mode is currently enabled.
-
oneflow.
is_nonzero
(input) -> (bool)¶ Returns True if the
input
is a single element tensor which is not equal to zero after type conversions. i.e. not equal toflow.tensor([0.])
orflow.tensor([0])
.Throws a
RuntimeError
ifinput.shape.numel() != 1
For Example:
>>> import oneflow as flow >>> flow.is_nonzero(flow.tensor([0.])) False >>> flow.is_nonzero(flow.tensor([1.5])) True >>> flow.is_nonzero(flow.tensor([3])) True
-
oneflow.
is_tensor
(input) -> (bool)¶ Note that this function is simply doing
isinstance(obj, Tensor)
. Using thatisinstance
check is better for typechecking with mypy, and more explicit - so it’s recommended to use that instead ofis_tensor
.- Parameters
obj (Object) – Object to test
For example:
>>> import oneflow as flow >>> x=flow.tensor([1,2,3]) >>> flow.is_tensor(x) True
-
oneflow.
isinf
()¶ Tests if each element of input is infinite (positive or negative infinity) or not.
- Parameters
input (Tensor) – the input tensor.
- Returns
A boolean tensor that is True where input is infinite and False elsewhere.
Example:
>>> import oneflow as flow >>> flow.isinf(flow.tensor([1, float('inf'), 2, float('-inf'), float('nan')])) tensor([False, True, False, True, False], dtype=oneflow.bool)
-
oneflow.
isnan
()¶ Returns a new tensor with boolean elements representing if each element of input is NaN or not.
- Parameters
input (Tensor) – the input tensor.
- Returns
A boolean tensor that is True where input is NaN and False elsewhere.
Example:
>>> import oneflow as flow >>> flow.isnan(flow.tensor([1, float('nan'), 2])) tensor([False, True, False], dtype=oneflow.bool)
-
oneflow.
le
(input, other) → Tensor¶ Returns the truth value of \(input <= other\) element-wise.
- Parameters
input (oneflow.Tensor) – A Tensor
other (oneflow.Tensor) – A Tensor
- Returns
A Tensor with bool 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([ True, False, True], dtype=oneflow.bool)
-
oneflow.
linspace
(start: float, end: float, steps: int, dtype: oneflow._oneflow_internal.dtype = oneflow.float32, device: Optional[Union[oneflow._oneflow_internal.device, str]] = None, placement: Optional[oneflow._oneflow_internal.placement] = None, sbp: Optional[Union[oneflow._oneflow_internal.sbp.sbp, List[oneflow._oneflow_internal.sbp.sbp]]] = None, requires_grad: bool = False)¶ Creates a one-dimensional tensor of size
steps
whose values are evenly spaced fromstart
toend
, inclusive. That is, the value are:\[(\text{start}, \text{start} + \frac{\text{end} - \text{start}}{\text{steps} - 1}, \ldots, \text{start} + (\text{steps} - 2) * \frac{\text{end} - \text{start}}{\text{steps} - 1}, \text{end})\]- Parameters
start (float) – the starting value for the set of points
end (float) – the ending value for the set of points
steps (int) – size of the constructed tensor
- Keyword Arguments
dtype (flow.dtype, optional) – If dtype is not given, the dtype is inferred to be flow.float32.
device (flow.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor.
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
For example:
>>> import oneflow as flow >>> y = flow.linspace(3, 10, steps=5) >>> y tensor([ 3.0000, 4.7500, 6.5000, 8.2500, 10.0000], dtype=oneflow.float32)
-
oneflow.
load
(path: str, global_src_rank: Optional[int] = None) → Any¶ Loads an object saved with oneflow.save() from a directory.
- Parameters
path (str) – The directory containing the object
global_src_rank (int, optional) – The source rank for loading global tensors. When specified, only the process whose rank == global_src_rank will really read the files in path, and tensors in the loaded object will be consistent with placement = flow.placement(‘cuda’, [global_src_rank])
- Returns
The loaded object
-
oneflow.
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)
-
oneflow.
log1p
()¶ Returns a new tensor with the natural logarithm of (1 + input).
\[\text{out}_{i}=\log_e(1+\text{input}_{i})\]For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.tensor(np.array([1.3, 1.5, 2.7]), dtype=flow.float32) >>> out = flow.log1p(x) >>> out tensor([0.8329, 0.9163, 1.3083], dtype=oneflow.float32)
-
oneflow.
log2
(input) → Tensor¶ Returns a new tensor with the natural logarithm to the base 2 of the elements of
input
.\[y_{i} = \log2_{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.log2(input)
-
oneflow.
logical_and
()¶ Computes the element-wise logical AND of the given input tensors. Zeros are treated as False and nonzeros are treated as True.
- Parameters
input (oneflow.Tensor) – The input Tensor
other (oneflow.Tensor) – The Tensor to compute AND with
- Returns
The output Tensor
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow >>> input1 = flow.tensor(np.array([1, 0, 1]).astype(np.float32), dtype=flow.float32) >>> input2 = flow.tensor(np.array([1, 1, 0]).astype(np.float32), dtype=flow.float32) >>> out = flow.logical_and(input1, input2) >>> out tensor([ True, False, False], dtype=oneflow.bool)
-
oneflow.
logical_not
()¶ Computes the element-wise logical NOT of the given input tensors. Zeros are treated as False and nonzeros are treated as True. :param input: The input Tensor :type input: oneflow.Tensor :param other: The Tensor to compute NOT with :type other: oneflow.Tensor
- Returns
The output Tensor
- Return type
For example:
>>> import oneflow as flow >>> input = flow.tensor([1, 0, -1], dtype=flow.float32) >>> out = flow.logical_not(input) >>> out tensor([False, True, False], dtype=oneflow.bool)
-
oneflow.
logical_or
()¶ Computes the element-wise logical OR of the given input tensors. Zeros are treated as False and nonzeros are treated as True.
- Parameters
input (oneflow.Tensor) – The input Tensor
other (oneflow.Tensor) – The Tensor to compute OR with
- Returns
The output Tensor
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow >>> input1 = flow.tensor(np.array([1, 0, 1]).astype(np.float32), dtype=flow.float32) >>> input2 = flow.tensor(np.array([1, 0, 0]).astype(np.float32), dtype=flow.float32) >>> out = flow.logical_or(input1, input2) >>> out tensor([ True, False, True], dtype=oneflow.bool)
-
oneflow.
logical_xor
()¶ Computes the element-wise logical XOR of the given input tensors. Zeros are treated as False and nonzeros are treated as True.
- Parameters
input (oneflow.Tensor) – The input Tensor
other (oneflow.Tensor) – The Tensor to compute XOR with
- Returns
The output Tensor
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow >>> input1 = flow.tensor(np.array([1, 0, 1]).astype(np.float32), dtype=flow.float32) >>> input2 = flow.tensor(np.array([1, 0, 0]).astype(np.float32), dtype=flow.float32) >>> out = flow.logical_xor(input1, input2) >>> out tensor([False, False, True], dtype=oneflow.bool)
-
oneflow.
lt
(input, other) → Tensor¶ Returns the truth value of \(input < other\) element-wise.
- Parameters
input (oneflow.Tensor) – A Tensor
other (oneflow.Tensor) – A Tensor
- Returns
A Tensor with bool 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([False, False, True], dtype=oneflow.bool)
-
oneflow.
manual_seed
(seed)¶ The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.manual_seed.html.
Sets the seed for generating random numbers. Returns a oneflow.Generator object.
- Parameters
seed (int) – The desired seed. Value must be within the inclusive range [-0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff]. Otherwise, a RuntimeError is raised. Negative inputs are remapped to positive values with the formula 0xffff_ffff_ffff_ffff + seed.
-
oneflow.
masked_fill
()¶ 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)
-
oneflow.
masked_select
(input, mask)¶ Returns a new 1-D tensor which indexes the input tensor according to the boolean mask mask which is a BoolTensor(In oneFlow BoolTensor is replaced by Int8Tensor).
The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable.
- Parameters
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor(np.array([[-0.4620, 0.3139], [0.3898, -0.7197], [0.0478, -0.1657]]), dtype=flow.float32) >>> mask = input.gt(0.05) >>> out = flow.masked_select(input, mask) >>> out tensor([0.3139, 0.3898], dtype=oneflow.float32)
-
oneflow.
matmul
(input, other) → Tensor¶ This operator applies matrix multiplication to two Tensor.
- Parameters
a (oneflow.Tensor) – A Tensor
b (oneflow.Tensor) – A Tensor
- Returns
The result Tensor
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> input1 = flow.tensor(np.random.randn(2, 6), dtype=flow.float32) >>> input2 = flow.tensor(np.random.randn(6, 5), dtype=flow.float32) >>> of_out = flow.matmul(input1, input2) >>> of_out.shape oneflow.Size([2, 5])
-
oneflow.
max
(input, dim=None, keepdim=False)¶ Computes the maximum value of all elements in the input tensor.
- Parameters
input (oneflow.Tensor) – the Input Tensor
dim (int, optional) – the dimension to reduce. Default: None
keepdim (bool, optional) – whether the output tensor has dim retained or not. Default: False
- Returns
If
dim
is None, returns the maximum value of all elements in the input tensor. Otherwise, returns a tuple of Tensor (values, indices), where the values are the maximum value of all elements in the input tensor, the indices are the indices of the elements in the original input tensor.- Return type
Tensor or Tuple(oneflow.Tensor, oneflow.Tensor(dtype=int64))
For example:
>>> import oneflow as flow >>> input = flow.Tensor([[4, 1, 5], [2, 6, 3]]) >>> flow.max(input) tensor(6., dtype=oneflow.float32) >>> (values, indices) = flow.max(input, dim=1) >>> values tensor([5., 6.], dtype=oneflow.float32) >>> indices tensor([2, 1], dtype=oneflow.int64)
-
oneflow.
maximum
()¶ Computes the element-wise maximum of x and y.
For example:
>>> import oneflow as flow >>> x = flow.tensor((1, 2, -1), dtype=flow.float32) >>> y = flow.tensor((3, 0, 4), dtype=flow.float32) >>> flow.maximum(x, y) tensor([3., 2., 4.], dtype=oneflow.float32) >>> x = flow.tensor((1,), dtype=flow.float32) >>> y = flow.tensor((3, 0, 4), dtype=flow.float32) >>> flow.maximum(x, y) tensor([3., 1., 4.], dtype=oneflow.float32)
-
oneflow.
mean
(input, dim=None, keepdim=False) → Tensor¶ Computes the mean of row of elements in a tensor in the given dimension. If the dimension is None, mean of all elements will be caculated.
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
oneflow.squeeze()
, resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).- Parameters
input (oneflow.Tensor) – the Input Tensor
dim (int or tuple of ints, optional) – the dimension to reduce. Default: None
keepdim (bool, optional) – whether the output tensor has dim retained or not. Default: False
For example:
>>> import oneflow as flow >>> input = flow.Tensor([[1, 2, 3], [4, 5, 6]]) >>> flow.mean(input) tensor(3.5000, dtype=oneflow.float32) >>> flow.mean(input, dim=0) tensor([2.5000, 3.5000, 4.5000], dtype=oneflow.float32) >>> flow.mean(input, dim=1) tensor([2., 5.], dtype=oneflow.float32)
-
oneflow.
median
(input) → Tensor¶ Returns the median of the values in input. The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.median.html#torch.median
Note
The median is not unique for
input
tensors with an even number of elements. In this case the lower of the two medians is returned.- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> x = flow.tensor((1, 2, -1), dtype=flow.float32) >>> flow.median(x) tensor(1., dtype=oneflow.float32)
-
oneflow.
median
(input, dim=- 1, keepdim=False, *, out=None)
Returns a tuple
(values, indices)
wherevalues
contains the median of each row ofinput
in the dimensiondim
, andindices
contains the index of the median values found in the dimensiondim
.By default,
dim
is the last dimension of theinput
tensor.If
keepdim
isTrue
, the output tensors are of the same size asinput
except in the dimensiondim
where they are of size 1. Otherwise,dim
is squeezed (seeflow.squeeze()
), resulting in the outputs tensor having 1 fewer dimension thaninput
.Note
The median is not unique for
input
tensors with an even number of elements in the dimensiondim
. In this case the lower of the two medians is returned.- Parameters
input (Tensor) – the input tensor.
dim (int) – the dimension to reduce.
keepdim (bool) – whether the output tensor has
dim
retained or not.
For example:
>>> import oneflow as flow >>> a = flow.tensor([[ 0.2505, -0.3982, -0.9948, 0.3518, -1.3131], ... [ 0.3180, -0.6993, 1.0436, 0.0438, 0.2270], ... [-0.2751, 0.7303, 0.2192, 0.3321, 0.2488], ... [ 1.0778, -1.9510, 0.7048, 0.4742, -0.7125]]) >>> flow.median(a, 1) (tensor([-0.3982, 0.2270, 0.2488, 0.4742], dtype=oneflow.float32), tensor([1, 4, 4, 3], dtype=oneflow.int64))
-
oneflow.
meshgrid
(*tensors, indexing='ij')¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/1.10/_modules/torch/functional.html#meshgrid.
Take \(N\) tensors, each of which can be either scalar or 1-dimensional vector, and create \(N\) N-dimensional grids, where the \(i\) th grid is defined by expanding the \(i\) th input over dimensions defined by other inputs.
- Parameters
tensors (list of Tensor) – list of scalars or 1 dimensional tensors. Scalars will be treated as tensors of size \((1,)\) automatically.
indexing ((string, optional) – the indexing mode, either “xy” or “ij”, defaults to “ij”. If “ij” is selected, the dimensions are in the same order as the cardinality of the inputs. If “xy” is selected, the first dimension corresponds to the cardinality of the second input and the second dimension corresponds to the cardinality of the first input.
- Returns
If the input has \(k\) tensors of size \((N_1,), (N_2,), \ldots , (N_k,)\), then the output would also have \(k\) tensors, where all tensors are of size \((N_1, N_2, \ldots , N_k)\).
- Return type
seq (sequence of Tensors)
For example:
>>> import numpy as np >>> import oneflow as flow >>> input1 = flow.tensor(np.array([2, 2, 3]), dtype=flow.float32) >>> input2 = flow.tensor(np.array([4, 5, 6]), dtype=flow.float32) >>> of_x, of_y = flow.meshgrid(input1, input2) >>> of_x tensor([[2., 2., 2.], [2., 2., 2.], [3., 3., 3.]], dtype=oneflow.float32) >>> of_y tensor([[4., 5., 6.], [4., 5., 6.], [4., 5., 6.]], dtype=oneflow.float32)
-
oneflow.
min
(input, dim=None, keepdim=False)¶ Computes the minimum value of all elements in the input tensor.
- Parameters
input (oneflow.Tensor) – the Input Tensor
dim (int, optional) – the dimension to reduce. Default: None
keepdim (bool, optional) – whether the output tensor has dim retained or not. Default: False
- Returns
If
dim
is None, returns the minimum value of all elements in the input tensor. Otherwise, returns a tuple of Tensor (values, indices), where the values are the minimum value of all elements in the input tensor, the indices are the indices of the elements in the original input tensor.- Return type
Tensor or Tuple(oneflow.Tensor, oneflow.Tensor(dtype=int64))
For example:
>>> import oneflow as flow >>> input = flow.Tensor([[4, 1, 5], [2, 6, 3]]) >>> flow.min(input) tensor(1., dtype=oneflow.float32) >>> (values, indices) = flow.min(input, dim=1) >>> values tensor([1., 2.], dtype=oneflow.float32) >>> indices tensor([1, 0], dtype=oneflow.int64)
-
oneflow.
minimum
()¶ Computes the element-wise minimum of x and y.
For example:
>>> import oneflow as flow >>> x = flow.tensor((1, 2, -1), dtype=flow.float32) >>> y = flow.tensor((3, 0, 4), dtype=flow.float32) >>> flow.minimum(x, y) tensor([ 1., 0., -1.], dtype=oneflow.float32) >>> x = flow.tensor((1,), dtype=flow.float32) >>> y = flow.tensor((3, 0, 4), dtype=flow.float32) >>> flow.minimum(x, y) tensor([1., 0., 1.], dtype=oneflow.float32)
-
oneflow.
mish
(x: Tensor) → Tensor¶ Applies the element-wise function:
\[ext{mish}(x) = x * ext{tanh}( ext{softplus}(x))\]For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([1, 2, 3]).astype(np.float32) >>> input = flow.tensor(x) >>> out = flow.mish(input) >>> out tensor([0.8651, 1.9440, 2.9865], dtype=oneflow.float32)
See
Mish
for more details.
-
oneflow.
mm
(input, mat2) → Tensor¶ The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.mm.html.
Performs a matrix multiplication of the matrices
input
andmat2
.If
input
is a \((n \times m)\) tensor,mat2
is a \((m \times p)\) tensor,out
will be a \((n \times p)\) tensor.Note
This function does not broadcast. For broadcasting matrix products, see
oneflow.matmul()
.- Parameters
input (oneflow.Tensor) – the first matrix to be matrix multiplied
mat2 (oneflow.Tensor) – the second matrix to be matrix multiplied
- Returns
The result Tensor
- Return type
For example:
>>> import oneflow as flow >>> mat1 = flow.randn(2, 3) >>> mat2 = flow.randn(3, 3) >>> of_out = flow.mm(mat1, mat2) >>> of_out.shape oneflow.Size([2, 3])
-
oneflow.
movedim
()¶ Moves the dimension(s) of input at the position(s) in source to the position(s) in destination. Other dimensions of input that are not explicitly moved remain in their original order and appear at the positions not specified in destination. The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.movedim.html.
- Parameters
input (Tensor) – the input tensor.
source (int or a list) – Original positions of the dims to move. These must be unique.
destination (int or a list) – Destination positions for each of the original dims. These must also be unique.
- Returns
the output Tensor.
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor(np.random.randn(2, 3, 4, 5), dtype=flow.float32) >>> output = flow.movedim(input, 1, 0) >>> output.shape oneflow.Size([3, 2, 4, 5]) >>> output = flow.movedim(input, (1, 2), (0, 1)) >>> output.shape oneflow.Size([3, 4, 2, 5])
-
oneflow.
mul
()¶ Computes the multiplication of input by other for each element, scalar and broadcast promotation are supported.
The formula is:
\[\text{out}_i = \text{input}_i \times \text{other}_i\]For example:
>>> import numpy as np >>> import oneflow as flow # element-wise multiply >>> input = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> other = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> out = flow.mul(input,other).numpy() >>> out.shape (2, 3) # scalar mutiply >>> input = 5 >>> other = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> out = flow.mul(input,other).numpy() >>> out.shape (2, 3) # broadcast mutiply >>> input = flow.tensor(np.random.randn(1,1), dtype=flow.float32) >>> other = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> out = flow.mul(input,other).numpy() >>> out.shape (2, 3)
-
oneflow.
mv
(input, vec) → Tensor¶ The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.mv.html.
Performs a matrix-vector product of the matrix
input
and the vectorvec
.If
input
is a \((n \times m)\) tensor,vec
is a 1-D tensor of size m,out
will be a 1-D tensor of size n.Note
This function does not broadcast.
- Parameters
input (oneflow.Tensor) – matrix to be matrix multiplied
vec (oneflow.Tensor) – vector to be matrix multiplied
- Returns
the output Tensor
- Return type
For example:
>>> import oneflow as flow >>> mat = flow.randn(2, 3) >>> vec = flow.randn(3) >>> out = flow.mv(mat, vec) >>> out.shape oneflow.Size([2])
-
oneflow.
narrow
(x, dim: int, start: int, length: int) → Tensor¶ Returns a new tensor that is a narrowed version of input tensor. The dimension dim is input from start to start + length.
- Parameters
input – 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 >>> input = flow.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> flow.narrow(input, 0, 0, 2) tensor([[1, 2, 3], [4, 5, 6]], dtype=oneflow.int64) >>> flow.narrow(input, 1, 1, 2) tensor([[2, 3], [5, 6], [8, 9]], dtype=oneflow.int64)
-
oneflow.
ne
(input, other) → Tensor¶ 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([False, False, False, True], dtype=oneflow.bool)
-
oneflow.
neg
()¶ 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)
-
oneflow.
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)
-
oneflow.
new_ones
(x, size=None, dtype=None, device=None, placement=None, sbp=None, requires_grad=False) → Tensor¶ Returns a Tensor of size size filled with 1. By default, the returned Tensor has the same oneflow.dtype and oneflow.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 global 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 global 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)
-
oneflow.
nms
(boxes, scores, iou_threshold: float)¶ Performs non-maximum suppression (NMS) on the boxes according to their intersection-over-union (IoU).
NMS iteratively removes lower scoring boxes which have an IoU greater than iou_threshold with another (higher scoring) box.
- Parameters
- Returns
int64 tensor with the indices of the elements that have been kept by NMS, sorted in decreasing order of scores
- Return type
-
class
oneflow.
no_grad
¶ Context-manager that disabled gradient calculation.
Disabling gradient calculation is useful for inference, when you are sure that you will not call Tensor.backward(). It will reduce memory consumption for computations that would otherwise have requires_grad=True.
In this mode, the result of every computation will have requires_grad=False, even when the inputs have requires_grad=True.
This context manager is thread local; it will not affect computation in other threads.
Also functions as a decorator. (Make sure to instantiate with parenthesis.)
>>> import oneflow as flow >>> x = flow.ones(2, 3, requires_grad=True) >>> with flow.no_grad(): ... y = x * x >>> y.requires_grad False >>> @flow.no_grad() ... def no_grad_func(x): ... return x * x >>> y = no_grad_func(x) >>> y.requires_grad False
-
oneflow.
nonzero
(input, *, out=None, as_tuple=False) → Tensor or tuple of Tensors¶ Note
When
as_tuple
isFalse
(default): returns a 2-D tensor where each row is the index for a nonzero value.When
as_tuple
isTrue
: returns a tuple of 1-D index tensors, allowing for advanced indexing, sox[x.nonzero(as_tuple=True)]
gives all nonzero values of tensorx
. Of the returned tuple, each index tensor contains nonzero indices for a certain dimension.See below for more details on the two behaviors.
When
as_tuple
isFalse
(default):Returns a tensor containing the indices of all non-zero elements of
input
. Each row in the result contains the indices of a non-zero element ininput
. The result is sorted lexicographically, with the last index changing the fastest (C-style).If
input
has \(n\) dimensions, then the resulting indices tensorout
is of size \((z \times n)\), where \(z\) is the total number of non-zero elements in theinput
tensor.When
as_tuple
isTrue
:Returns a tuple of 1-D tensors, one for each dimension in
input
, each containing the indices (in that dimension) of all non-zero elements ofinput
.If
input
has \(n\) dimensions, then the resulting tuple contains \(n\) tensors of size \(z\), where \(z\) is the total number of non-zero elements in theinput
tensor.As a special case, when
input
has zero dimensions and a nonzero scalar value, it is treated as a one-dimensional tensor with one element.- Parameters
input (Tensor) – the input tensor.
- Keyword Arguments
out (Tensor, optional) – the output tensor containing indices
- Returns
If
as_tuple
isFalse
, the output tensor containing indices. Ifas_tuple
isTrue
, one 1-D tensor for each dimension, containing the indices of each nonzero element along that dimension.- Return type
Tensor or tuple of Tensors
Example:
>>> import oneflow as flow >>> flow.nonzero(flow.tensor([1, 1, 1, 0, 1])) tensor([[0], [1], [2], [4]], dtype=oneflow.int64) >>> flow.nonzero(flow.tensor([[0.6, 0.0, 0.0, 0.0], ... [0.0, 0.4, 0.0, 0.0], ... [0.0, 0.0, 1.2, 0.0], ... [0.0, 0.0, 0.0,-0.4]])) tensor([[0, 0], [1, 1], [2, 2], [3, 3]], dtype=oneflow.int64) >>> flow.nonzero(flow.tensor([1, 1, 1, 0, 1]), as_tuple=True) (tensor([0, 1, 2, 4], dtype=oneflow.int64),) >>> flow.nonzero(flow.tensor([[0.6, 0.0, 0.0, 0.0], ... [0.0, 0.4, 0.0, 0.0], ... [0.0, 0.0, 1.2, 0.0], ... [0.0, 0.0, 0.0,-0.4]]), as_tuple=True) (tensor([0, 1, 2, 3], dtype=oneflow.int64), tensor([0, 1, 2, 3], dtype=oneflow.int64)) >>> flow.nonzero(flow.tensor(5), as_tuple=True) (tensor([0], dtype=oneflow.int64),)
-
oneflow.
normal
(mean, std, size, *, out=None, placement=None, sbp=None, generator=None, dtype=None, device=None, requires_grad=False) → Tensor¶ Returns a tensor of random numbers drawn from separate normal distributions whose mean and standard deviation are given.
- Parameters
mean (float) – the mean for all distributions
std (float) – the standard deviation for all distributions
size (int...) – a sequence of integers defining the shape of the output tensor.
- Keyword Arguments
out (Tensor, optional) – the output tensor.
placement (flow.placement, optional) – The desired device of returned global tensor. If None, will construct local tensor.
sbp (flow.sbp, optional) – The desired sbp of returned global tensor. It must be equal with the numbers of placement.
generator (
oneflow.Generator
, optional) – a pseudorandom number generator for samplingdtype (
oneflow.dtype
, optional) – the desired data type of returned tensor. Default: oneflow.float32.device – the desired device of returned tensor. Default: cpu.
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
Example:
>>> import oneflow as flow >>> generator = flow.Generator() >>> generator.manual_seed(0) >>> y = flow.normal(0, 1, 5, generator=generator) >>> y tensor([2.2122, 1.1631, 0.7740, 0.4838, 1.0434], dtype=oneflow.float32)
-
oneflow.
numel
(input) → int¶ Returns the total number of elements in the
input
tensor.- Parameters
input (oneflow.Tensor) – Input Tensor
>>> import oneflow as flow >>> a = flow.randn(1, 2, 3, 4, 5) >>> flow.numel(a) 120 >>> a = flow.zeros(4,4) >>> flow.numel(a) 16
-
oneflow.
ones
(*size: Union[int, Tuple[int, …], oneflow.Size, List[int]], dtype: Optional[oneflow._oneflow_internal.dtype] = None, device: Optional[Union[str, oneflow._oneflow_internal.device]] = None, placement: Optional[oneflow._oneflow_internal.placement] = None, sbp: Optional[oneflow._oneflow_internal.sbp.sbp] = None, requires_grad: bool = False)¶ Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size.
- Parameters
size (an integer or tuple of integer values) – defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
dtype (flow.dtype, optional) – the desired data type of returned tensor.
device (flow.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type
placement (flow.placement, optional) – the desired placement of returned global 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 global 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 oneflow as flow >>> y = flow.ones(5) >>> y tensor([1., 1., 1., 1., 1.], dtype=oneflow.float32) >>> y = flow.ones(2,3) # construct local tensor >>> y tensor([[1., 1., 1.], [1., 1., 1.]], dtype=oneflow.float32) >>> placement = flow.placement("cpu", ranks=[0]) >>> y = flow.ones(4, 5, placement=placement, sbp=flow.sbp.broadcast) # construct global tensor >>> y.is_global True
-
oneflow.
ones_like
(input) is equivalent to flow.ones(input.shape, dtype=input.dtype)¶ - Parameters
other (Tensor) – The size of input will determine size of the output tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.tensor(np.random.rand(5), dtype=flow.float32) >>> y = flow.ones_like(x) >>> y tensor([1., 1., 1., 1., 1.], dtype=oneflow.float32)
-
oneflow.
permute
(input, *dims) → Tensor¶ Returns a view of the original tensor with its dimensions permuted.
- Parameters
dims (tuple of ints) – 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) >>> output = flow.permute(input, (1, 0, 2, 3)).shape >>> output oneflow.Size([6, 2, 5, 3])
-
oneflow.
pow
()¶ - Takes the power of each element in input with exponent and returns a tensor with the result. Exponent can be either a single float number, a single int number, or a tensor with the same shape as input.
When exponent is a scalar value, the operation applied is:
\[\begin{split}\\text{out}_i = x_i ^ \\text{exponent}\end{split}\]- u200b
When exponent is a tensor, the operation applied is:
\[\begin{split}\\text{out}_i = x_i ^ {\\text{exponent}_i}\end{split}\]- Args:
input (Tensor): the input tensor.
exponent (int, float, Tensor): the exponent.
- Returns:
Tensor: The result of variance on the specified axis of input Tensor
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.tensor(np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]), dtype=flow.float32) >>> out = flow.pow(x, 2) >>> out tensor([ 1., 4., 9., 16., 25., 36.], dtype=oneflow.float32) >>> x = flow.tensor(np.array([1.0, 2.0, 3.0, 4.0]), dtype=flow.float32) >>> y = flow.tensor(np.array([1.0, 2.0, 3.0, 4.0]), dtype=flow.float32) >>> out = flow.pow(x, y) >>> out tensor([ 1., 4., 27., 256.], dtype=oneflow.float32)
-
oneflow.
prod
(input, dim=None, keepdim=False) → Tensor¶ Computes the product of row of elements in a tensor in the given dimension. If the dimension is None, product of all elements will be caculated.
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
oneflow.squeeze()
, resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).- Parameters
input (oneflow.Tensor) – the Input Tensor
dim (int or tuple of ints, optional) – the dimension to reduce. Default: None
keepdim (bool, optional) – whether the output tensor has dim retained or not. Default: False
For example:
>>> import oneflow as flow >>> input = flow.Tensor([[1, 2, 3], [4, 5, 6]]) >>> flow.prod(input) tensor(720., dtype=oneflow.float32) >>> flow.prod(input, dim=0) tensor([ 4., 10., 18.], dtype=oneflow.float32) >>> flow.prod(input, dim=1) tensor([ 6., 120.], dtype=oneflow.float32)
-
oneflow.
rand
(*size, *, dtype=None, generator=None, device=None, placement=None, sbp=None, requires_grad=False) → Tensor¶ Returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1)
The shape of the tensor is defined by the variable argument
size
.- Parameters
size (int... or oneflow.Size) – Defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple or oneflow.Size.
dtype (flow.dtype, optional) – The desired data type of returned tensor. Default:
flow.float32
.generator (flow.Generator, optional) – a pseudorandom number generator for sampling
device (flow.device, optional) – The desired device of returned local tensor. If None, uses the current device.
placement (flow.placement, optional) – The desired device of returned global tensor. If None, will construct local tensor.
sbp (flow.sbp, optional) – The desired sbp of returned global tensor. It must be equal with the numbers of placement.
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
For example:
>>> import oneflow as flow >>> x = flow.rand(3,3) # construct local tensor >>> x.shape oneflow.Size([3, 3]) >>> x.is_global False >>> placement = flow.placement("cpu", ranks=[0]) >>> sbp = flow.sbp.broadcast >>> x = flow.rand(3, 3, placement=placement, sbp=sbp) # construct global tensor >>> x.is_global True
-
oneflow.
randint
(low=0, high, size, *, dtype=None, generator=None, device=None, placement=None, sbp=None, requires_grad=False) → Tensor¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.randint.html.
Returns a tensor filled with random integers generated uniformly between low (inclusive) and high (exclusive).
The shape of the tensor is defined by the variable argument
size
.- Parameters
low (int, optional) – Lowest integer to be drawn from the distribution. Default: 0.
high (int) – One above the highest integer to be drawn from the distribution.
size (tuple or oneflow.Size) – Defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple or oneflow.Size.
- Keyword Arguments
dtype (oneflow.dtype, optional) – The desired data type of returned tensor. Default:
flow.int64
.generator (oneflow.Generator, optional) –
device (oneflow.device, optional) – The desired device of returned local tensor. If None, uses the current device.
placement (oneflow.placement, optional) – The desired device of returned global tensor. If None, will construct local tensor.
sbp (oneflow.sbp, optional) – The desired sbp of returned global tensor. It must be equal with the numbers of placement.
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
For example:
>>> import oneflow as flow >>> generator = flow.Generator() >>> generator.manual_seed(0) >>> y = flow.randint(0, 5, (3,3), generator=generator) # construct local tensor >>> y tensor([[2, 2, 3], [4, 3, 4], [2, 4, 2]], dtype=oneflow.int64) >>> y.is_global False >>> placement = flow.placement("cpu", ranks=[0]) >>> y = flow.randint(0, 5, (3,3), generator=generator, placement=placement, sbp=flow.sbp.broadcast) # construct global tensor >>> y.is_global True
-
oneflow.
randn
(*size, *, dtype=None, generator=None, device=None, placement=None, sbp=None, requires_grad=False) → Tensor¶ Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution).
The shape of the tensor is defined by the variable argument
size
.- Parameters
size (int... or oneflow.Size) – Defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple or oneflow.Size.
dtype (flow.dtype, optional) – The desired data type of returned tensor. Default:
flow.float32
.generator (flow.Generator, optional) – a pseudorandom number generator for sampling
device (flow.device, optional) – The desired device of returned local tensor. If None, uses the current device.
placement (flow.placement, optional) – The desired device of returned global tensor. If None, will construct local tensor.
sbp (flow.sbp, optional) – The desired sbp of returned global tensor. It must be equal with the numbers of placement.
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
For example:
>>> import oneflow as flow >>> x = flow.randn(3,3) # construct local tensor >>> x.shape oneflow.Size([3, 3]) >>> x.is_global False >>> placement = flow.placement("cpu", ranks=[0]) >>> sbp = flow.sbp.broadcast >>> x = flow.randn(3,3,placement=placement,sbp=sbp) # construct global tensor >>> x.is_global True
-
oneflow.
randperm
(n, *, generator=None, dtype=torch.int64, device=None, placement=None, sbp=None, requires_grad=False) → Tensor¶ Returns a random permutation of integers from
0
ton - 1
.- Parameters
n (int) – the upper bound (exclusive)
- Keyword Arguments
generator (
oneflow.Generator
, optional) – a pseudorandom number generator for samplingdtype (
oneflow.dtype
, optional) – the desired data type of returned tensor. Default:oneflow.int64
.device – the desired device of returned tensor. Default: cpu.
placement – (
flow.placement
, optional): The desired device of returned global tensor. If None, will construct local tensor.sbp – (
flow.sbp
, optional): The desired sbp of returned global tensor. It must be equal with the numbers of placement.requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
Example:
>>> import oneflow as flow >>> generator = flow.Generator() >>> generator.manual_seed(0) >>> y = flow.randperm(5, generator=generator) # construct local tensor >>> y tensor([2, 4, 3, 0, 1], dtype=oneflow.int64) >>> y.is_global False >>> placement = flow.placement("cpu", ranks=[0]) >>> y = flow.randperm(5, generator=generator, placement=placement, sbp=flow.sbp.broadcast) # construct global tensor >>> y.is_global True
-
oneflow.
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]]), dtype=flow.float32) >>> out = flow.reciprocal(x) >>> out.numpy() array([[1. , 0.5 , 0.33333334], [0.25 , 0.2 , 0.16666667]], dtype=float32)
-
oneflow.
repeat
(input, sizes) → Tensor¶ This operator repeat the input tensor to a larger size along the specified dimensions.
- Parameters
input (oneflow.Tensor) – the input Tensor.
sizes (flow.Shape or List) – 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 >>> np_arr = np.random.randn(5, 3, 6, 9).astype(np.float32) >>> input = flow.Tensor(np_arr) >>> out = input.repeat(1, 1, 2, 2) >>> out.shape oneflow.Size([5, 3, 12, 18]) >>> out = input.repeat(2, 1, 1, 2, 2) >>> out.shape oneflow.Size([2, 5, 3, 12, 18])
-
oneflow.
repeat_interleave
(input, repeats, dim=None, *, output_size=None) → Tensor¶ The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.repeat_interleave.html
Repeat elements of a tensor.
Warning
This is different from
oneflow.Tensor.repeat()
but similar tonumpy.repeat
.- Parameters
input (oneflow.Tensor) – the input Tensor.
repeats (Tensor or int) – The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.
dim (int, optional) – The dimension along which to repeat values. By default, use the flattened input array, and return a flat output array.
- Keyword Arguments
output_size (int, optional) – Total output size for the given axis ( e.g. sum of repeats). If given, it will avoid stream syncronization needed to calculate output shape of the tensor.
- Returns
Repeated tensor which has the same shape as input, except along the given axis.
- Return type
For example:
>>> import oneflow as flow >>> x = flow.tensor([1, 2, 3]) >>> y = flow.tensor([[1, 2], [3, 4]]) >>> flow.repeat_interleave(y, 2) tensor([1, 1, 2, 2, 3, 3, 4, 4], dtype=oneflow.int64) >>> flow.repeat_interleave(y, 3, dim=1) tensor([[1, 1, 1, 2, 2, 2], [3, 3, 3, 4, 4, 4]], dtype=oneflow.int64) >>> flow.repeat_interleave(y, flow.tensor([1, 2]), dim=0) tensor([[1, 2], [3, 4], [3, 4]], dtype=oneflow.int64) >>> flow.repeat_interleave(y, flow.tensor([1, 2]), dim=0, output_size=3) tensor([[1, 2], [3, 4], [3, 4]], dtype=oneflow.int64)
-
oneflow.
reshape
(input, shape: Optional[Sequence[int]] = None)¶ 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 – Shape of the output tensor.
- 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 = flow.reshape(input, shape=[2, 2, 2, -1]).shape >>> y oneflow.Size([2, 2, 2, 2])
-
oneflow.
roc_auc_score
()¶
-
oneflow.
roll
(input, shifts, dims=None)¶ Roll the tensor along the given dimension(s).
Elements that are shifted beyond the last position are re-introduced at the first position.
If a dimension is not specified, the tensor will be flattened before rolling and then restored to the original shape.
- Parameters
input (oneflow.Tensor) – the input Tensor.
shifts (int or tuple of ints) – The number of places by which the elements of the tensor are shifted. If shifts is a tuple, dims must be a tuple of the same size, and each dimension will be rolled by the corresponding value.
dims (int or tuple of ints) – Axis along which to roll.
- Returns
The result Tensor.
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = np.array([[1, 2], ... [3, 4], ... [5, 6], ... [7, 8]]) >>> input = flow.Tensor(x) >>> input.shape oneflow.Size([4, 2]) >>> out = flow.roll(input, 1, 0) >>> out tensor([[7., 8.], [1., 2.], [3., 4.], [5., 6.]], dtype=oneflow.float32) >>> input.roll(-1, 1) tensor([[2., 1.], [4., 3.], [6., 5.], [8., 7.]], dtype=oneflow.float32)
-
oneflow.
round
()¶ This operator rounds the value of Blob to the nearest integer. :param input: A Tensor :type input: oneflow.Tensor
- Returns
The result Tensor
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> x1 = flow.tensor(np.array([1.49999, 1.500001, 2.7]).astype(np.float32)) >>> out1 = flow.round(x1) >>> out1.numpy() array([1., 2., 3.], dtype=float32) >>> x2 = flow.tensor(np.array([2.499999, 7.5000001, 5.3, 6.8]).astype(np.float32)) >>> out2 = flow.round(x2) >>> out2.numpy() array([2., 8., 5., 7.], dtype=float32)
-
oneflow.
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]), dtype=flow.float32) >>> out = flow.rsqrt(a).numpy() >>> out array([1. , 0.70710677, 0.57735026], dtype=float32)
-
oneflow.
save
(obj: Any, path: Union[str, pathlib.Path], global_dst_rank: Optional[int] = None) → None¶ Save an object to a directory.
- Parameters
obj – The object to be saved
path (str) – The directory in which the object is saved
global_dst_rank (int, optional) – The destination rank for saving global tensors. When specified, whole tensors will be saved by the process whose rank == global_src_rank, while other processes will not do any disk I/O.
-
oneflow.
scatter
(input, dim, index, src)¶ This operator writes the elements specified by index along with the axis dim from the src into the input.
Take a 3-D blob as example, the output is specified by:
input[index[i][j][k]][j][k] = src[i][j][k] # if dim == 0 input[i][index[i][j][k]][k] = src[i][j][k] # if dim == 1 input[i][j][index[i][j][k]] = src[i][j][k] # if dim == 2
input, index and src (if it is a Tensor) should all have the same number of dimensions. It is also required that index.shape(d) <= src.shape(d) for all dimensions d, and that index.shape(d) <= self.shape(d) for all dimensions d != dim. Note that index and src do not broadcast.
- Parameters
- Returns
The scatterd Tensor.
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.ones((3,5))*2 >>> index = flow.tensor(np.array([[0,1,2],[0,1,4]], ), dtype=flow.int32) >>> src = flow.Tensor(np.array([[0,10,20,30,40],[50,60,70,80,90]])) >>> out = flow.scatter(input, 1, index, src) >>> out tensor([[ 0., 10., 20., 2., 2.], [50., 60., 2., 2., 70.], [ 2., 2., 2., 2., 2.]], dtype=oneflow.float32)
-
oneflow.
scatter_add
(input, dim, index, src)¶ This operator scatter the src with addition operation according to index along dim into the input.
Take a 3-D blob as example, the output is specified by:
input[index[i][j][k]][j][k] += src[i][j][k] # if dim == 0 input[i][index[i][j][k]][k] += src[i][j][k] # if dim == 1 input[i][j][index[i][j][k]] += src[i][j][k] # if dim == 2
- Parameters
- Returns
The scatterd Tensor.
- Return type
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.ones((3,5))*2 >>> index = flow.tensor(np.array([[0,1,2],[0,1,4]], ), dtype=flow.int32) >>> src = flow.Tensor(np.array([[0,10,20,30,40],[50,60,70,80,90]])) >>> out = flow.scatter_add(input, 1, index, src) >>> out tensor([[ 2., 12., 22., 2., 2.], [52., 62., 2., 2., 72.], [ 2., 2., 2., 2., 2.]], dtype=oneflow.float32)
-
oneflow.
scatter_nd
(index, update, shape)¶ This operator inserts the elements in update according to the index and create a new Tensor.
- Parameters
index – The indices of update. Its type should be flow.int.
update – The update Tensor.
shape (Sequence[int]) – The constant tensor shape, the constant tensor elements are all zero.
For example:
>>> import oneflow as flow >>> import numpy as np >>> index = flow.tensor(np.array([[1], [6], [4]]), dtype=flow.int) >>> update = flow.tensor(np.array([10.2, 5.1, 12.7]), dtype=flow.float) >>> out = flow.scatter_nd(index, update, [8]) >>> out tensor([ 0.0000, 10.2000, 0.0000, 0.0000, 12.7000, 0.0000, 5.1000, 0.0000], dtype=oneflow.float32)
-
oneflow.
searchsorted
() → oneflow.Tensor¶ The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.searchsorted.html?highlight=searchsorted
Find the indices from the innermost dimension of sorted_sequence such that, if the corresponding values in values were inserted before the indices, the order of the corresponding innermost dimension within sorted_sequence would be preserved. Return a new tensor with the same size as values. If right is False (default), then the left boundary of sorted_sequence is closed. More formally, the returned index satisfies the following rules:
sorted_sequence
right
returned index satisfies
1-D
False
sorted_sequence[i-1] < values[m][n]…[l][x] <= sorted_sequence[i]
1-D
True
sorted_sequence[i-1] <= values[m][n]…[l][x] < sorted_sequence[i]
N-D
False
- sorted_sequence[m][n]…[l][i-1] < values[m][n]…[l][x]
<= sorted_sequence[m][n]…[l][i]
N-D
True
- sorted_sequence[m][n]…[l][i-1] <= values[m][n]…[l][x]
sorted_sequence[m][n]…[l][i]
- Parameters
sorted_sequence (Tensor) – N-D or 1-D tensor, containing monotonically increasing sequence on the innermost dimension.
values (Tensor or Scalar) – N-D tensor or a Scalar containing the search value(s).
out_int32 (bool optional) – indicate the output data type. torch.int32 if True, torch.int64 otherwise. Default value is False, i.e. default output data type is torch.int64.
right (bool optional) – if False, return the first suitable location that is found. If True, return the last such index. If no suitable index found, return 0 for non-numerical value (eg. nan, inf) or the size of innermost dimension within sorted_sequence (one pass the last index of the innermost dimension). In other words, if False, gets the lower bound index for each value in values on the corresponding innermost dimension of the sorted_sequence. If True, gets the upper bound index instead. Default value is False.
For example:
>>> import oneflow as flow >>> sorted_sequence = flow.tensor([[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]) >>> sorted_sequence tensor([[ 1, 3, 5, 7, 9], [ 2, 4, 6, 8, 10]], dtype=oneflow.int64) >>> values = flow.tensor([[3, 6, 9], [3, 6, 9]]) >>> values tensor([[3, 6, 9], [3, 6, 9]], dtype=oneflow.int64) >>> flow.searchsorted(sorted_sequence, values) tensor([[1, 3, 4], [1, 2, 4]], dtype=oneflow.int64) >>> flow.searchsorted(sorted_sequence, values, right=True) tensor([[2, 3, 5], [1, 3, 4]], dtype=oneflow.int64) >>> sorted_sequence_1d = flow.tensor([1, 3, 5, 7, 9]) >>> sorted_sequence_1d tensor([1, 3, 5, 7, 9], dtype=oneflow.int64) >>> flow.searchsorted(sorted_sequence_1d, values) tensor([[1, 3, 4], [1, 3, 4]], dtype=oneflow.int64)
-
oneflow.
seed
() → int¶ The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.seed.html.
Sets the seed for generating random numbers to a non-deterministic random number. Returns a 64 bit number used to seed the RNG.
-
oneflow.
select
()¶ Slices the self tensor along the selected dimension at the given index. This function returns a view of the original tensor with the given dimension removed.
- Parameters
input (Tensor) – the input tensor.
dim (int) – the dimension to slice.
select (int) – the index to select with.
- Returns
the output Tensor.
- Return type
For example:
>>> import oneflow as flow >>> input = flow.rand(3, 4, 5) >>> out = flow.select(input, 0, 1) >>> out.size() oneflow.Size([4, 5]) >>> out = flow.select(input, 1, 1) >>> out.size() oneflow.Size([3, 5])
-
oneflow.
selu
(x: Tensor) → Tensor¶ Applies element-wise function :math:` ext{SELU}(x) = scale * (max(0,x) + min(0, lpha * (exp(x) - 1)))`, with \(lpha=1.6732632423543772848170429916717\) and \(scale=1.0507009873554804934193349852946\).
See
SELU
for more details.For examples:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([1, 2, 3]).astype(np.float32) >>> input = flow.tensor(x) >>> out = flow.nn.functional.selu(input) >>> out tensor([1.0507, 2.1014, 3.1521], dtype=oneflow.float32)
-
class
oneflow.
set_grad_enabled
(is_train=True)¶ Context-manager that enabled gradient calculation.
Enables gradient calculation, if it has been disabled via no_grad.
This context manager is thread local; it will not affect computation in other threads.
Also functions as a decorator. (Make sure to instantiate with parenthesis.)
- Parameters
mode (bool) – Flag whether to enable or disable gradient calculation. (default: True)
>>> import oneflow as flow >>> x = flow.ones(2, 3, requires_grad=True) >>> with flow.set_grad_enabled(True): ... y = x * x >>> y.requires_grad True >>> @flow.set_grad_enabled(False) ... def no_grad_func(x): ... return x * x >>> y = no_grad_func(x) >>> y.requires_grad False
-
oneflow.
set_printoptions
(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None, sci_mode=None)¶ Set options for printing. Items shamelessly taken from NumPy
- Parameters
precision – Number of digits of precision for floating point output (default = 4).
threshold – Total number of array elements which trigger summarization rather than full repr (default = 1000).
edgeitems – Number of array items in summary at beginning and end of each dimension (default = 3).
linewidth – The number of characters per line for the purpose of inserting line breaks (default = terminal_columns).
profile – Sane defaults for pretty printing. Can override with any of the above options. (any one of default, short, full)
sci_mode – Enable (True) or disable (False) scientific notation. If None (default) is specified, the value is defined by oneflow._tensor_str._Formatter. This value is automatically chosen by the framework.
Note
linewidth equals to terminal columns, manual setting will invalidate the default automatic setting.
-
oneflow.
set_rng_state
(state)¶ The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.set_rng_state.html.
Returns the random number generator state as a oneflow.ByteTensor.
-
oneflow.
sigmoid
(input) → Tensor¶ Applies the element-wise function \(\text{Sigmoid}(x) = \frac{1}{1 + \exp(-x)}\)
See
Sigmoid
for more details.For examples:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([0.81733328, 0.43621480, 0.10351428]) >>> input = flow.tensor(x, dtype=flow.float32) >>> out = flow.nn.functional.sigmoid(input) >>> out tensor([0.6937, 0.6074, 0.5259], dtype=oneflow.float32)
-
oneflow.
sign
()¶ Computes the sign of Tensor.
\[\text{out}_{i} = \text{sgn}(\text{input}_{i})\]- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> x1 = flow.tensor(np.array([-2, 0, 2]).astype(np.float32)) >>> out1 = flow.sign(x1) >>> out1.numpy() array([-1., 0., 1.], dtype=float32) >>> x2 = flow.tensor(np.array([-3.2, -4.5, 5.8]).astype(np.float32),device=flow.device('cuda')) >>> out2 = flow.sign(x2) >>> out2.numpy() array([-1., -1., 1.], dtype=float32)
-
oneflow.
silu
(x: Tensor) → Tensor¶ The formula is:
\[ext{silu}(x) = x * sigmoid(x)\]For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([1, 2, 3]).astype(np.float32) >>> input = flow.tensor(x) >>> out = flow.silu(input) >>> out tensor([0.7311, 1.7616, 2.8577], dtype=oneflow.float32)
See
SiLU
for more details.
-
oneflow.
sin
()¶ Returns a new tensor with the sine of the elements of
input
.sin(x: Tensor) -> Tensor
\[\text{y}_{i} = \sin(\text{x}_{i})\]- Parameters
x (Tensor) – the input tensor.
For example: .. code-block:: python
>>> import oneflow as flow >>> import numpy as np >>> x1 = flow.tensor(np.array([-0.5461, 0.1347, -2.7266, -0.2746]).astype(np.float32)) >>> y1 = flow.sin(x1) >>> y1 tensor([-0.5194, 0.1343, -0.4032, -0.2712], dtype=oneflow.float32)
>>> x2 = flow.tensor(np.array([-1.4, 2.6, 3.7]).astype(np.float32), device=flow.device('cuda')) >>> y2 = flow.sin(x2) >>> y2 tensor([-0.9854, 0.5155, -0.5298], device='cuda:0', dtype=oneflow.float32)
-
oneflow.
sin_
()¶ In-place version of
oneflow.sin()
-
oneflow.
sinh
()¶ Returns a new tensor with the hyperbolic sine of the elements of
input
.\[\text{out}_{i} = \sinh(\text{input}_{i})\]- Parameters
input (Tensor) – the input tensor.
For example:
>>> import numpy as np >>> import oneflow as flow >>> x1 = flow.tensor(np.array([1, 2, 3]), dtype=flow.float32) >>> x2 = flow.tensor(np.array([1.53123589,0.54242598,0.15117185]), dtype=flow.float32) >>> x3 = flow.tensor(np.array([1,0,-1]), dtype=flow.float32) >>> flow.sinh(x1).numpy() array([ 1.1752012, 3.6268604, 10.017875 ], dtype=float32) >>> flow.sinh(x2).numpy() array([2.20381 , 0.5694193, 0.1517483], dtype=float32) >>> flow.sinh(x3).numpy() array([ 1.1752012, 0. , -1.1752012], dtype=float32)
-
oneflow.
slice
(input, slice_tup_list: Sequence[Tuple[int, int, int]])¶ Extracts a slice from a tensor. The slice_tup_list assigns the slice indices in each dimension, the format is (start, stop, step). The operator will slice the tensor according to the slice_tup_list.
- Parameters
input – A Tensor.
slice_tup_list – A list of slice tuple, indicate each dimension slice (start, stop, step).
For example:
>>> import numpy as np >>> import oneflow as flow >>> input = flow.Tensor(np.random.randn(3, 6, 9).astype(np.float32)) >>> tup_list = [[None, None, None], [0, 5, 2], [0, 6, 3]] >>> y = flow.slice(input, slice_tup_list=tup_list) >>> y.shape oneflow.Size([3, 3, 2])
-
oneflow.
slice_update
(input, update, slice_tup_list: Sequence[Tuple[int, int, int]])¶ Update a slice of tensor x. Like x[start:stop:step] = update.
- Parameters
x – A Tensor, whose slice will be updated.
update – A Tensor, indicate the update content.
slice_tup_list – A list of slice tuple, indicate each dimension slice (start, stop, step).
For example:
>>> import numpy as np >>> import oneflow as flow >>> input = flow.Tensor(np.array([1, 1, 1, 1, 1]).astype(np.float32)) >>> update = flow.Tensor(np.array([2, 3, 4]).astype(np.float32)) >>> flow.slice_update(input, update, slice_tup_list=[[1, 4, 1]]) tensor([1., 2., 3., 4., 1.], dtype=oneflow.float32)
-
oneflow.
softmax
(x: Tensor, dim: int) → Tensor¶ Softmax is defined as:
\[\begin{split}\text{Softmax}(x_{i}) = \frac{\\exp(x_i)}{\sum_j \exp(x_j)}\end{split}\]See
Softmax
for more details.
-
oneflow.
softplus
(x: Tensor, beta: double = 1, threshold: double = 20) → Tensor¶ Applies the element-wise function:
\[\text{Softplus}(x) = \frac{1}{\beta} * \log(1 + \exp(\beta * x))\]For numerical stability the implementation reverts to the linear function when \(input \times \beta > threshold\).
See
Softplus
for more details.
-
oneflow.
softsign
(x: Tensor) → Tensor¶ The formula is:
\[softsign(x) = \frac{x}{1 + |x|}\]For example:
>>> import numpy as np >>> import oneflow as flow >>> x = np.array([1, 2, 3]).astype(np.float32) >>> input = flow.tensor(x) >>> out = flow.nn.functional.softsign(input) >>> out tensor([0.5000, 0.6667, 0.7500], dtype=oneflow.float32)
See
Softsign
for more details.
-
oneflow.
sort
(input, 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) – the 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)
-
oneflow.
split
()¶ 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))
-
oneflow.
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, dtype=flow.float32) >>> output = flow.sqrt(input).numpy() >>> output array([1. , 1.4142135, 1.7320508], dtype=float32)
-
oneflow.
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, dtype=flow.float32) >>> output = flow.square(input).numpy() >>> output array([1., 4., 9.], dtype=float32)
-
oneflow.
squeeze
()¶ 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 (int, optinal) – Defaults to None, if given, the input will be squeezed only in this dimension.
- 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])
-
oneflow.
stack
()¶ 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 >>> x1 = flow.tensor(np.random.rand(1, 3, 5)) >>> x2 = flow.tensor(np.random.rand(1, 3, 5)) >>> y = flow.stack([x1, x2], dim = -1) >>> y.shape oneflow.Size([1, 3, 5, 2])
-
oneflow.
std
()¶ 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.
dim (int or tuple of 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(1.)
-
oneflow.
sub
()¶ 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), dtype=flow.float32) >>> other = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> out = flow.sub(input,other).numpy() >>> out.shape (2, 3) # scalar subtract >>> input = 5 >>> other = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> out = flow.sub(input,other).numpy() >>> out.shape (2, 3) # broadcast subtract >>> input = flow.tensor(np.random.randn(1,1), dtype=flow.float32) >>> other = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> out = flow.sub(input,other).numpy() >>> out.shape (2, 3)
-
oneflow.
sum
(input, dim=None, keepdim=False) → Tensor¶ Computes the sum of row of elements in a tensor in the given dimension. If the dimension is None, sum of all elements will be caculated.
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
oneflow.squeeze()
, resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).- Parameters
input (oneflow.Tensor) – the Input Tensor
dim (int or tuple of ints, optional) – the dimension to reduce. Default: None
keepdim (bool, optional) – whether the output tensor has dim retained or not. Default: False
For example:
>>> import oneflow as flow >>> input = flow.Tensor([[1, 2, 3], [4, 5, 6]]) >>> flow.sum(input) tensor(21., dtype=oneflow.float32) >>> flow.sum(input, dim=0) tensor([5., 7., 9.], dtype=oneflow.float32) >>> flow.sum(input, dim=1) tensor([ 6., 15.], dtype=oneflow.float32)
-
oneflow.
swapaxes
(input, axis0, axis1) → Tensor¶ This function is equivalent to NumPy’s swapaxes function.
For example:
>>> import oneflow as flow >>> x = flow.tensor([[[0,1],[2,3]],[[4,5],[6,7]]]) >>> x.shape oneflow.Size([2, 2, 2]) >>> flow.swapaxes(x, 0, 1).shape oneflow.Size([2, 2, 2]) >>> flow.swapaxes(x, 0, 2).shape oneflow.Size([2, 2, 2])
-
oneflow.
swapdims
(input, dim0, dim1) → Tensor¶ This function is equivalent to torch’s swapdims function.
For example:
>>> import oneflow as flow >>> x = flow.tensor([[[0,1],[2,3]],[[4,5],[6,7]]]) >>> x tensor([[[0, 1], [2, 3]], [[4, 5], [6, 7]]], dtype=oneflow.int64) >>> flow.swapdims(x, 0, 1) tensor([[[0, 1], [4, 5]], [[2, 3], [6, 7]]], dtype=oneflow.int64) >>> flow.swapdims(x, 0, 2) tensor([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype=oneflow.int64)
-
oneflow.
t
()¶ oneflow.t(input) → Tensor.
Expects input to be <= 2-D tensor and transposes dimensions 0 and 1.
0-D and 1-D tensors are returned as is. When input is a 2-D tensor this is equivalent to transpose(input, 0, 1).
- Parameters
input (oneflow.Tensor) – An input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.tensor(np.random.randn(), dtype=flow.float32) >>> flow.t(x).shape oneflow.Size([]) >>> x = flow.tensor(np.random.randn(3), dtype=flow.float32) >>> flow.t(x).shape oneflow.Size([3]) >>> x = flow.tensor(np.random.randn(2,3), dtype=flow.float32) >>> flow.t(x).shape oneflow.Size([3, 2])
-
oneflow.
tan
()¶ Returns the tan value of the elements of
input
.\[\text{out}_{i} = \tan(\text{input}_{i})\]- Parameters
input (Tensor) – the input tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> np_arr = np.array([-1/4*np.pi, 0, 1/4*np.pi]).astype(np.float32) >>> input = flow.tensor(np_arr, dtype=flow.float32) >>> output = flow.tan(input) >>> output tensor([-1., 0., 1.], dtype=oneflow.float32)
-
oneflow.
tanh
(x: Tensor) → Tensor¶ The equation is:
\[out = \frac{e^x-e^{-x}}{e^x+e^{-x}}\]See
Tanh
for more details.
-
oneflow.
tensor
()¶ - Constructs a tensor with data, return a global tensor if placement and sbp are in kwargs,
otherwise return a local tensor.
- Parameters
data – Initial data for the tensor. Can be a list, tuple, NumPy ndarray, scalar or tensor.
- Keyword Arguments
dtype (oneflow.dtype, optional) – Default: if None, infers data type from data.
device (oneflow.device, optional) – the desired device of returned tensor. If placement and sbp is None, uses the current cpu for the default tensor type.
placement (oneflow.placement, optional) – the desired placement of returned tensor.
sbp (oneflow.sbp or tuple of oneflow.sbp, optional) – the desired sbp of returned tensor.
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False
pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.
Note
The Keyword Argument device is mutually exclusive with placement and sbp.
For example:
>>> import oneflow as flow >>> x = flow.tensor([1,2,3]) >>> x tensor([1, 2, 3], dtype=oneflow.int64)
-
oneflow.
tensor_scatter_nd_update
(tensor, indices, updates)¶ This operation creates a new tensor by applying sparse updates to the input tensor. This is similar to an index assignment.
This operator is very similar to
scatter_nd()
, except that the updates are scattered onto an existing tensor (as opposed to a zero-tensor).- Parameters
tensor – The tensor will be scattered.
indices – The indices of
update
. Its type should be flow.int.update – The update Tensor.
For example:
>>> import oneflow as flow >>> tensor = flow.arange(8) >>> indices = flow.tensor([[1], [3], [5]]) >>> updates = flow.tensor([-1, -2, -3]) >>> flow.tensor_scatter_nd_update(tensor, indices, updates) tensor([ 0, -1, 2, -2, 4, -3, 6, 7], dtype=oneflow.int64)
-
oneflow.
tensor_split
()¶ Splits a tensor into multiple sub-tensors, all of which are views of input, along dimension dim according to the indices or number of sections specified by indices_or_sections . The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.tensor_split.html.
- Parameters
input (Tensor) – the input tensor.
indices_or_sections (int or a list) – If indices_or_sections is an integer n , input is split into n sections along dimension dim.If input is divisible by n along dimension dim, each section will be of equal size, input.size (dim) / n. If input is not divisible by n, the sizes of the first int(input.size(dim) % n). sections will have size int(input.size(dim) / n) + 1, and the rest will have size int(input.size(dim) / n). If indices_or_sections is a list or tuple of ints, then input is split along dimension dim at each of the indices in the list, tuple or tensor. For instance, indices_or_sections=[2, 3] and dim=0 would result in the tensors input[:2], input[2:3], and input[3:].If indices_or_sections is a tensor, it must be a zero-dimensional or one-dimensional long tensor on the CPU.
dim (int) – dimension along which to split the tensor.
- Returns
the output TensorTuple.
- Return type
oneflow.TensorTuple
For example:
>>> import oneflow as flow >>> input = flow.rand(3,4,5) >>> output = flow.tensor_split(input,(2,3),2) >>> output[0].size() oneflow.Size([3, 4, 2]) >>> output[1].size() oneflow.Size([3, 4, 1]) >>> output[2].size() oneflow.Size([3, 4, 2])
-
oneflow.
tensordot
(a, b, dims=Union[int, Tensor, Tuple[List[int], List[int]], List[List[int]]], out=None) → Tensor¶ Compute tensor dot along given dimensions.
Given two tensors a and b, and dims which represent two lists containing dim indices, tensordot traverses the two lists and calculate the tensor dot along every dim pair.
- Parameters
a (oneflow.Tensor) – The input tensor to compute tensordot
b (oneflow.Tensor) – The input tensor to compute tensordot
dims (int or list or tuple or oneflow.Tensor) – The dims to calculate tensordot. If it’s an integer or oneflow.Tensor with only one element, the last dims of tensor a and the first dims of tensor b will be calculated. If it’s a list or tuple or oneflow.Tensor with more than one element, it must contain two array-like object, which represent the dims of tensor a and tensor b to be calculated.
out (oneflow.Tensor) – The tensor to save result (NOT IMPLEMENTED YET)
- Returns
The result tensor
- Return type
For example:
>>> import oneflow as flow >>> a = flow.randn(3, 4, 5) >>> b = flow.randn(4, 5, 6) >>> flow.tensordot(a, b, dims=2).shape oneflow.Size([3, 6]) >>> b = flow.randn(5, 6, 7) >>> flow.tensordot(a, b, dims=1).shape oneflow.Size([3, 4, 6, 7]) >>> b = flow.randn(3, 4, 7) >>> flow.tensordot(a, b, dims=[[0, 1], [0, 1]]).shape oneflow.Size([5, 7])
Note
Three common use cases are:
dims = 0 : tensor product \(a \otimes b\)
dims = 1 : tensor dot product \(a \cdot b\)
dims = 2 : (default) tensor double contraction \(a : b\)
The part of documentation is referenced from https://numpy.org/doc/stable/reference/generated/numpy.tensordot.html.
Note
The operation is equivalent to the series of operations:
Permute the dimensions of the tensor A that require tensordot to the end
Permute the dimensions of the tensor B that require tensordot to the start
Reshape the permuted tensor A into a 2-dimensional tensor, where the size of the 0th dimension is the product of the dimensions that do not require dot product, and the size of the 1st dimension is the product of the dimensions that require dot product
Reshape the permuted tensor B into a 2-dimensional tensor, where the size of the 0th dimension is the product of the dimensions that require dot product, and the size of the 1st dimension is the product of the dimensions that do not require dot product
Calculate the matrix multiplication of reshaped tensor A and reshaped tensor B
Reshape the result of matrix multiplication, the target shape is the concatenation of the dimensions that do not require tensordot of tensor A and B
This series of operations can be equivalently represented by the following code:
>>> import oneflow as flow >>> a = flow.randn(2, 4, 3) >>> b = flow.randn(3, 4, 2) >>> dims = [[0, 2], [2, 0]] >>> permuted_a = a.permute(1, 0, 2) # 0, 2 are the dimensions requiring tensordot and are placed in the end in permuting >>> permuted_b = b.permute(2, 0, 1) # 2, 0 are the dimensions requiring tensordot and are placed at the beginning in permuting >>> reshaped_a = permuted_a.reshape(4, 2 * 3) # 4 is the dimensions of a that do not require tensordot >>> reshaped_b = permuted_b.reshape(2 * 3, 4) # 4 is the dimensions of a that do not require tensordot >>> matmul_result = flow.matmul(reshaped_a, reshaped_b) >>> result = matmul_result.reshape(4, 4) # 4, 4 are the concatentation of dimensions that do not require tensordot of a and b >>> flow.all(result == flow.tensordot(a, b, dims)) tensor(True, dtype=oneflow.bool)
-
oneflow.
tile
(input, dims) → Tensor¶ The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.tile.html.
Constructs a tensor by repeating the elements of
input
. Thedims
argument specifies the number of repetitions in each dimension.If
dims
specifies fewer dimensions thaninput
has, then ones are prepended todims
until all dimensions are specified. For example, ifinput
has shape (8, 6, 4, 2) anddims
is (2, 2), thendims
is treated as (1, 1, 2, 2).Analogously, if
input
has fewer dimensions thandims
specifies, theninput
is treated as if it were unsqueezed at dimension zero until it has as many dimensions asdims
specifies. For example, ifinput
has shape (4, 2) anddims
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.
dims (tuple) – the number of repetitions per dimension.
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) >>> out = input.tile(2,1,2,1) >>> out.shape oneflow.Size([10, 3, 12, 9]) >>> x = np.random.randn(5, 2, 1) >>> input = flow.Tensor(x) >>> out = input.tile(3,4) >>> out.shape oneflow.Size([5, 6, 4])
-
oneflow.
topk
(input, 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])
-
oneflow.
transpose
()¶ Returns a tensor that is a transposed version of input. The given dimensions dim0 and dim1 are swapped.
The resulting out tensor shares its underlying storage with the input tensor, so changing the content of one would change the content of the other.
- Parameters
input (oneflow.Tensor) – the input tensor.
dim0 (int) – the first dimension to be transposed.
dim1 (int) – the second dimension to be transposed.
- Returns
A transposed tensor.
- Return type
For example:
>>> import numpy as np >>> import oneflow as flow >>> input = flow.tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) >>> out = flow.transpose(input, 0, 1).shape >>> out oneflow.Size([6, 2, 5, 3])
-
oneflow.
tril
()¶ Returns the lower triangular part of a matrix (2-D tensor) or batch of matrices input along the specified diagonal, the other elements of the result tensor out are set to 0.
Note
if diagonal = 0, the diagonal of the returned tensor will be the main diagonal,
if diagonal > 0, the diagonal of the returned tensor will be above the main diagonal,
if diagonal < 0, the diagonal of the returned tensor will be below the main diagonal.
- Parameters
input (Tensor) – the input tensor.
diagonal (int, optional) – the diagonal to specify.
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.tensor(np.ones(shape=(3, 3)).astype(np.float32)) >>> flow.tril(x) tensor([[1., 0., 0.], [1., 1., 0.], [1., 1., 1.]], dtype=oneflow.float32)
-
oneflow.
unbind
()¶ This function is equivalent to PyTorch’s unbind function. Removes a tensor dimension.
Returns a tuple of all slices along a given dimension, already without it.
- Parameters
x (Tensor) – the tensor to unbind
dim (int) – dimension to remove
For example:
>>> import oneflow as flow >>> x = flow.tensor(range(12)).reshape([3,4]) >>> flow.unbind(x) (tensor([0, 1, 2, 3], dtype=oneflow.int64), tensor([4, 5, 6, 7], dtype=oneflow.int64), tensor([ 8, 9, 10, 11], dtype=oneflow.int64)) >>> flow.unbind(x, 1) (tensor([0, 4, 8], dtype=oneflow.int64), tensor([1, 5, 9], dtype=oneflow.int64), tensor([ 2, 6, 10], dtype=oneflow.int64), tensor([ 3, 7, 11], dtype=oneflow.int64))
-
oneflow.
unsqueeze
(input, dim) → Tensor¶ 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])
-
oneflow.
var
()¶ 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.
dim (int or tuple of 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 >>> input = flow.tensor(np.random.randn(2, 3, 4, 5)) >>> output = flow.var(input, 1, True)
-
oneflow.
vsplit
()¶ Splits input, a tensor with two or more dimensions, into multiple tensors vertically according to indices_or_sections. Each split is a view of input. This is equivalent to calling oneflow.tensor_split(input, indices_or_sections, dim=0) (the split dimension is 0), except that if indices_or_sections is an integer it must evenly divide the split dimension or a runtime error will be thrown. The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.vsplit.html.
- Parameters
input (Tensor) – the input tensor.
indices_or_sections (int or a list) – If indices_or_sections is an integer n , input is split into n sections along dimension dim.If input is divisible by n along dimension dim, each section will be of equal size, input.size (dim) / n. If input is not divisible by n, the sizes of the first int(input.size(dim) % n). sections will have size int(input.size(dim) / n) + 1, and the rest will have size int(input.size(dim) / n). If indices_or_sections is a list or tuple of ints, then input is split along dimension dim at each of the indices in the list, tuple or tensor. For instance, indices_or_sections=[2, 3] and dim=0 would result in the tensors input[:2], input[2:3], and input[3:].If indices_or_sections is a tensor, it must be a zero-dimensional or one-dimensional long tensor on the CPU.
- Returns
the output TensorTuple.
- Return type
oneflow.TensorTuple
For example:
>>> import oneflow as flow >>> input = flow.rand(4, 4, 5, 6) >>> output = flow.vsplit(input, (1, 3)) >>> output[0].size() oneflow.Size([1, 4, 5, 6]) >>> output[1].size() oneflow.Size([2, 4, 5, 6]) >>> output[2].size() oneflow.Size([1, 4, 5, 6])
-
oneflow.
where
(condition, 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)
-
oneflow.
zeros
(*size: Union[int, Tuple[int, …], oneflow.Size, List[int]], dtype: Optional[oneflow._oneflow_internal.dtype] = None, device: Optional[Union[str, oneflow._oneflow_internal.device]] = None, placement: Optional[oneflow._oneflow_internal.placement] = None, sbp: Optional[oneflow._oneflow_internal.sbp.sbp] = None, requires_grad: bool = False)¶ Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size.
- Parameters
size (an integer or tuple of integer values) – a variable number of arguments or a collection like a list or tuple.
dtype (flow.dtype, optional) – the desired data type of returned tensor.
device (flow.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type
placement (flow.placement, optional) – the desired placement of returned global 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 global 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 oneflow as flow >>> y = flow.zeros(5) >>> y tensor([0., 0., 0., 0., 0.], dtype=oneflow.float32) >>> y = flow.zeros(2,3) >>> y tensor([[0., 0., 0.], [0., 0., 0.]], dtype=oneflow.float32)
-
oneflow.
zeros_like
(input) is equivalent to flow.zeros(input.shape, dtype=input.dtype)¶ - Parameters
other (Tensor) – The size of input will determine size of the output tensor.
For example:
>>> import oneflow as flow >>> import numpy as np >>> x = flow.tensor(np.random.rand(5), dtype=flow.float32) >>> y = flow.zeros_like(x) >>> y tensor([0., 0., 0., 0., 0.], dtype=oneflow.float32)
-
oneflow.
relu
()¶ Applies the rectified linear unit function element-wise. See
ReLU
for more details.- Parameters
inplace – If set to
True
, will do this operation in-place. Default:False
For examples:
>>> import oneflow as flow >>> import numpy as np >>> ndarr = np.asarray([1, -2, 3]) >>> input = flow.Tensor(ndarr) >>> output = flow.relu(input) >>> output tensor([1., 0., 3.], dtype=oneflow.float32)
-
oneflow.
set_num_threads
()¶ Sets the number of threads used for intraop parallelism on CPU.
Warning
To ensure that the correct number of threads is used, set_num_threads must be called before running eager, eager globe or ddp.