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.

oneflow.BoolTensor(*args, **kwargs)

创建一个张量,其数据类型为 oneflow.bool,它的参数与 oneflow.Tensor() 相同。

oneflow.ByteTensor(*args, **kwargs)

创建一个张量,其数据类型为 oneflow.uint8,它的参数与 oneflow.Tensor() 相同。

oneflow.CharTensor(*args, **kwargs)

创建一个张量,其数据类型为 oneflow.int8,它的参数与 oneflow.Tensor() 相同。

oneflow.DoubleTensor(*args, **kwargs)

创建一个张量,其数据类型为 oneflow.float64,它的参数与 oneflow.Tensor() 相同。

oneflow.FloatTensor(*args, **kwargs)

创建一个张量,其数据类型为 oneflow.float32,它的参数与 oneflow.Tensor() 相同。

oneflow.HalfTensor(*args, **kwargs)

创建一个张量,其数据类型为 oneflow.float16,它的参数与 oneflow.Tensor() 相同。

oneflow.IntTensor(*args, **kwargs)

创建一个张量,其数据类型为 oneflow.int32,它的参数与 oneflow.Tensor() 相同。

oneflow.LongTensor(*args, **kwargs)

创建一个张量,其数据类型为 oneflow.int64,它的参数与 oneflow.Tensor() 相同。

oneflow.abs(x)Tensor

返回一个包含 x 中每个元素的绝对值的tensor:y = |x|

参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x = flow.tensor([-1, 2, -3, 4], dtype=flow.float32)
>>> flow.abs(x)
tensor([1., 2., 3., 4.], dtype=oneflow.float32)
oneflow.acos(x)Tensor

返回一个包含 x 中元素的反余弦值的新 tensor。

公式为:

\[\text{out}_{i} = \arccos(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([0.5, 0.6, 0.7], dtype=flow.float32)
>>> output = flow.acos(input)
>>> output
tensor([1.0472, 0.9273, 0.7954], dtype=oneflow.float32)
oneflow.acosh(x)Tensor

返回具有 x 中元素的反双曲余弦的新 tensor。

公式为:

\[\text{out}_{i} = \cosh^{-1}(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x1 = flow.tensor([2, 3, 4], dtype=flow.float32)
>>> out1 = flow.acosh(x1)
>>> out1
tensor([1.3170, 1.7627, 2.0634], dtype=oneflow.float32)
>>> x2 = flow.tensor([1.5, 2.6, 3.7], dtype=flow.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)

在由多个平面组成的信号 input 上应用 1D 自适应平均池化。

参考: oneflow.nn.AdaptiveAvgPool1d

参数:
  • input (Tensor): 输入张量

  • output_size - 目标输出大小(单个整数)

oneflow.adaptive_avg_pool2d(input, output_size)

在由多个平面组成的信号 input 上应用 2D 自适应平均池化。

参考: oneflow.nn.AdaptiveAvgPool2d

参数:
  • input (Tensor): 输入张量

  • output_size - 目标输出大小(单个整数或包含两个整数的元组)

oneflow.adaptive_avg_pool3d(input, output_size)

在由多个平面组成的信号 input 上应用 3D 自适应平均池化。

参考: oneflow.nn.AdaptiveAvgPool3d

参数:
  • input (Tensor) - 输入张量

  • output_size - 目标输出大小(单个整数或包含三个整数的元组)

oneflow.add(input, other)oneflow.Tensor

计算 inputother 的和。支持 element-wise 、标量和广播形式的加法。

公式为:

\[out = input + other\]
参数:
  • input (Tensor) - 输入张量

  • other (Tensor) - 其余输入张量

返回类型:

oneflow.Tensor

示例:

>>> import oneflow as flow

# element-wise 加法
>>> x = flow.randn(2, 3, dtype=flow.float32)
>>> y = flow.randn(2, 3, dtype=flow.float32)
>>> out = flow.add(x, y)
>>> out.shape
oneflow.Size([2, 3])

# 标量加法
>>> x = 5
>>> y = flow.randn(2, 3, dtype=flow.float32)
>>> out = flow.add(x, y)
>>> out.shape
oneflow.Size([2, 3])

# 广播加法
>>> x = flow.randn(1, 1, dtype=flow.float32)
>>> y = flow.randn(2, 3, dtype=flow.float32)
>>> out = flow.add(x, y)
>>> out.shape
oneflow.Size([2, 3])
oneflow.addcmul(input, tensor1, tensor2, *, value=1)Tensor

执行 tensor1 与 tensor2 的逐元乘法,将结果与标量值相乘并加到输入中。

该文档引用自: https://pytorch.org/docs/stable/generated/torch.addcmul.html

\[\text{out}_i = \text{input}_i + value \times\ \text{tensor1}_i \times\ \text{tensor2}_i\]
参数:
  • input (Tensor) - 被添加的张量

  • tensor1 (Tensor) - 被乘的张量

  • tensor2 (Tensor) - 被乘的张量

关键字参数:

value (Number, optional): 乘法器 \(tensor1 * tensor2\).

返回类型:

oneflow.Tensor: 输出张量

示例:

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

mat1mat2 进行矩阵乘法,并且将结果与 input 相加求和后,返回计算结果。

如果 mat1 是一个 \((n \times m)\) 张量,同时 mat2 是一个 \((m \times p)\) 张量, 则 input 必须是可广播为 (n times p) 的张量,out 也必须为 \((n \times p)\) 的张量。

公式为:

alphamat1mat2 的矩阵向量乘积的缩放比例因数, betainput 的因数

\[\text{out} = \beta\ \text{input} + \alpha\ (\text{mat1}_i \mathbin{@} \text{mat2}_i)\]

如果 input 的类型为 floatdouble, 参数 betaalpha 应为实数,否则只能是整数(integers)。

参数:
  • beta (Number, 可选): input 的因数 (\(\beta\))

  • input (Tensor): 作为加数的矩阵

  • alpha (Number, 可选): \(mat1 \mathbin{@} mat2\) 的因数 (\(\alpha\))

  • mat1 (Tensor): 作为第一个乘数的矩阵

  • mat2 (Tensor): 作为第二个乘数的矩阵

  • out (Tensor, 可选): 输出张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow
>>> input = flow.tensor([[1,2,4],[5,11,9.1]], dtype=flow.float64)
>>> mat1 = flow.tensor([[7.3,1.9,7.3],[10.2,1,5.5]], dtype=flow.float64)
>>> mat2 = flow.tensor([[7.3,1.9,7.3],[10.2,1,5.5],[3.7,2.2,8.1]], dtype=flow.float64)
>>> 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([1.7], dtype=flow.float64)
>>> mat1 = flow.tensor([[1,2],[5,9.1],[7.7,1.4]], dtype=flow.float64)
>>> mat2 = flow.tensor([[1,2,3.7],[5,9.1,6.8]], dtype=flow.float64)
>>> 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

对于给定维度 diminput 的每一行,如果该行的所有元素都为 True,则返回 True,否则返回 False。如果维度是 None,则计算输入张量中的所有元素是否为真。

如果 keepdimTrue ,输出张量与 input 大小相同,但 dim 的维度大小为 1 的情况除外。否则, dim 会被压缩 oneflow.squeeze() ,导致输出张量的大小减少 1(或 len(dim))。

参数:
  • input (oneflow.Tensor) - 输入张量

  • dim (int, optional) - 减少的维度。 默认值: None

  • keepdim (bool, optional) - 输出张量是否有 dim 的保留. 默认值: False

示例:

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

这个函数与 PyTorch 的 amax 函数等价,它返回沿维度的最大值。

参数:
  • input (oneflow.Tensor) - 输入张量。

  • dim (int or List of int, optional) - 维度或要减少的维度,dim 的默认值为 None.

  • keepdim (bool, optional) - 是否保留维度。keepdim 默认为 False。

返回值类型:

oneflow.Tensor: 输入张量的最大值。

示例:

>>> 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=None, keepdim=False)Tensor

这个函数与 PyTorch 的 amin 函数等价。

该文档引用自: https://pytorch.org/docs/stable/generated/torch.amin.html

返回给定维度 dim 中输入张量的每个切片的最小值。

如果 keepdimTrue,则输出张量的大小与输入的大小相同,但维度 dim 大小为 1 的除外。否则,dim 被压缩(参考 oneflow.squeeze()),导致输出张量具有1(或 len(dim) )更少的维度。

参数:
  • input (oneflow.Tensor) - 输入张量。

  • dim (int, Tuple[int]) - 维度或减少的维度。

  • keepdim (bool) - 输出张量是否保留了`dim`

示例:

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

对于给定维度 dim 中的每一行输入,如果该行中的任何元素评估为 True,则返回 True,否则返回 False。如果维度为 None,则计算输入张量中的元素是否都为真。

如果 keepdim 为 True,则输出张量的大小与输入相同,但尺寸为1的维度 dim 除外。否则,dim 被挤压 oneflow.squeeze() ,导致输出张量具有1(或 len(dim))更少的维度。

参数:
  • input (oneflow.Tensor) - 输入张量

  • dim (int, optional) - 减少的维度。 默认值: None

  • keepdim (bool, optional) - 输出张量是否有 dim 的保留. 默认值: False

示例:

>>> 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=0, end, step=1, *, dtype=kInt64, device=None, requires_grad=Flase)Tensor

返回一个大小为 \(\left\lfloor \frac{\text{end} - \text{start}}{\text{step}} \right\rfloor + 1\) , 其元素为从 start (包括)到 end (不包括)跨度为 step 的所有整数。

公式为:

\[\text{out}_{i+1} = \text{out}_i + \text{step}.\]
参数:
  • start (int): 返回的集合的起始值。默认为 0。

  • end (int): 返回的集合的结束值。

  • step (int): 相邻点的跨度。默认为 1。

关键词参数:
  • dtype (flow.dtype, 可选): 如果未设定 dtype ,则 dtypeflow.int64.

  • device (flow.device, 可选): 返回张量的所需设备。当前设备作为默认张量。

  • requires_grad (bool, 可选): 如果 autograd 为 True 则记录对返回张量的操作。默认为 False.

示例:

>>> import oneflow as flow

>>> y = flow.arange(0, 5)
>>> y
tensor([0, 1, 2, 3, 4], dtype=oneflow.int64)
oneflow.arccos()

acos(x) -> Tensor

返回一个包含 x 中元素的反余弦值的新 tensor。

公式为:

\[\text{out}_{i} = \arccos(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([0.5, 0.6, 0.7], dtype=flow.float32)
>>> output = flow.acos(input)
>>> output
tensor([1.0472, 0.9273, 0.7954], dtype=oneflow.float32)
oneflow.arccosh()

acosh(x) -> Tensor

返回具有 x 中元素的反双曲余弦的新 tensor。

公式为:

\[\text{out}_{i} = \cosh^{-1}(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x1 = flow.tensor([2, 3, 4], dtype=flow.float32)
>>> out1 = flow.acosh(x1)
>>> out1
tensor([1.3170, 1.7627, 2.0634], dtype=oneflow.float32)
>>> x2 = flow.tensor([1.5, 2.6, 3.7], dtype=flow.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(x)Tensor

返回一个新的 tensor 包含 x 中每个元素的反正弦。

公式为:

\[\text{out}_{i} = \sin^{-1}(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([-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([[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(x)Tensor

返回一个包含 x 中每个元素的反双曲正弦的新 tensor。

公式为:

\[\text{out}_{i} = \sinh^{-1}(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

关键词参数:

out (Tensor, optional): 输出张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([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([[-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(x)Tensor

返回一个包含 x 中所有元素的反正切的新 tensor。

公式为:

\[\text{out}_{i} = \tan^{-1}(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([0.5, 0.6, 0.7], dtype=flow.float32)
>>> output = flow.atan(input)
>>> output.shape
oneflow.Size([3])
oneflow.arctanh(x)Tensor

返回一个包含 x 中元素的反双曲正切值的新 tensor。

公式为:

\[\text{out}_{i} = \tanh^{-1}(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([0.5, 0.6, 0.7], dtype=flow.float32)
>>> output = flow.atanh(input)
>>> output
tensor([0.5493, 0.6931, 0.8673], dtype=oneflow.float32)
oneflow.argmax(input, dim=- 1, keepdim=False)Tensor

返回 input 在指定维度上的最大值的 index

参数:
  • input (oneflow.tensor): 输入张量

  • dim (int, 可选): 要计算的维度,默认为最大维度(-1)。

  • keepdim (bool,可选的): 返回值是否保留 input 的原有维数。默认为 False 。

返回类型:

oneflow.tensor: 包含 input 特定维度最大值的 index 的新张量(dtype=int64)。

示例:

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

返回 input 在指定维度上的最小值的 index

参数:
  • input (oneflow.tensor): 输入张量

  • dim (int, 可选): 要计算的维度,默认为最大维度(-1)。

  • keepdim (bool,可选的): 返回值是否保留 input 的原有维数。默认为 False 。

返回类型:

oneflow.tensor: 包含 input 特定维度最小值的 index 的新张量(dtype=int64)。

示例:

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

此算子以指定的 dim 对输入张量进行排序,并返回排序张量的索引。

参数:
  • input (oneflow.Tensor) - 输入张量。

  • dim (int, optional) - 要排序的维度。默认为最后一个维度(-1)。

  • descending (bool, optional) - 控制排序顺序(升序或降序)。

返回值类型:

oneflow.Tensor: 排序张量的索引。

示例:

>>> 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=flow.int32)Tensor

返回一个包含所有 input 中非 0 元素的 index 的列表。返回列表为一个 tensor,其中元素为坐标值。

参数:
  • input (oneflow.tensor): 输入张量

  • dtype (Optional[flow.dtype], 可选): 输出的数据类型,默认为 flow.int32

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([[0, 1, 0], [2, 0, 2]], dtype=flow.int32)
>>> output = flow.argwhere(input)
>>> output
tensor([[0, 1],
        [1, 0],
        [1, 2]], dtype=oneflow.int32)
oneflow.as_strided()

创建具有指定大小、步幅和存储偏移的 oneflow.Tensor 输入的视图。

该文档引用自: https://pytorch.org/docs/1.10/generated/torch.as_strided.html

参数:
  • input (Tensor) - 被添加的张量。

  • size (tuple or ints) - 输出张量的形状。

  • stride (tuple or ints) - 输出张量的步长。

  • storage_offset (int) - 输出张量的底层存储中的偏移量。

返回类型:

oneflow.Tensor: 输出张量

示例:

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

该接口与 PyTorch 一致。

如果可能,将数据转换为张量,共享数据并保留 autograd 历史记录

如果 data 已经是具有请求的 dtype 和 device 的张量,则返回数据本身,但如果 data 是具有不同 dtype 或 device 的张量,则将其复制为使用 data.to(dtype=dtype, device=device)。

如果 data 是具有相同 dtype 和 device 的 NumPy 数组(一个 ndarray),则使用 oneflow.from_numpy 构造一个张量。

参数:
  • data (array_like) - 张量的初始数据。可以是 list, tuple, NumPy, ndarray, scalar和其他类型。

  • dtype (oneflow.dtype, optional) - 返回张量的所需数据类型。默认值:如果为 None,从数据推断数据类型。

  • device (oneflow.device, optional) - 构造张量的设备。如果为 None 并且 data 是张量,则使用 data 的设备。如果为 Nonedata 不是张量,则在 CPU 上构建结果张量。

示例:

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

arcsin(x) -> Tensor

返回一个新的 tensor 包含 x 中每个元素的反正弦。

公式为:

\[\text{out}_{i} = \sin^{-1}(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([-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([[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()

arcsinh(x) -> Tensor

返回一个包含 x 中每个元素的反双曲正弦的新 tensor。

公式为:

\[\text{out}_{i} = \sinh^{-1}(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

关键词参数:

out (Tensor, optional): 输出张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([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([[-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()

arctan(x) -> Tensor

返回一个包含 x 中所有元素的反正切的新 tensor。

公式为:

\[\text{out}_{i} = \tan^{-1}(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([0.5, 0.6, 0.7], dtype=flow.float32)
>>> output = flow.atan(input)
>>> output.shape
oneflow.Size([3])
oneflow.atan2(input, other)

考虑象限的 \(input_{i}/other_{i}\) 的按元素反正切。返回元素为向量 \((other_{i},input_{i})\) 和向量 (1, 0) 之间的按元素夹角(以弧度表示并带符号)的新张量。

inputother 的形状必须是可广播的。

参数:
  • input (Tensor): 第一个输入张量。

  • other (Tensor): 第二个输入张量。

示例:

>>> import oneflow as flow

>>> x1 = flow.tensor([1,2,3], dtype=flow.float32)
>>> y1 = flow.tensor([3,2,1], dtype=flow.float32)
>>> x2 = flow.tensor([1.53123589,0.54242598,0.15117185], dtype=flow.float32)
>>> y2 = flow.tensor([-0.21906378,0.09467151,-0.75562878], dtype=flow.float32)
>>> x3 = flow.tensor([1,0,-1], dtype=flow.float32)
>>> y3 = flow.tensor([0,1,0], dtype=flow.float32)

>>> flow.atan2(x1,y1)
tensor([0.3218, 0.7854, 1.2490], dtype=oneflow.float32)
>>> flow.atan2(x2,y2)
tensor([1.7129, 1.3980, 2.9441], dtype=oneflow.float32)
>>> flow.atan2(x3,y3)
tensor([ 1.5708,  0.0000, -1.5708], dtype=oneflow.float32)
oneflow.atanh()

arctanh(x) -> Tensor

返回一个包含 x 中元素的反双曲正切值的新 tensor。

公式为:

\[\text{out}_{i} = \tanh^{-1}(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([0.5, 0.6, 0.7], dtype=flow.float32)
>>> output = flow.atanh(input)
>>> output
tensor([0.5493, 0.6931, 0.8673], dtype=oneflow.float32)
oneflow.batch_gather(in, indices)Tensor

indices 重新排列元素

参数:
  • in (Tensor): 输入张量

  • indices (Tensor): 索引张量,它的数据类型必须是 int32/64。

示例:

示例1:

>>> import oneflow as flow

>>> x = flow.tensor([[1, 2, 3], [4, 5, 6]], dtype=flow.float32)
>>> indices = flow.tensor([1, 0], dtype=flow.int64)
>>> out = flow.batch_gather(x, indices)
>>> out
tensor([[4., 5., 6.],
        [1., 2., 3.]], dtype=oneflow.float32)

示例2:

>>> import oneflow as flow

>>> x = flow.tensor([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]], dtype=flow.float32)
>>> indices = flow.tensor([[1, 0], [0, 1]], dtype=flow.int64)
>>> out = flow.batch_gather(x, indices)
>>> out
tensor([[[4., 5., 6.],
         [1., 2., 3.]],

        [[1., 2., 3.],
         [4., 5., 6.]]], dtype=oneflow.float32)
oneflow.bernoulli(x, *, generator=None, out=None)

返回一个 Tensor,起元素为0或1,且符合伯努利分布 。

参数:
  • x (Tensor): 伯努利分布的概率值的输入张量

  • generator (Generator, 可选): 用于采样的伪随机数生成器

  • out (Tensor, 可选): 输出张量

形状:
  • Input \((*)\): 输入可以是任何形状

  • Output \((*)\): 输出与输入的形状相同

示例:

>>> import oneflow as flow

>>> x = flow.tensor([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], dtype=flow.float32)
>>> y = flow.bernoulli(x)
>>> y
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=oneflow.float32)
oneflow.bmm()

对存储在 input 和 mat2 中的矩阵进行批量矩阵-矩阵乘法。

inputmat2 必须是 3-D 张量,每个张量都包含相同数量的矩阵。

如果 input 是 (b x n x m) 张量,mat2 是 (b x m x p) 张量,out 将是 (b x n x p) 张量。

参数:
  • input (oneflow.Tensor) - 要相乘的第一批矩阵。

  • mat2 (oneflow.Tensor) - 要相乘的第二批矩阵。

示例:

>>> 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(x, like_tensor, broadcast_axes=None)Tensor

x 沿着 broadcast_axes 广播为 like_tensor 的形式。

参数:
  • x (Tensor): 输入张量

  • like_tensor (Tensor): 参考张量

  • broadcast_axes (Optional[Sequence], 可选): 想要广播的维度,默认为None。

返回类型:

oneflow.tensor: 广播输入张量。

示例:

>>> 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(x, dtype)Tensor

将输入张量 x 转化为数据类型 dtype

参数:
  • x (oneflow.tensor): 输入张量

  • dtype (flow.dtype): 输出张量的数据类型

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow
>>> input = flow.randn(2, 3, 4, 5, dtype=flow.float32)
>>> output = flow.cast(input, flow.int8)
>>> output.shape
oneflow.Size([2, 3, 4, 5])
oneflow.cat(inputs, dim=0)Tensor

在指定的维度 dim 上连接两个或以上 tensor。

类似于 numpy.concatenate

参数:
  • inputs : 一个包含要连接的 Tensorlist

  • dim (int): 要连接的维度。

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input1 = flow.randn(2, 6, 5, 3, dtype=flow.float32)
>>> input2 = flow.randn(2, 6, 5, 3, dtype=flow.float32)
>>> input3 = flow.randn(2, 6, 5, 3, dtype=flow.float32)

>>> out = flow.cat([input1, input2, input3], dim=1)
>>> out.shape
oneflow.Size([2, 18, 5, 3])
oneflow.ceil(x)Tensor

返回一个新的 tensor,tensor 中元素为大于或等于 x 中元素的最小整数。

公式为:

\[\text{out}_{i} = \left\lceil \text{input}_{i} \right\rceil = \left\lfloor \text{input}_{i} \right\rfloor + 1\]
参数:

x (oneflow.tensor): 张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x = flow.tensor([0.1, -2, 3.4], dtype=flow.float32)
>>> y = flow.ceil(x)
>>> y.shape
oneflow.Size([3])
>>> y
tensor([ 1., -2.,  4.], dtype=oneflow.float32)
>>> x = flow.tensor([[2.5, 4.6, 0.6],[7.8, 8.3, 9.2]], dtype=flow.float32)
>>> y = x.ceil()
>>> y.shape
oneflow.Size([2, 3])
>>> y
tensor([[ 3.,  5.,  1.],
        [ 8.,  9., 10.]], dtype=oneflow.float32)
>>> x = flow.tensor([[[2.2, 4.4, 6.5],[7.1, 8.2, 9.3]],[[10.6,11.2,12.2],[13.5,14.8,15.9]]], dtype=flow.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(input, chunks, dim)Tensor

将张量 input 拆分为特定数量的块。 每个块都是输入张量的一个视图。 最后一个块会小于其他的块如果在指定的维度 diminput 的大小不能被 chunks 整除。

参数:
  • input (oneflow.tensor): 要拆分的张量

  • chunks (int): 要返回的块数。

  • dim (int): 拆分张量的维度。

返回类型:

包含 oneflow.tensor 的 List。

示例:

>>> 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(input, min=None, max=None)Tensor

返回新的结果 tensor,结果 tensor 中将 input 中元素限制在范围 [ min, max ] 中。

公式为:

\[\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}\]

如果 input 的类型是 FloatTensorFloatTensor,参数 minmax 必须为实数, 如果 input 为其它类型的 tensor,参数 minmax 必须为 integer

参数:
  • input (Tensor): 输入张量

  • min (Number): 要限制到的范围的下限,默认为None

  • max (Number): 要限制到的范围的上限,默认为None

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow
>>> input = flow.tensor([0.2, 0.6, -1.5, -0.3], dtype=flow.float32)
>>> output = flow.clamp(input, min=-0.5, max=0.5)
>>> output
tensor([ 0.2000,  0.5000, -0.5000, -0.3000], dtype=oneflow.float32)

>>> input = flow.tensor([0.2, 0.6, -1.5, -0.3], dtype=flow.float32)
>>> output = flow.clamp(input, min=None, max=0.5)
>>> output
tensor([ 0.2000,  0.5000, -1.5000, -0.3000], dtype=oneflow.float32)

>>> input = flow.tensor([0.2, 0.6, -1.5, -0.3], dtype=flow.float32)
>>> output = flow.clamp(input, min=-0.5, max=None)
>>> output
tensor([ 0.2000,  0.6000, -0.5000, -0.3000], dtype=oneflow.float32)
oneflow.clip()

函数 oneflow.clamp() 的别名.

oneflow.concat()

cat(inputs, dim=0) -> Tensor

在指定的维度 dim 上连接两个或以上 tensor。

类似于 numpy.concatenate

参数:
  • inputs : 一个包含要连接的 Tensorlist

  • dim (int): 要连接的维度。

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input1 = flow.randn(2, 6, 5, 3, dtype=flow.float32)
>>> input2 = flow.randn(2, 6, 5, 3, dtype=flow.float32)
>>> input3 = flow.randn(2, 6, 5, 3, dtype=flow.float32)

>>> out = flow.cat([input1, input2, input3], dim=1)
>>> out.shape
oneflow.Size([2, 18, 5, 3])
oneflow.cos(x)Tensor

返回一个包含 x 中元素的余弦值的新 tensor。

公式为:

\[\text{out}_{i} = \cos(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([1.4309,  1.2706, -0.8562,  0.9796], dtype=flow.float32)
>>> output = flow.cos(input)
oneflow.cosh(x)Tensor

返回一个包含 x 中元素的双曲余弦值的新 tensor。

公式为:

\[\text{out}_{i} = \cosh(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([ 0.1632,  1.1835, -0.6979, -0.7325], dtype=flow.float32)
>>> output = flow.cosh(input)
>>> output
tensor([1.0133, 1.7860, 1.2536, 1.2805], dtype=oneflow.float32)
oneflow.cumprod(input, dim)Tensor

此运算符计算给定维度中输入元素的累积乘积。

方程是:

\[y_{i}=x_{0}*x_{1}*...*x_{i}\]
参数:
  • input (Tensor) - 输入张量。

  • dim (int) - 进行 cumsum 的维度,其有效范围为 [-N, N-1),N 是张量的维度。

返回类型:

oneflow.Tensor: 带有 cumprod 结果的张量

示例:

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

此算子计算给定维度中输入元素的累积和。

方程是:

\[y_{i}=x_{0}+x_{1}+...+x_{i}\]
参数:
  • input (Tensor) - 输入张量。

  • dim (int) - 进行 cumsum 的维度,其有效范围为 [-N, N-1),N 是张量的维度。

返回类型:

oneflow.Tensor: 带有 cumsum 结果的张量

示例:

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

从应该由 oneflow.nn.OneRecReader 之前生成的输入中解码张量。

参数:
  • input (Tensor) - 之前由 oneflow.nn.OneRecReader 生成的张量。

  • key (str) - 要解码的张量的字段名称。

  • shape (bool) - 要解码的张量的形状。

  • is_dynamic (bool) - 张量形状是否是动态的。

  • reshape (tuple) - 设置该参数可以 reshape 张量

  • batch_padding (tuple) - 如果需要批量填充,请设置它。

示例:

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(x, diagonal=0)Tensor

如果 x 是一个向量(一维张量),返回一个二维平方张量,其中 x 的元素作为对角线。 如果 x 是一个矩阵(二维张量),返回一个一维张量,其元素为 x 的对角线元素。

参数 diagonal 决定要考虑哪条对角线:
  • 如果 diagonal = 0,则考虑主对角线

  • 如果 diagonal > 0,则考虑主对角线上方

  • 如果 diagonal < 0,则考虑主对角线下方

参数:
  • x (Tensor): 输入张量

  • diagonal (Optional[Int32], 0): 要考虑的对角线(默认为0)

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor(
...     [
...        [1.0, 2.0, 3.0],
...        [4.0, 5.0, 6.0],
...        [7.0, 8.0, 9.0],
...     ], dtype=flow.float32)
>>> flow.diag(input)
tensor([1., 5., 9.], dtype=oneflow.float32)
oneflow.diagonal(input, offset, dim1, dim2)Tensor

返回输入的部分视图,其对角线元素与 dim1 和 dim2 的关系的对角线元素作为一个维度附加在形状的最后。

参数:
  • input (Tensor) - 输入张量。必须至少是二维的。

  • offset (Optional[int], 0) - 要考虑的对角线。默认值:0(主对角线)。

  • dim1 (Optional[int], 0) - 对角线的第一个维度。默认值:0。

  • dim2 (Optional[int], 1) - 第二维度,相对于它取对角线。默认值:1。

返回值:

oneflow.Tensor: 输出张量。

示例:

>>> 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(input, other)Tensor

计算 input 除以 other,支持 element-wise、标量和广播形式的除法。

公式为:

\[out = \frac{input}{other}\]
参数:
  • input (Union[int, float, oneflow.tensor]): input.

  • other (Union[int, float, oneflow.tensor]): other.

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

# element-wise 除法
>>> input = flow.randn(2, 3, dtype=flow.float32)
>>> other = flow.randn(2, 3, dtype=flow.float32)
>>> out = flow.div(input,other)
>>> out.shape
oneflow.Size([2, 3])

# 标量除法
>>> input = 5
>>> other = flow.randn(2, 3, dtype=flow.float32)
>>> out = flow.div(input,other)
>>> out.shape
oneflow.Size([2, 3])

# 广播除法
>>> input = flow.randn(1, 1, dtype=flow.float32)
>>> other = flow.randn(2, 3, dtype=flow.float32)
>>> out = flow.div(input,other)
>>> out.shape
oneflow.Size([2, 3])
oneflow.dot()

该算子计算张量输入和其他的点积。

公式为:

$$ \sum_{i=1}^{n}(x[i] * y[i]) $$

参数:
  • input (Tensor) - 点积中的第一个张量。

  • other (Tensor) - 点积中的第二个张量。

形状:
  • input:输入必须是一维的。

  • other:其他必须是一维的。

示例:

>>> import oneflow as flow
>>> flow.dot(flow.Tensor([2, 3]), flow.Tensor([2, 1]))
tensor(7., dtype=oneflow.float32)
oneflow.einsum(equation, *operands)

使用基于爱因斯坦求和惯例的符号,沿指定维度对输入 operands 的元素的乘积进行求和。

通过基于爱因斯坦求和惯例的简短格式,详见 equation ,einsum 允许计算许多常见的多维线性代数数组操作。 equation 格式的细节将在下面描述,但通常是用一些下标来标记输入 operands 的每个维度,并定义哪些下标是输出的一部分。然后沿着元素下标不属于输出的一部分的维度,对 operands 元素的乘积进行计算。例如,矩阵乘法可以用 einsum 计算,如 flow.einsum(“ij,jk->ik”, A, B)。这里 j 是求和的下标,i 和 k 是输出的下标(详见下面的章节)。

Equation:

equation 字符串指定了每个输入的 operands 维度的标号(字母 [a-zA-Z]),顺序与维度相同,每个 operands 的标号之间用逗号(’,’)隔开。 例如: 'ij,jk' 指定两个二维 operands 的标号。用同一标号标注的维度的尺寸必须是 broadcastable,也就是说,它们的尺寸必须匹配或为 1。例外的情况是,如果一个标号在同一个输入 operands 上重复出现,这个 operands 用这个下标标注的尺寸必须匹配,并且该 operands 将被其沿这个尺寸的对角线所取代。 在 equation 中出现一次的标号将是输出的一部分,按字母顺序递增排序。 输出是通过将输入的 operands 元素相乘来计算的,然后将子标不属于输出的尺寸相加。 也可以通过在 equation 末尾添加一个箭头(’->’)来明确定义输出下标号。 箭头右边是输出的子标。 例如,下面的方程计算了一个矩阵乘法的转置。 矩阵乘法:’ij,jk->ki’。对于某些输入 operands ,输出的标号必须至少出现一次,而对于输出,则最多出现一次。

省略号(’…’)可以用来代替标号,表示省略的尺寸。 每个输入 operands 最多可以包含一个省略号,该省略号将覆盖未被标号覆盖的尺寸。 例如,对于一个有5个维度的输入 operands ,公式“‘ab…c‘“中的省略号覆盖第三和第四维度。 省略号不需要覆盖整个 operands 的相同数量的维度,但是省略号的’形状’(它们所覆盖的维度的大小)必须一起播出。如果输出没有 用箭头(’->’)符号明确定义,省略号将在输出中排在第一位(最左边的尺寸)。 在输入操作数精确出现一次的下标标签之前。 例如,下面的公式实现了 批量矩阵乘法”’…ij,…jk’”。

最后几点说明:equation 中的不同元素之间可以包含空白(下标、省略号、 箭头和逗号),但像`’. .’’是无效的。空字符串”’‘“对标量操作数有效。

Note

flow.einsum 处理省略号(‘…’)的方式与 NumPy 不同,它允许省略号覆盖的维度被求和。 省略号所覆盖的维度进行求和,也就是说,省略号不需要成为输出的一部分。

Note

这个函数没有对给定的表达式进行优化,所以相同计算的不同公式可能会 运行得更快或消耗更少的内存。使用 opt_einsum(https://optimized-einsum.readthedocs.io/en/stable/)可以优化公式。

参数:
  • equation (String) - 爱因斯坦求和法的标号。

  • operands (oneflow.Tensor) - 计算爱因斯坦求和的张量。

示例:

>>> 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=None, device=None, placement=None, sbp=None, requires_grad=False)Tensor

返回一个填充了未初始化数据的张量。 张量的形状由变量参数 size 定义。

参数:
  • size (int… 或 oneflow.Size): 定义输出张量的形状。可以是可变数量的参数或集合,如列表或元组或 oneflow.Size

  • dtype (flow.dtype, 可选的): 返回张量的数据类型。默认:flow.float32

  • device (torch.device, 可选的): 返回的本地张量的所需设备。默认使用当前设备

  • placement (flow.placement, 可选的): 设置返回张量的 placement 属性。如果为None,则构造 local tensor

  • sbp (flow.sbp 或 List[flow.sbp], 可选的): 返回的global tensor的所需 sbp

  • requires_grad (bool, 可选的): 用 autograd 记录对返回张量的操作,默认为 False

示例:

>>> import oneflow as flow
>>> y = flow.empty(4, 5)  # 构造空 local tensor
>>> y.shape
oneflow.Size([4, 5])
>>> y.is_global
False
>>> placement = flow.placement("cpu", {0: [0]})
>>> y = flow.empty(4, 5, placement=placement, sbp=flow.sbp.broadcast)  # 构造空 global tensor
>>> y.is_global
True
class oneflow.enable_grad

启用梯度计算的上下文管理模式。

如果其被 no_grad 禁用,则在调用时启用梯度计算。

此上下文管理模式位于本地线程;不会影响其他线程的计算。

同时可以作为一种修饰模式。(请确保使用括号实例化)

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

返回 \(input == other\) 的 element-wise 真实值。

参数:
  • input (oneflow.tensor): 要去对比的张量

  • other (oneflow.tensor, float or int): 对比的目标

返回类型:
  • oneflow.tensor,元素为 boolean, 若 input 等于 other 则为 True。

示例:

>>> import oneflow as flow

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

>>> y = flow.eq(input, other)
>>> y
tensor([ True,  True,  True, False], dtype=oneflow.bool)
oneflow.equal()

oneflow.eq(input, other) -> Tensor

返回 \(input == other\) 的 element-wise 真实值。

参数:
  • input (oneflow.tensor): 要去对比的张量

  • other (oneflow.tensor, float or int): 对比的目标

返回类型:
  • oneflow.tensor,元素为 boolean, 若 input 等于 other 则为 True。

示例:

>>> import oneflow as flow

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

>>> y = flow.eq(input, other)
>>> y
tensor([ True,  True,  True, False], dtype=oneflow.bool)
oneflow.erf(x)Tensor

计算每个元素的误差函数。误差函数定义如下:

\[\operatorname{erf}(x)=\frac{2}{\sqrt{\pi}} \int_{0}^{x} e^{-t^{2}} d t\]
参数:

x (oneflow.tensor): 输入张量

返回类型:

oneflow.tensor

示例

>>> import oneflow as flow

>>> x = flow.tensor([0, -1., 10.], dtype=flow.float32)
>>> out = flow.erf(x)
>>> out.shape
oneflow.Size([3])
>>> out
tensor([ 0.0000, -0.8427,  1.0000], dtype=oneflow.float32)
>>> x = flow.tensor([[0, -1., 10.], [5, 7, 0.8]], dtype=flow.float32)
>>> out = flow.erf(x)
>>> out.shape
oneflow.Size([2, 3])
>>> out
tensor([[ 0.0000, -0.8427,  1.0000],
        [ 1.0000,  1.0000,  0.7421]], dtype=oneflow.float32)
>>> x = flow.tensor([[0, -1., 10.], [5, 7, 0.8], [2, 3, 4]], dtype=flow.float32)
>>> out = x.erf()
>>> out.shape
oneflow.Size([3, 3])
>>> out
tensor([[ 0.0000, -0.8427,  1.0000],
        [ 1.0000,  1.0000,  0.7421],
        [ 0.9953,  1.0000,  1.0000]], dtype=oneflow.float32)
oneflow.erfc(x)Tensor

计算 x 的每个元素的互补误差函数。互补误差函数定义如下:

\[\operatorname{erfc}(x)=1-\frac{2}{\sqrt{\pi}} \int_{0}^{x} e^{-t^{2}} d t\]
参数:

x (oneflow.tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x = flow.tensor([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([[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()

计算 :attr: input 的反误差函数。反误差函数在 (-1, 1) 的范围内定义为:

\[\mathrm{erfinv}(\mathrm{erf}(x)) = x\]
参数:
  • input: (oneflow.Tensor) - 输入张量

示例:

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

此运算符计算 x 的指数。

公式为:

\[out = e^x\]
参数:

x (Tensor): 张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x = flow.tensor([1, 2, 3], dtype=flow.float32)
>>> y = flow.exp(x)
>>> y
tensor([ 2.7183,  7.3891, 20.0855], dtype=oneflow.float32)
oneflow.expand(input, *sizes)Tensor

此运算符将输入张量扩展到更大的尺寸。 将 -1 作为 size 意味着不更改该维度的大小。 Tensor input 可以扩展到大的维度,新的维度将附加在前面。 对于新维度,sizes 不能设置为 -1。

参数:
  • input (oneflow.Tensor): 输入张量

  • *sizes (oneflow.Size or int): 所需的展开尺寸。

返回类型:

oneflow.Tensor

示例: .. code-block:: python

>>> import oneflow as flow
>>> input = flow.tensor([[[[0, 1]], [[2, 3]], [[4, 5]]]], dtype=flow.float32)
>>> 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(x)Tensor

返回一个新的张量,其元素为 x 的元素指数减去 1。

公式为:

\[y_{i} = e^{x_{i}} - 1\]
参数:

x (oneflow.tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x = flow.tensor([0, -1., 10.], dtype=flow.float32)
>>> out = flow.erf(x)
>>> out.shape
oneflow.Size([3])
>>> out
tensor([ 0.0000, -0.8427,  1.0000], dtype=oneflow.float32)
>>> x = flow.tensor([[0, -1., 10.], [5, 7, 0.8]], dtype=flow.float32)
>>> out = flow.erf(x)
>>> out.shape
oneflow.Size([2, 3])
>>> out
tensor([[ 0.0000, -0.8427,  1.0000],
        [ 1.0000,  1.0000,  0.7421]], dtype=oneflow.float32)
>>> x = flow.tensor([[0, -1., 10.], [5, 7, 0.8], [2, 3, 4]], dtype=flow.float32)
>>> out = x.erf()
>>> out.shape
oneflow.Size([3, 3])
>>> out
tensor([[ 0.0000, -0.8427,  1.0000],
        [ 1.0000,  1.0000,  0.7421],
        [ 0.9953,  1.0000,  1.0000]], dtype=oneflow.float32)
oneflow.eye(n, m=None, dtype=flow.float, device=None, placement=None, sbp=None, requires_grad=False)Tensor

返回一个二维 tensor ,对角线上的元素为 1 ,其他元素为 0 。

参数:
  • n (int): 行数

  • m (Optional[int], 可选): 列数,如果为 None ,则默认与 n 相值。

关键词参数:
  • device (flow.device, 可选): 返回张量的所需设备。如果为 None ,则使用当前设备作为默认张量。

  • requires_grad (bool, 可选): 使用 autograd 记录对返回张量的操作。默认值: False

返回值:

oneflow.tensor: 对角线上为 1,其他地方为 0 的 tensor 。

示例:

>>> import oneflow as flow

>>> out = flow.eye(3, 3)
>>> out
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]], dtype=oneflow.float32)
oneflow.flatten(input, start_dim=0, end_dim=- 1)Tensor

将 tensor 指定连续范围的维度展平。

参数:
  • start_dim (int): 起始维度 (默认为 0)。

  • end_dim (int): 结束维度 (默认为 -1)。

示例:

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

沿指定维度 dims 反转 n-D 张量的顺序。

Note

有别于 NumPy 的算子 np.flip 在一定时间内返回一个 input 的视图, oneflow.flip 创建一个 input 的备份。因为创建 tensor 的备份所需的工作量比查看数据 oneflow.flipnp.flip 慢。

参数:
  • input (Tensor): 输入张量

  • dims (list 或者 tuple): 要翻转的维度

示例:

>>> import oneflow as flow

>>> input = flow.arange(0, 8, dtype=flow.float32).reshape(2, 2, -1)
>>> 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(input)Tensor

返回一个新 tensor ,其元素为对 input 向下取整的结果。

\[\text{out}_{i} = \lfloor \text{input}_{i} \rfloor\]
参数:

input (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([-0.5,  1.5, 0,  0.8], dtype=flow.float32)
>>> output = flow.floor(input)
>>> output.shape
oneflow.Size([4])
>>> output
tensor([-1.,  1.,  0.,  0.], dtype=oneflow.float32)

>>> input1 = flow.tensor([[0.8, 1.0], [-0.6, 2.5]], dtype=flow.float32)
>>> output1 = input1.floor()
>>> output1.shape
oneflow.Size([2, 2])
>>> output1
tensor([[ 0.,  1.],
        [-1.,  2.]], dtype=oneflow.float32)
oneflow.floor_()

函数 oneflow.floor() 的 In-place 版本。

oneflow.fmod(input, other)Tensor

计算逐元素余数。

被除数和除数可能同时包含整数和浮点数。余数与被除数 input 同号。

支持广播到通用形状、整数和浮点输入。

参数:
  • input (Tensor): 被除数

  • other (Tensor or Scalar): 除数

返回类型:

oneflow.tensor

示例:

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

从一个 numpy.ndarray 创建一个 Tensor

返回的 tensor 和 ndarray 共享相同的内存。对 tensor 的修改将反映在 ndarray 中,反之亦然。

它目前所接受 ndarray 的数据类型为 numpy.float64、numpy.float32、numpy.float16、numpy.int64、numpy.int32、numpy.int8、numpy.uint8。

例如:
>>> 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, value, dtype=None, device=None, placement=None, sbp=None, requires_grad=False)Tensor

创建并返回一个大小为 size ,其元素全部为 value 的 tensor。此 tensor 的数据类型与 value 相同。

参数:
  • size (int…): 列表,元组或者描述输出张量的整数 torch.Size

  • fill_value (Number): 用于填充输出张量的值

  • dtype (flow.dtype, 可选): 返回张量的数据类型

  • device (flow.device, 可选): 返回张量的所需设备。如果为 None ,使用当前设备

  • placement (flow.placement, 可选): 设置返回张量的 placement 属性。如果为 None ,则返回的张量是使用参数 device 的本地张量。

  • sbp (flow.sbp.sbp 或 flow.sbp.sbp 的元组, 可选): 返回的consistent tensor的所需 sbp 描述符。如果为 None ,则返回的张量是使用参数 device 的本地张量。

  • requires_grad (bool, 可选): 使用 autograd 记录对返回张量的操作。默认值: False

返回类型:

oneflow.tensor

示例:

>>> 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) # 构造 local tensor
>>> y
tensor([[5., 5., 5.],
        [5., 5., 5.]], dtype=oneflow.float32)
>>> placement = flow.placement("cpu", {0: [0]})
>>> y = flow.full((2,3),5.0, placement=placement, sbp=flow.sbp.broadcast)  # 构造 global tensor
>>> y.is_global 
True
oneflow.gather(input, dim, index, sparse_grad=False)Tensor

沿 dim 指定的维度收集值。

对 3-D tensor ,输出被定义为:

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

inputindex 的维度数必须相同。对于所有 d != dim 的维度 d , 必须有 index.size(d) <= input.size(d)out 的形状和 index 的形状相同。 请注意, inputindex 不会相互广播。

参数:
  • input (Tensor): 源张量

  • dim (int): 索引的维度

  • index (LongTensor): 要收集的元素的索引

示例:

>>> import oneflow as flow

>>> input = flow.randn(3, 4, 3, 5, dtype=flow.float32)
>>> index = flow.randint(0, 3, (3, 4, 3, 5))
>>> output = flow.gather(input, 1, index)
>>> output.shape
oneflow.Size([3, 4, 3, 5])
oneflow.gather_nd(input, index)Tensor

此接口与TensorFlow一致。文档参考自: https://www.tensorflow.org/api_docs/python/tf/gather_nd

该算子将来自 input 的切片汇聚成一个新的张量,由 index 定义新张量的形状。 .. math:

output[i_{0},i_{1},...,i_{K-2}] = input[index(i_{0},i_{1},...,i_{K-2})]
参数:
  • input: 输入张量

  • index: 切片索引

示例:

>>> import oneflow as flow
>>> input = flow.tensor([[1, 2,3], [4, 5,6],[7,8,9]], dtype=flow.float)
>>> index_1 = flow.tensor([[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([[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()

Gelu 激活算子,其公式为:

\[out = 0.5 * x * (1 + tanh(\sqrt{\frac{2}{\pi}} * (x + 0.044715x^{3})))\]
参数:

x (oneflow.tensor) - 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([-0.5, 0, 0.5], dtype=flow.float32)
>>> gelu = flow.nn.GELU()

>>> out = gelu(input)
>>> out
tensor([-0.1543,  0.0000,  0.3457], dtype=oneflow.float32)
oneflow.get_rng_state()

该文档引用自: https://pytorch.org/docs/1.10/generated/torch.get_rng_state.html

设置随机数生成器状态。

参数:
  • new_state (oneflow.ByteTensor) - 所需的状态

oneflow.gt(input, other)

返回 \(input > other\) 的 element-wise 真实值。

参数:
  • input (oneflow.tensor): 输入张量

  • other (oneflow.tensor): 输入张量

返回类型:

oneflow.tensor: 数据类型为 int8 的张量

示例:

>>> import oneflow as flow

>>> input1 = flow.randn(2, 6, 5, 3, dtype=flow.float32)
>>> input2 = flow.randn(2, 6, 5, 3, dtype=flow.float32)

>>> out = flow.gt(input1, input2).shape
>>> out
oneflow.Size([2, 6, 5, 3])
oneflow.hsplit()

将输入(一个或多个维度的张量)按照 indices_or_sections 水平地分割成多个张量。 每个分割都是输入的一个视图。 如果输入是一维的,就相当于调用 oneflow.tensor_split(input, indices_or_sections, dim=0) (分割的维度为0),如果输入有两个或更多维度,则相当于调用 oneflow.tensor_split(input, indices_or_sections, dim=1)(分割维度为1),但如果 indices_or_sections 是一个整数,它必须均匀地除以分割维度,否则会产生一个 Runtime Error。

该文档参考自: https://pytorch.org/docs/1.10/generated/torch.hsplit.html.

参数:
  • input (Tensor) - 输入张量。

  • indices_or_sections (int or a list) - 如果 indices_or_sections 是一个整数 n ,那么会沿着 dim 维度将输入分成 n 个部分。如果沿维度 dim 输入可被n整除,则每个部分的大小都相同。

    如果输入不能被 n 整除,那么第一个 int(input.size(dim)%n) 部分的大小为 int(input.size(dim) / n)+1,其余部分的大小为 int(input.size(dim) / n)。 如果 indices_or_sections 是一个 ints 的列表或元组,那么将输入沿着 dim 的维度在列表、元组或元组中的每个索引处进行分割。 例如,indices_or_sections=[2, 3],dim=0,将导致张量input[:2], input[2:3], and input[3:]的出现 。如果 indices_or_sections 是一个张量,它必须在 CPU 上必须是一个零维或一维的长张量。

返回类型:

oneflow.TensorTuple: 输出的 TensorTuple

示例:

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

目标是否在前 k 个预测中。

参数:
  • targets (Tensor): 数据类型为 int32 或 int64 的目标张量

  • predictions (Tensor): float32 类型的预测张量

  • k (int): 要查看计算精度的最大元素的数量

返回类型:

oneflow.tensor: 元素为 bool 的张量。k 处的计算精度作 bool 张量值。

示例:

>>> import oneflow as flow

>>> targets1 = flow.tensor([3, 1], dtype=flow.int32)
>>> predictions1 = flow.tensor([[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([3, 1], dtype=flow.int32, device=flow.device('cuda'))
>>> predictions2 = flow.tensor([[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(input, dim, index)Tensor

此接口与 PyTorch 一致。 文档参考自: https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch/#torchindex_select

沿指定维度 dim 选择值。

index 必须是一个 1-D 张量,数据类型为 Int32。 dim 必须在输入维度的范围内。 index 的值必须在输入的第 dim 维度的范围内。 注意 inputindex 不会互相广播。

参数:
  • input (Tensor): 源张量

  • dim (int): index 沿的维度

  • index (Tensor): 包含要索引的 index 的一维张量

示例:

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

用于启用或禁用 inference mode 的上下文管理。

Inference mode 是一个新的上下文管理模式,类似于 no_grad ,可以在你确定你的运算将与 autograd 没有关联时,可以使用这个模式。因为禁用了路径追踪和版本计数堆,在此模式下运行的代码将获得更好的性能。

此上下文管理模式位于本地线程;不会影响其他线程的计算。

同时可以作为一种修饰模式。(请确保使用括号实例化)

参数:
  • mode (bool): 标记启用或禁用 inference mode (默认: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

该文档引用自: https://pytorch.org/docs/1.10/_modules/torch/random.html.

返回用于生成随机数的初始种子,作为一个 Python long

oneflow.is_floating_point(input)boolean

如果 input 的数据类型是浮点数据类型,则返回 True 。浮点数据类型为 flow.float64、flow.float32 或者 flow.float16 。

参数:

input (Tensor): 输入张量

示例:

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

如果 grad 模式目前被启用,则返回 True

oneflow.is_nonzero(input)Tensor

如果 input 是转换类型后不等于值为 0 的单元素张量的张量 ( flow.tensor([0.]) 或者 flow.tensor([0]) )返回 True 。

报告 RuntimeError 如果 input.shape.numel()!=1

示例:

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

注意,这个函数只是在执行 isinstance(obj, Tensor) 。使用 isinstance 检查对 mypy 的类型更明确,所以建议使用该函数而不是 is_tensor

参数:

obj (Object): 被测试的对象。

示例:

>>> import oneflow as flow

>>> x=flow.tensor([1,2,3])
>>> flow.is_tensor(x)
True
oneflow.isinf()

测试输入的每个元素是否为 infinite (正或负的 infinite)

参数:
  • input (Tensor) - 输入张量。

返回类型:

一个布尔类型的张量,当输入为 infinite 为 True,否则为 False。

示例:

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

返回一个带有布尔元素的新张量,表示输入的每个元素是否为 NaN。

参数:
  • input (Tensor) - 输入张量。

返回类型:

一个布尔张量,在输入为 NaN 的情况下为 True,否则为 False。

示例:

>>> import oneflow as flow
>>> flow.isnan(flow.tensor([1, float('nan'), 2]))
tensor([False,  True, False], dtype=oneflow.bool)
oneflow.le(input, other)Tensor

返回 \(input <=other\) 的 element-wise 真实值。

参数:
  • input (oneflow.tensor): 输入张量

  • other (oneflow.tensor): 输入张量

返回类型:

oneflow.tensor: 数据类型为 bool 的张量

示例:

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

从应该由 oneflow.nn.OneRecReader 之前生成的输入中解码张量。

参数:
  • input: (Tensor) - 之前由 oneflow.nn.OneRecReader 生成的张量。

  • key (str) - 要解码的张量的字段名称。

  • shape (bool) - 要解码的张量的形状。

  • is_dynamic (bool) - 张量形状是否是动态的。

  • reshape (tuple) - 设置该参数重塑张量。

  • batch_padding (tuple) - 设置该参数进行批量填充。

示例:

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.load(path: str, global_src_rank: Optional[int] = None)Any

加载一个被 oneflow.save() 保存的对象。

参数:
  • path (str): 对象所在的路径

  • global_src_rank (int, optional): 加载全局 (global) 张量的需要的秩 (rank)。被指定时,只有秩与 global_src_rank相等的进程才会真正读取 path 中的文件,并且被加载对象中的张量会与 placement = flow.placement(‘cuda’, [global_src_rank]) 全局。

返回类型:

加载好的对象

oneflow.log(x)Tensor

返回一个新 tensor 包含 x 中元素的自然对数。 公式为:

\[y_{i} = \log_{e} (x_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.randn(2, 3, 4, 5, dtype=flow.float32)
>>> output = flow.log(input)
oneflow.log1p(x)Tensor

返回一个新的 tensor,其自然对数的公式为 (1 + x)。

\[\text{out}_{i}=\log_e(1+\text{input}_{i})\]
参数:

x (Tensor): 张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x = flow.tensor([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

返回一个新的张量,张量中的元素是 input 以2为底的自然对数。

\[y_{i} = log2_{e} (x_{i})\]
参数:
  • input (Tensor) - 输入张量。

示例:

>>> 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(input, other)Tensor

计算给定的输入 tensor 的逐元素逻辑 AND 。 值为 0 的元素被视作 False ,非 0 元素被视作 True

参数:
  • input (oneflow.tensor): 输入张量

  • other (oneflow.tensor): 计算 and 逻辑的另一个张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input1 = flow.tensor([1, 0, 1], dtype=flow.float32)
>>> input2 = flow.tensor([1, 1, 0], dtype=flow.float32)

>>> out = flow.logical_and(input1, input2)
>>> out
tensor([ True, False, False], dtype=oneflow.bool)
oneflow.logical_not()

计算给定输入张量的元素的逻辑非。零被视为假,非零被视为真。

参数:
  • input (oneflow.Tensor): 输入张量。

  • other (oneflow.Tensor): 与之计算逻辑非的张量。

返回类型:

oneflow.Tensor: 输出张量

示例:

>>> 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(input, other)Tensor

计算给定的输入 tensor 的逐元素逻辑 OR 。 值为 0 的元素被视作 False ,非 0 元素被视作 True

参数:
  • input (oneflow.tensor): 输入张量

  • other (oneflow.tensor): 计算 or 逻辑的另一个张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input1 = flow.tensor([1, 0, 1], dtype=flow.float32)
>>> input2 = flow.tensor([1, 0, 0], dtype=flow.float32)

>>> out = flow.logical_or(input1, input2)
>>> out
tensor([ True, False,  True], dtype=oneflow.bool)
oneflow.logical_slice(input, slice_tup_list: Sequence[Tuple[int, int, int]])

从 global tensor 中提取切片。 slice_tup_list 指定了每个维度的分片索引,格式是(start, stop, step)。 该算子将根据 slice_tup_list 对张量进行分割。

参数:
  • input (Tensor) - 输入张量

  • slice_tup_list - 分片元组列表,表示每个维度分片 (start, stop, step)。

示例:

>>> import oneflow as flow

>>> placement = flow.placement("cpu", ranks=[0])
>>> x = flow.Tensor([[1, 2], [3, 4]], placement=placement, sbp=flow.sbp.broadcast)
>>> y = flow.logical_slice(x, slice_tup_list=[[0, 1, 1]])
>>> y.numpy()
array([[1., 2.]], dtype=float32)
oneflow.logical_xor(input, other)Tensor

计算给定的输入 tensor 的逐元素逻辑 XOR 。 值为 0 的元素被视作 False ,非 0 元素被视作 True

参数:
  • input (oneflow.tensor): 输入张量

  • other (oneflow.tensor): 计算 xor 逻辑的另一个张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input1 = flow.tensor([1, 0, 1], dtype=flow.float32)
>>> input2 = flow.tensor([1, 0, 0], dtype=flow.float32)
>>> out = flow.logical_xor(input1, input2)
>>> out
tensor([False, False,  True], dtype=oneflow.bool)
oneflow.lt(input, other)Tensor

返回 \(input < other\) 的 element-wise 真实值。

参数:
  • input (oneflow.tensor): 输入张量

  • other (oneflow.tensor): 输入张量

返回类型:

oneflow.tensor: 数据类型为 int8 的张量

示例:

>>> import oneflow as flow

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

>>> out = flow.lt(input1, input2)
>>> out
tensor([False, False,  True], dtype=oneflow.bool)
oneflow.manual_seed(seed)

该文档引用自: https://pytorch.org/docs/1.10/generated/torch.manual_seed.html

设置生成随机数的种子。返回一个 oneflow.Generator 对象。

参数:
  • seed (int) - 所需的 seed。该值必须在以下范围内 [ -0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff ] 。否则,会产生一个 RuntimeError,负的输入被重新映射为正值,公式为 0xffff_ffff_ffff_ffff + seed

oneflow.masked_fill(input, mask, value)Tensor

如果参数 mask 为 True ,则在 input 中填充 value 。 参数 mask 的形状必须能被广播为 input 的形状。

参数:
  • input (Tensor): 被填充的张量

  • mask (BoolTensor): 决定是否填充的 boolean 张量

  • value (float): 要填充的值

示例:

>>> import oneflow as flow

>>> fill_value = 8.7654321 # random value e.g. -1e9 3.1415
>>> input = flow.tensor([[[-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]]], dtype=flow.float32)
>>> mask = flow.gt(input, 0)
>>> output = flow.masked_fill(input, mask, fill_value)
>>> output
tensor([[[-0.1317,  8.7654,  8.7654,  8.7654],
         [-1.5195,  8.7654, -0.5330,  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)Tensor

返回一个新的 tensor ,其元素为依据 mask 的真实值在 input 中索引的元素。

mask 是一个 BoolTensor (在 oneflow 中, BoolTensor 被替换为 Int8Tensor )

参数 mask 的形状必须能被广播为 input 的形状。

参数:
  • input (Tensor): 输入张量

  • mask (Tensor): 包含要索引的二进制掩码的张量

示例:

>>> import oneflow as flow

>>> input = flow.tensor([[-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(a, b)Tensor

此运算符将矩阵乘法应用于两个 Tensor ab

参数:
  • a (oneflow.tensor): 张量

  • b (oneflow.tensor): 张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input1 = flow.randn(2, 6, dtype=flow.float32)
>>> input2 = flow.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)Tensor

返回 input 中的最大值。

参数:
  • input (oneflow.tensor): 输入张量

  • dim (int, optional): 要进行计算的维度。默认: None

  • keepdim (bool, optional): 输出张量是否保留 input 的维度。默认: False

返回类型:

张量或元组(oneflow.tensor, oneflow.tensor(dtype=int64)): 如果参数 dimNone ,返回 input 所有元素中的最大值。如果 dim 不是 None , 则返回一个包含张量的元组(values, indices), valuesinput 当前维度的最大值, indices 是最大值在 input 中当前维度的索引。

示例:

>>> 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.mean(input, dim=None, keepdim=False)Tensor

计算给定维度上张量中各行元素的均值,如果 dim 为 None ,则计算所有元素的均值。

参数:
  • input (oneflow.tensor): 输入张量

  • dim (int, optional): 要进行计算的维度。默认: None

  • keepdim (bool, optional): 输出张量是否保留 input 的维度。默认: False

示例:

>>> 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) where values contains the median of each row of input in the dimension dim, and indices contains the index of the median values found in the dimension dim.

By default, dim is the last dimension of the input tensor.

If keepdim is True, the output tensors are of the same size as input except in the dimension dim where they are of size 1. Otherwise, dim is squeezed (see flow.squeeze()), resulting in the outputs tensor having 1 fewer dimension than input.

Note

The median is not unique for input tensors with an even number of elements in the dimension dim. 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)sequence of Tensors

此接口与 PyTorch 一致。 文档参考自: https://pytorch.org/docs/stable/_modules/torch/functional.html#meshgrid

\(N\) 个 tensor ,可以是标量或者 1 维张量,并创建 \(N\) 个 N-D 网格, 第 i 个网格是通过扩展第 i 个输入并且其维度由其他的输入决定。

参数:

tensors (list of Tensor): 标量或一维张量列表。标量将被自动视为大小为 \((1,)\) 的张量

返回类型:

seq (sequence of Tensors): 如果输入有 \(k\) 个张量,大小分别为 \((N_1,), (N_2,), ... , (N_k,)\) , 则输出也有 \(k\) 个张量,其中所有张量的大小为 \((N_1, N_2, ... , N_k)\)

示例:

>>> import oneflow as flow

>>> input1 = flow.tensor([1, 2, 3], dtype=flow.float32)
>>> input2 = flow.tensor([4, 5, 6], dtype=flow.float32)
>>> of_x, of_y = flow.meshgrid(input1, input2)
>>> of_x
tensor([[1., 1., 1.],
        [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)Tensor

返回 input 中的最小值。

参数:
  • input (oneflow.tensor): 输入张量

  • dim (int, optional): 要进行计算的维度。默认: None

  • keepdim (bool, optional): 输出张量是否保留 input 的维度。默认: False

返回类型:

张量或元组(oneflow.tensor, oneflow.tensor(dtype=int64)): 如果参数 dimNone ,返回 input 所有元素中的最小值。如果 dim 不是 None , 则返回一个包含张量的元组(values, indices), valuesinput 当前维度的最小值, indices 是最小值在 input 中当前维度的索引。

示例:

>>> 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.mish(x: Tensor)Tensor

逐元素地应用此公式:

\[\text{mish}(x) = x * \text{tanh}(\text{softplus}(x))\]

示例:

>>> import oneflow as flow

>>> input = flow.tensor([1, 2, 3], dtype=flow.float32)

>>> out = flow.mish(input)
>>> out
tensor([0.8651, 1.9440, 2.9865], dtype=oneflow.float32)

更多细节参考 Mish

oneflow.movedim()

source 中的的维度移动到 destination 的位置。 其他没有明确移动的 source 维度保持原来的顺序,并出现在 destination 未指定的位置上。

该文档引用自: https://pytorch.org/docs/1.10/generated/torch.movedim.html

参数:
  • input (Tensor) -输入张量。

  • source (int or a list) - 要移动的数据的原始位置。这必须是唯一的。

  • destination (int or a list) - 要移动的数据的目标位置。这必须是唯一的。

返回类型:

oneflow.Tensor: 输出张量

示例:

>>> 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(input, other)Tensor

计算 inputother 相乘,支持 element-wise、标量和广播形式的乘法。

公式为:

\[out = input \times other\]
参数:
  • input (Tensor): 输入张量。

  • other (Tensor): 输入张量。

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

# element-wise 乘法
>>> input = flow.randn(2, 3, dtype=flow.float32)
>>> other = flow.randn(2, 3, dtype=flow.float32)
>>> out = flow.mul(input,other)
>>> out.shape
oneflow.Size([2, 3])

# 标量乘法
>>> input = 5
>>> other = flow.randn(2, 3, dtype=flow.float32)
>>> out = flow.mul(input,other)
>>> out.shape
oneflow.Size([2, 3])

# 广播乘法
>>> input = flow.randn(1, 1, dtype=flow.float32)
>>> other = flow.randn(2, 3, dtype=flow.float32)
>>> out = flow.mul(input,other)
>>> out.shape
oneflow.Size([2, 3])
oneflow.narrow(x, dim, start, length)Tensor

返回一个 x 的缩小版本张量。其形状为在 dim 维度上从 start 开始到 start + length

参数:
  • x : 要缩小的张量

  • dim : 要去进行缩小的维度

  • start : 起始维度

  • length : 到结束维度的距离

返回类型:

oneflow.tensor

示例:

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

计算 element-wise 的不等性。 第二个参数是一个可以与第一个参数广播的数字或张量。

参数:
  • input (oneflow.Tensor): 需要比较的对象。

  • other (oneflow.Tensor, float 或者 int): 与输入比较的张量。

返回:

一个布尔值构成的张量,当 input 不等于 other 时为真,否则为假。

示例:

>>> import oneflow as flow

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

>>> y = flow.ne(input, other)
>>> y
tensor([False, False, False,  True], dtype=oneflow.bool)
oneflow.neg()

negative(input) -> Tensor

返回 input 的负值。

参数:
  • input (oneflow.tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

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

返回 input 的负值。

参数:
  • input (oneflow.tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([1.0, -1.0, 2.3], 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

返回一个大小为 1 的张量,默认情况下,返回的张量具有与此张量相同的 oneflow.dtype 和 oneflow.device。

参数:
  • size (int…) - 一个 list, tuple, 或者 flow,定义输出张量形状的大小。

  • dtype (flow.dtype, optional) - 返回张量的所需类型。默认值:如果为 None,则与此张量相同的 flow.dtype。

  • device (flow.device, optional) - 返回张量的所需设备。默认值:如果为 None,则与此张量相同的 flow.device。

  • placement (flow.placement, optional) - 返回的全局张量的期望位置。默认值:如果为 None,则返回的张量是使用参数 device 的本地张量。

  • sbp (flow.sbp.sbp or tuple of flow.sbp.sbp, optional) - 返回的全局张量所需的 sbp 描述符。默认值:如果为 None,则返回的张量是使用参数device的本地张量

  • requires_grad (flow.bool, optional) - 如果 autograd 在返回的张量上记录操作。默认值:False。 >>> import numpy as np

示例:

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

根据盒子的交叉联合(IoU),对盒子执行非最大抑制(NMS)。

NMS迭代地删除那些与另一个(高分)盒子的 IoU 大于 iou_threshold 的低分盒子。

参数:
  • boxes (Tensor[N, 4]) - 要执行 NMS 的盒子。它们应该是 (x1, y1, x2, y2) 格式,0 <= x1 < x20 <= y1 < y2

  • scores (Tensor[N]) - 每个盒子的分数

  • iou_threshold (float): 丢弃所有 IoU > iou_threshold 的重叠框。

返回类型:

tensor: int64张量,包含被 NMS 保留的元素的索引,按照分数递减的顺序排序

class oneflow.no_grad

禁用梯度计算的上下文管理器。

当确定不调用 Tensor.backward() 时,禁用梯度计算对于推理很有效,此操作相比 requires_grad=True 时可以减少计算的内存消耗。

此模式下,任何计算都会视 requires_grad 为 False ,即便输入有 requires_grad=True。

此上下文管理器只会影响本地线程,不会影响其他线程的运算。

也起到装饰器的作用。 (请确保用括号进行实例化)

>>> 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, as_tuple=False)Tensor or tuple of Tensors

Note

as_tuple 默认为 False 时,返回一个 2-D tensor,每一行是一个非 0 值的索引(index)。

as_tupleTrue 时,返回一个包含 1-D 索引张量的元组,允许高级索引, 因此 x[x.nonzero(as_tuple=True)] 给出 x 的所有非 0 值。在返回的元组中,每个 索引张量包含特定维度的非 0 元素的索引。

有关这两种情况的更多详细信息,请参见下文。

as_tuple False 时(默认)

返回一个 tensor 包含所有 input 中非 0 元素的索引(index)。结果中的每一行包含 input 中一个 非 0 元素的索引,结果按字典顺序排序,对最后一个索引的改变最快(C-style)。

如果 input 的维度数为 \(n\) ,则结果索引张量 out 的形状为 \((z \\times n)\)\(z\)input 中非 0 元素的数量。

as_tuple True

返回一个包含 1-D 张量的元组,每个张量分别对应 input 的每个维度,并包含 input 当前维度中所有非 0 元素的索引。

如果 input 的维度数为 \(n\) ,则此返回元组包含 \(n\) 个大小为 \(z\) 的张量, \(z\)input 中非 0 元素的总数。

有一种特殊情况是,当 input 是 0 维张量并且有一个非 0 的标量值,则被视为一个只有一个元素的 1 维张量

参数:

input (Tensor): 输入张量

关键词参数:

out (Tensor, optional): 包含索引的输出张量

返回类型:

如果 as_tupleFalse ,则返回 oneflow.tensor ,其元素为 input 中的索引。 如果 as_tupleTrue ,则返回包含 oneflow.tensor 的元组, 每个张量包含非 0 元素在当前维度的索引。

示例:

>>> 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: Union[int, Tuple[int, ], oneflow.Size, List[int]], out=None, placement: Optional[oneflow._oneflow_internal.placement] = None, sbp: Optional[oneflow._oneflow_internal.sbp.sbp] = None, generator=None, dtype: Optional[oneflow._oneflow_internal.dtype] = None, device: Optional[Union[str, oneflow._oneflow_internal.device]] = None, requires_grad: bool = False)

返回一个从独立的正态分布中抽取的随机数的张量,其平均值和标准差已给定。

参数:
  • mean (float) - 所有分布的均值。

  • std (float) - 所有分布的方差。

  • size (int…) - 一系列定义输出张量形状的整数。

关键词参数:
  • out (Tensor, 可选) - 用于采样的伪随机数生成器。

  • placement (flow.placement,可选) - 返回的全局张量的所需设备。如果为 None,将构建局部张量。

  • sbp (flow.sbp, 可选) - 返回的全局张量所需的 sbp。它必须与 placement 的数量相等。

  • generator (oneflow.Generator, 可选) - 用于采样的伪随机数生成器。

  • dtype (oneflow.dtype, 可选) - 返回张量的所需数据类型。默认使用当前设备。

  • device - 返回张量的所需设备。默认值:CPU。

  • requires_grad (bool, 可选) - 如果 autograd 应该在返回的张量上记录操作。默认值:False。

示例:

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

返回 input 张量中的元素总数量。

参数:
  • input (oneflow.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, dtype=None, device=None, placement=None, sbp=None, requires_grad=False)Tensor

返回一个元素全部为标量 1 ,形状由参数 size 决定的 tensor。

参数:
  • size (一个整数或包含整数的元组)): 决定输出张量的形状,可以是数字变量或集合例如列表或元组

  • dtype (flow.dtype, 可选): 返回张量的数据类型

  • device (flow.device, 可选): 返回的本地张量的所需设备。默认使用当前设备

  • placement (flow.placement, 可选): 设置返回张量的 placement 属性。如果为 None,则构造 local tensor

  • sbp (flow.sbp.sbp 或包含 flow.sbp.sbp 的元组, 可选): 返回的global tensor的所需 sbp 描述符。如果为 None ,则返回的张量是使用参数 device 的本地张量

  • requires_grad (bool, 可选): 用 autograd 记录对返回张量的操作,默认为 False

示例:

>>> import oneflow as flow
>>> y = flow.ones(5)
>>> y
tensor([1., 1., 1., 1., 1.], dtype=oneflow.float32)
>>> y = flow.ones(2,3) # 构造 local tensor
>>> y
tensor([[1., 1., 1.],
        [1., 1., 1.]], dtype=oneflow.float32)
>>> placement = flow.placement("cpu", {0: [0]})
>>> y = flow.ones(4, 5, placement=placement, sbp=flow.sbp.broadcast) # 构造 global tensor
>>> y.is_global
True
oneflow.ones_like(x)Tensor

返回一个元素全部为值为 1 的标量,且形状与 x 相同的 tensor。 flow.ones_like(x) 等价于 flow.ones(x.shape, dtype=x.dtype)

参数:

x (Tensor): 输入的形状将会决定输出的形状

示例:

>>> import oneflow as flow
>>> x = flow.randn(5, dtype=flow.float32)
>>> y = flow.ones_like(x)
>>> y
tensor([1., 1., 1., 1., 1.], dtype=oneflow.float32)
oneflow.permute(input, *dims)Tensor

返回原张量的维度换位后的 view 。

参数:
  • dims (int 的元组 ) - 需要的维度排序

示例:

>>> import oneflow as flow

>>> input = flow.rand(2, 6, 5, 3)
>>> output = flow.permute(input, (1, 0, 2, 3)).shape
>>> output
oneflow.Size([6, 2, 5, 3])
oneflow.pow(input, exponent)Tensor

返回一个Tensor, 其元素为用 exponent 计算 input 中 每​​个元素的幂。exponent 可以是单个浮点数,整数或者与 input 具有相同形状的 tensor。

当指数是标量时,操作为:

\[\text{out}_i = x_i ^ \text{exponent}\]

当指数是张量时:

\[\text{out}_i = x_i ^ {\text{exponent}_i}\]
参数:
  • input (Tensor): 输入张量

  • exponent (int, float, Tensor): 指数

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x = flow.tensor([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([1.0, 2.0, 3.0, 4.0], dtype=flow.float32)
>>> y = flow.tensor([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

在给定维度上计算 input 中每行中元素的乘积,并返回一个包含结果的新 tensor 。

注意: 如果参数 dim 为 None ,返回一个只有一个元素的 tensor ,其元素为 input 中所有数之积

参数:
  • input (Tensor): 输入源张量

  • dim (int): 要做乘法的维度

示例:

>>> 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, out=None, generator=None, dtype=None, layout=None, device=None, placement=None, sbp=None, requires_grad=False)Tensor

返回一个由在区间 [0, 1) 上均匀分布的随机数填充的新 tensor 。

输出 tensor 的形状由变量 size 决定。

参数:
  • size (int… 或 oneflow.Size): 定义输出张量的形状。可以是数字变量,或者集合例如列表或元组,或者 oneflow.Size

  • out (可选): 输出张量

  • dtype (flow.dtype, 可选): 返回张量的数据类型。默认: flow.float32

  • layout (可选): 返回的 Tensor 的 layout。

  • generator (flow.Generator, 可选): 用于采样的伪随机数生成器

  • device (flow.device, 可选): 返回的本地张量的所需设备。默认使用当前设备

  • placement (flow.placement, 可选): 设置返回张量的 placement 属性。如果为None,则构造 local tensor

  • sbp (flow.sbp, 可选): 返回的consistent tensor的所需 sbp 描述符。必须和 placement 的数量相等

  • requires_grad (bool, 可选): 用 autograd 记录对返回张量的操作,默认为 False

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow
>>> x = flow.rand(3,3) # 构造 local tensor
>>> x.shape
oneflow.Size([3, 3])
>>> x.is_global
False
>>> placement = flow.placement("cpu", {0: [0]})
>>> sbp = flow.sbp.broadcast
>>> x = flow.rand(3, 3, placement=placement, sbp=sbp) # 构造 global tensor
>>> x.is_global
True
oneflow.randint(low, high, size, out=None, generator=None, dtype=None, layout=None, device=None, placement=None, sbp=None, requires_grad=False)Tensor

返回一个张量,其中填充了在 low (包括)和 high (不包括)之间均匀生成的随机整数。

输出 tensor 的形状由变量 size 决定。

参数:
  • low (flow.int64): 返回张量中的最小值(包括)

  • high (flow.int64): 返回张量中的最大值(不包括)

  • size (int… 或 oneflow.Size): 定义输出张量的形状。可以是数字变量,或者集合例如列表或元组,或者 oneflow.Size

关键词参数:
  • out (可选): 输出张量

  • dtype (flow.dtype, 可选): 返回张量的数据类型。默认: flow.float32

  • layout (可选): 返回的 Tensor 的 layout。

  • generator (flow.Generator, 可选): 用于采样的伪随机数生成器

  • device (flow.device, 可选): 返回的本地张量的所需设备。默认使用当前设备

  • placement (flow.placement, 可选): 设置返回张量的 placement 属性。如果为None,则构造 local tensor

  • sbp (flow.sbp, 可选): 返回的consistent tensor的所需 sbp 描述符。必须和 placement 的数量相等

  • requires_grad (bool, 可选): 用 autograd 记录对返回张量的操作,默认为 False

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow
>>> generator = flow.Generator()
>>> generator.manual_seed(0)
>>> y = flow.randint(0, 5, (3,3), generator=generator) # 构造 local tensor
>>> y
tensor([[2, 2, 3],
        [4, 3, 4],
        [2, 4, 2]], dtype=oneflow.int64)
>>> y.is_global
False
>>> placement = flow.placement("cpu", {0: [0]})
>>> y = flow.randint(0, 5, (3,3), generator=generator, placement=placement, sbp=flow.sbp.broadcast) # 构造 global tensor
>>> y.is_global
True
oneflow.randn(*size, out=None, generator=None, dtype=None, layout=None, device=None, placement=None, sbp=None, requires_grad=False)Tensor

返回一个由符合期望为 0 ,方差为 1 的正态分布的随机数填充的新 tensor 。

输出 tensor 的形状由变量 size 决定。

参数:
  • size (int… 或 oneflow.Size): 定义输出张量的形状。可以是数字变量,或者集合例如列表或元组,或者 oneflow.Size

关键词参数:
  • out (可选): 输出张量

  • dtype (flow.dtype, 可选): 返回张量的数据类型。默认: flow.float32

  • layout (可选): 返回的 Tensor 的 layout。

  • generator (flow.Generator, 可选): 用于采样的伪随机数生成器

  • device (flow.device, 可选): 返回的本地张量的所需设备。默认使用当前设备

  • placement (flow.placement, 可选): 设置返回张量的 placement 属性。如果为None,则构造 local tensor

  • sbp (flow.sbp, 可选): 返回的consistent tensor的所需 sbp 描述符。必须和 placement 的数量相等

  • requires_grad (bool, 可选): 用 autograd 记录对返回张量的操作,默认为 False

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow
>>> x = flow.randn(3,3) # 构造 local tensor
>>> x.shape
oneflow.Size([3, 3])
>>> x.is_global
False
>>> placement = flow.placement("cpu", {0:[0]})
>>> sbp = flow.sbp.broadcast
>>> x = flow.randn(3,3,placement=placement,sbp=sbp) # 构造 global tensor
>>> x.is_global
True
oneflow.randperm(n, generator=None, out=None, dtype=None, layout=None, device=None, placement=None, sbp=None, requires_grad=False, pin_memory=False)Tensor

返回从 0n - 1 (不包括)的整数的随机排列。

参数:
  • n (int): 最大值(不包括)

返回类型:

oneflow.tensor

关键词参数:
  • generator (flow.Generator, 可选): 用于采样的伪随机数生成器

  • out (可选): 输出张量

  • dtype (flow.dtype, 可选): 返回张量的数据类型。默认: flow.float32

  • layout (可选): 返回的 Tensor 的 layout。

  • device (flow.device, 可选): 返回的本地张量的所需设备。默认使用当前设备

  • placement (flow.placement, 可选): 设置返回张量的 placement 属性。如果为None,则构造 local tensor

  • sbp (flow.sbp, 可选): 返回的consistent tensor的所需 sbp 描述符。必须和 placement 的数量相等

  • requires_grad (bool, 可选): 用 autograd 记录对返回张量的操作,默认为 False

  • pin_memory (bool, 可选):目前不支持 pin_memory

示例:

>>> import oneflow as flow
>>> generator = flow.Generator()
>>> generator.manual_seed(0)
>>> y = flow.randperm(5, generator=generator) # 构造 local tensor
>>> y
tensor([2, 4, 3, 0, 1], dtype=oneflow.int64)
>>> y.is_global
False
>>> placement = flow.placement("cpu", {0: [0]})
>>> y = flow.randperm(5, generator=generator, placement=placement, sbp=flow.sbp.broadcast) # 构造 global tensor
>>> y.is_global
True
oneflow.reciprocal(x)Tensor

计算 x 的倒数,如果 x 为0,倒数将被设置为0。

参数:

x (Tensor): 输入张量。

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x = flow.tensor([[1, 2, 3], [4, 5, 6]], dtype=flow.float32)
>>> out = flow.reciprocal(x)
>>> out
tensor([[1.0000, 0.5000, 0.3333],
        [0.2500, 0.2000, 0.1667]], dtype=oneflow.float32)
oneflow.repeat(input, *sizes)Tensor

沿指定维度通过重复使 input 尺寸变大,并返回。

参数:
  • x (oneflow.tensor): 输入张量

  • *size (flow.Size 或 int): 沿每个维度重复的次数

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([[[[0, 1]],
...                       [[2, 3]],
...                       [[4, 5]]]], dtype=flow.int32)
>>> out = input.repeat(1, 1, 2, 2)
>>> out
tensor([[[[0, 1, 0, 1],
          [0, 1, 0, 1]],

         [[2, 3, 2, 3],
          [2, 3, 2, 3]],

         [[4, 5, 4, 5],
          [4, 5, 4, 5]]]], dtype=oneflow.int32)
oneflow.reshape(input, shape=None)Tensor

返回一个新张量,此张量的内容为输入 input,形状为指定的 shape

我们可以将 shape 中的某一个维度设置为 -1 ,算子会自动推断出完整的形状。

参数:
  • input: 输入张量

  • shape: 输出张量的形状

返回类型:

oneflow.tensor: 数据类型与 input 相同的张量

示例:

>>> import oneflow as flow

>>> input = flow.tensor(
...    [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], dtype=flow.float32
... )

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

将张量根据指定维度滚动。

超出最后一个位置的元素将在第一个位置重新引入。

如果维度没有被指定,张量将在滚动前被扁平化,然后被恢复至原形状。

参数:
  • input (oneflow.Tensor) - 输入张量

  • shifts (int or int 元组 ) - 张量元素移动的位置数。如果 shifts 是一个元组,则 dims 必须也是一个相同大小的元组,且每个维度将会根据对应的值滚动。

  • dims (int or tuple of python:ints) - 滚动所沿的维度

返回:

oneflow.Tensor: 结果张量。

示例:

>>> import oneflow as flow
>>> x = flow.tensor([[1, 2],
...               [3, 4],
...               [5, 6],
...               [7, 8]], dtype=flow.float32)
>>> 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(x)

返回一个新 tensor,其元素为 x 中元素四舍五入到整数。

参数:

x (oneflow.tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x1 = flow.tensor([1.49999, 1.500001, 2.7], dtype=flow.float32)
>>> out1 = flow.round(x1)
>>> out1
tensor([1., 2., 3.], dtype=oneflow.float32)
>>> x2 = flow.tensor([2.499999, 7.5000001, 5.3, 6.8], dtype=flow.float32)
>>> out2 = flow.round(x2)
>>> out2
tensor([2., 8., 5., 7.], dtype=oneflow.float32)
oneflow.rsqrt(input)Tensor

返回一个新的张量,它的元素是 input 的每个元素的平方根的倒数。

\[\text{out}_{i} = \frac{1}{\sqrt{\text{input}_{i}}}\]
参数:
  • input (Tensor): 输入张量

示例:

>>> import oneflow as flow

>>> a = flow.tensor([1.0, 2.0, 3.0], dtype=flow.float32)
>>> out = flow.rsqrt(a)
>>> out
tensor([1.0000, 0.7071, 0.5774], dtype=oneflow.float32)
oneflow.save(obj: Any, path: Union[str, pathlib.Path], global_dst_rank: Optional[int] = None)None

保存一个对象到一个路径。

参数:
  • obj: 被保存的对象

  • path (str): 对象被保存的路径

  • global_dst_rank (int, 可选): 用于保存全局张量的地点秩。被指定时,对于所有张量,只有秩 == global_src_rank 的进程被保存,而其他的进程不会进行任何磁盘存取。

oneflow.scatter(input, dim, index, src)Tensor

src 中由 index 指定的元素沿维度 dim 写入 input 中。

以 3-D tensor 为例,输出被指定为:

input[index[i][j][k]][j][k] = src[i][j][k]  # 当 dim == 0 时
input[i][index[i][j][k]][k] = src[i][j][k]  # 当 dim == 1 时
input[i][j][index[i][j][k]] = src[i][j][k]  # 当 dim == 2 时

inputindexsrc (若为 Tensor )的维度数必须相同。并且在每个维度 d 上, index.shape(d) <= src.shape(d) 。当维度 d != dim 时, index.shape(d) <= self.shape(d) 。 注意 indexsrc 不广播。

参数:
  • input (Tensor): 输入张量

  • dim (int): 要索引的维度

  • index (Tensor): 要写入的元素的索引张量

  • src (Tensor or float): 写入的元素的源张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.ones((3,5))*2
>>> index = flow.tensor([[0,1,2],[0,1,4]], dtype=flow.int32)
>>> src = flow.tensor([[0.,10.,20.,30.,40.],[50.,60.,70.,80.,90.]], dtype=flow.float32)
>>> 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)Tensor

src 中由 index 指定的元素沿维度 diminput 做加法。

以 3-D tensor 为例,输出被指定为:

input[index[i][j][k]][j][k] += src[i][j][k]  # 当 dim == 0 时
input[i][index[i][j][k]][k] += src[i][j][k]  # 当 dim == 1 时
input[i][j][index[i][j][k]] += src[i][j][k]  # 当 dim == 2 时
参数:
  • input (Tensor): 输入张量

  • dim (int): 要索引的维度

  • index (Tensor): 要做加法的元素的索引张量

  • src (Tensor or float): 加数的源张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.ones((3,5))*2
>>> index = flow.tensor([[0,1,2],[0,1,4]], dtype=flow.int32)
>>> src = flow.tensor([[0,10,20,30,40],[50,60,70,80,90]], dtype=flow.float32)
>>> 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)Tensor

此接口与 TensorFlow 保持一致,更多信息请参考 https://www.tensorflow.org/api_docs/python/tf/scatter_nd 。 依据 shape 创建一个新的元素皆为 0 的 tensor ,并根据 index 在新的 tensor 中插入 update 的元素。

参数:
  • index (Tensor): 在新 tensor 插入 update 的元素时的索引张量。数据类型应为 oneflow.int

  • update (Tensor): 源张量

  • shape (Sequence[int]): 常数张量形状,常数张量元素均为 0 。

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow
>>> index = flow.tensor([[1], [6], [4]], dtype=flow.int)
>>> update = flow.tensor([10.2, 5.1, 12.7], dtype=flow.float32)
>>> 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

该文档引用自:https://pytorch.org/docs/1.10/generated/torch.searchsorted.html?highlight=searchsorted

从 sorted_sequence 的最内层维度中找到索引,如果在这些索引之前插入 values 中的相应值,那么 sorted_sequence 中相应的最内层维度的顺序将被保留下来。返回一个大小与 value 相同的新张量。如果 right 是 False(默认),那么 sorted_sequence 的左边边界将被关闭。即,返回的索引满足以下规则:

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]

参数:
  • sorted_sequence (Tensor) - N-D 或1-D 张量,包含最内侧维度上的单调增加序列。

  • values (Tensor or Scalar) - N-D 张量或包含搜索值的标量。

  • out_int32 (bool 可选) - 标明输出数据类型。如果为 True,则为 torch.int32,否则为 torch.int64。默认值为 False,即默认输出数据类型为 torch.int64。

  • right (bool 可选) - 如果是 False,返回找到的第一个合适的位置。如果为 True,则返回最后一个索引。如果没有找到合适的索引,返回 0 作为非数字值 (例如:Nan, inf),或者在 sorted_sequence 中的最内层维度的大小(一个传递最内层维度的最后一个索引)。则如果是 False,则得到获得每个值的下限索引,这些值是在 sorted_sequence 的相应最内层维度上的每个值的下限索引。如果是 True,则获得上界的索引。默认值是 False。

示例:

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

该文档引用自: https://pytorch.org/docs/1.10/generated/torch.seed.html.

将生成随机数的种子设置为一个非决定性的随机数。返回一个用于 RNG 种子的 64 位数字。

oneflow.select()

将原张量根据给定的索引在指定维度上切分。此算子返回移除了给定维度后原张量的 view 。

参数:
  • input (Tensor) - 输入张量

  • dim (int) - 切分的维度

  • select (int) - 选择的索引

返回:

oneflow.Tensor: 输出张量。

示例:

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

应用以下 element-wise 公式:

\(\text{SELU}(x) = scale * (\max(0,x) + \min(0, \alpha * (\exp(x) - 1)))\) , \(\alpha=1.6732632423543772848170429916717\) , \(scale=1.0507009873554804934193349852946\)

更多信息请参考 SELU

示例:

>>> import oneflow as flow

>>> input = flow.tensor([1, 2, 3], dtype=flow.float32)
>>> 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)

启用梯度计算的上下文管理器。

如果梯度计算通过 no_grad 被禁用,则启用它。

这个上下文管理器是线程本地的;它不会影响其他线程的计算。

也可以作为一个装饰器。(确保用括号来实例化)。

参数:
  • mode (bool) - 标记是否启用或禁用梯度计算。(默认: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)

设置打印的选项。取自NumPy。

参数:
  • precision - 输出浮点数的精度位数 (默认=4)。

  • threshold - 触发总结的数组元素的总数而不是完全 repr (默认值=1000)。

  • edgeitems - 在每个维度的开始和结束时,汇总的数组的数量(默认值=3)。

  • linewidth - 每行插入换行符的字符数(默认 = terminal_columns )。

  • profile - 默认为 pretty printing,可以用上述任何一个选项来覆盖。(default, short, full 中的任何一个)

  • sci_mode - 启用(True)或禁用(False)科学计数法。如果 None(默认)被指定,其值由 oneflow._tensor_str._Formatter 定义。这个值是由框架自动选择的。

Note

线宽等于终端列,手动设置将使默认的自动设置失效。

oneflow.set_rng_state(state)

该文档引用自: https://pytorch.org/docs/1.10/generated/torch.set_rng_state.html.

返回随机数发生器的状态为 oneflow.ByteTensor

oneflow.sigmoid(input)Tensor

应用以下 element-wise 公式: \(\text{Sigmoid}(x) = \frac{1}{1 + \exp(-x)}\)

更多信息请参考 Sigmoid

示例:

>>> import oneflow as flow

>>> input = flow.tensor([0.81733328, 0.43621480, 0.10351428], dtype=flow.float32)
>>> out = flow.nn.functional.sigmoid(input)
>>> out
tensor([0.6937, 0.6074, 0.5259], dtype=oneflow.float32)
oneflow.sign(x)Tensor

x 中元素的正负。

公式为:

\[\text{out}_{i} = \text{sgn}(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x1 = flow.tensor([-2, 0, 2], dtype=flow.float32)
>>> out1 = flow.sign(x1)
>>> out1
tensor([-1.,  0.,  1.], dtype=oneflow.float32)
>>> x2 = flow.tensor([-3.2, -4.5, 5.8], dtype=flow.float32, device=flow.device('cuda'))
>>> out2 = flow.sign(x2)
>>> out2
tensor([-1., -1.,  1.], device='cuda:0', dtype=oneflow.float32)
oneflow.silu(x)Tensor

公式为:

\[\text{silu}(x) = x * sigmoid(x)\]

示例:

>>> import oneflow as flow

>>> input = flow.tensor([1, 2, 3], dtype=flow.float32)
>>> out = flow.silu(input)
>>> out
tensor([0.7311, 1.7616, 2.8577], dtype=oneflow.float32)

更多信息请参考 SiLU

oneflow.sin(input)Tensor

返回一个元素为 input 正弦值的新张量。

\[\text{y}_{i} = \sin(\text{x}_{i})\]
参数:

input (Tensor): 输入张量

示例:

>>> import oneflow as flow
>>> x1 = flow.tensor([-0.5461,  0.1347, -2.7266, -0.2746], dtype=flow.float32)
>>> y1 = flow.sin(x1)
>>> y1
tensor([-0.5194,  0.1343, -0.4032, -0.2712], dtype=oneflow.float32)
>>> x2 = flow.tensor([-1.4, 2.6, 3.7], dtype=flow.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_()

函数 oneflow.sin() 的 In-place 版本。

oneflow.sinh(x)Tensor

返回一个包含 x 中元素的双曲正弦值的新 tensor。

公式为:

\[\text{out}_{i} = \sinh(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x1 = flow.tensor([1, 2, 3], dtype=flow.float32)
>>> x2 = flow.tensor([1.53123589,0.54242598,0.15117185], dtype=flow.float32)
>>> x3 = flow.tensor([1,0,-1], dtype=flow.float32)

>>> flow.sinh(x1)
tensor([ 1.1752,  3.6269, 10.0179], dtype=oneflow.float32)
>>> flow.sinh(x2)
tensor([2.2038, 0.5694, 0.1517], dtype=oneflow.float32)
>>> flow.sinh(x3)
tensor([ 1.1752,  0.0000, -1.1752], dtype=oneflow.float32)
oneflow.slice(input, slice_tup_list)Tensor

从张量中提取切片。 以格式为 (start, stop, step) 的 slice_tup_list 为切片索引,在每个维度进行切片。

参数:
  • input (Tensor)

  • slice_tup_list (Sequence[Tuple[int, int, int]]): 指定每个维度的切片 (start, stop, step)

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow
>>> input = flow.randn(3, 6, 9, dtype=flow.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.softmax(input, dim=None)Tensor

将 Softmax 函数应用于 n 维 input tensor 。并且重新缩放, 使输出 tensor 的元素于 [0,1] 范围内并且总和为 1 。

Softmax 的公式为:

\[\text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}\]

当输入张量是稀疏张量时,未指定的值将被视为 -inf

形状:
  • Input: \((*)\) ,其中 * 表示任意数量的附加维度

  • Output: \((*)\) ,与 input 相同的形状

返回类型:

input 具有相同维度和形状的张量,其值在 [0, 1] 范围内

参数:
  • dim (int): 要计算 Softmax 的维度(沿 dim 的每个切片的总和为 1)

示例:

>>> import oneflow as flow

>>> m = flow.nn.Softmax(dim = 2)
>>> x = flow.tensor([[[-0.46716809,  0.40112534,  0.61984003], [-1.31244969, -0.42528763,  1.47953856]]], dtype=flow.float32)
>>> out = m(x)
>>> out
tensor([[[0.1575, 0.3754, 0.4671],
         [0.0507, 0.1230, 0.8263]]], dtype=oneflow.float32)
oneflow.softplus(x: Tensor)Tensor

逐元素地应用此公式:

\[\text{Softplus}(x) = \frac{1}{\beta} * \log(1 + \exp(\beta * x))\]

更多细节参考 Softplus

oneflow.softsign(x: Tensor)Tensor

softsign 的公式为:

\[softsign(x) = \frac{x}{1 + |x|}\]

示例:

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

参考 Softsign 获得更多细节。

oneflow.sort(input, dim=- 1, descending=False)

按值升序沿给定维度 dim 对张量 input 的元素进行排序。

参数:
  • input (oneflow.tensor): 输入张量

  • dim (int, 可选): 要排序的维度,默认为(dim = -1)

  • descending (bool, 可选): 控制排序方式(升序或降序)

返回类型:

Tuple(oneflow.tensor, oneflow.tensor(dtype=int32)): 一个元素为 (values, indices) 的元组 元素为排序后的 input 的元素,索引是原始输入张量 input 中元素的索引。

示例:

>>> import oneflow as flow

>>> input = flow.tensor([[1, 3, 8, 7, 2], [1, 9, 4, 3, 2]], dtype=flow.float32)
>>> (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(x, split_size_or_sections, dim=0)Tensor

将张量分成块。

如果 split_size_or_sections 为一个整数,则 x 会被分成等大的块。 如果给定维度 dim 上的 tensor 大小不能被 split_size 整除,则最后一块的大小会小于其它块。

如果 split_size_or_sections 是一个列表, 那么 x 将根据 split_size_or_sections 被拆分为 len(split_size_or_sections) 个大小为 dim 的块。

参数:
  • x (Tensor): 要拆分的张量

  • split_size_or_sections (Union[int, List[int]]): 单个块的大小或包含每个块大小的列表

  • dim (int): 拆分张量所沿的维度。

示例:

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

返回一个元素为 input 元素平方根的新 tensor 。 公式为:

\[\text{out}_{i} = \sqrt{\text{input}_{i}}\]
参数:
  • input (Tensor): 输入张量

示例:

>>> import oneflow as flow

>>> input = flow.tensor([1.0, 2.0, 3.0], dtype=flow.float32)
>>> output = flow.sqrt(input)
>>> output
tensor([1.0000, 1.4142, 1.7321], dtype=oneflow.float32)
oneflow.square(x)Tensor

返回一个新的张量,其元素为 x 中元素的的平方。

\[\text{out}_{i} = \sqrt{\text{input}_{i}}\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([1.0, 2.0, 3.0], dtype=flow.float32)
>>> output = flow.square(input)
>>> output
tensor([1., 4., 9.], dtype=oneflow.float32)
oneflow.squeeze(input, dim=None)Tensor

移除 input 中指定大小为1的维度。 如果 dim 没被设定,则移除 input 中所有大小为 1 的维度。

返回值中的元素数量与 tensor input 相同。

参数:
  • input (oneflow.tensor): 输入张量

  • dim (int, 可选): 输入张量只会在这个维度上被压缩,默认为 None

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([[[[1, 1, 1]]]], dtype=flow.float32)
>>> input.shape
oneflow.Size([1, 1, 1, 3])
>>> out = flow.squeeze(input, dim=[1, 2]).shape
>>> out
oneflow.Size([1, 3])
oneflow.stack(input, dim=0)Tensor

沿新维度连接多个张量。 返回的 tensor 和 input 共享相同的基础数据。

若定义参数 dim ,其应在范围 [-input.ndimension() - 1, input.ndimension() + 1] 内,值为负的 dim 会导致 dim = dim + input.ndimension() + 1 上的 stack()

参数:
  • inputs (List[oneflow.Tensor]): 输入张量的列表。每个张量应该具有相同的形状

  • dim (int): 要连接维度的索引。默认为0

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x1 = flow.rand(1, 3, 5)
>>> x2 = flow.rand(1, 3, 5)
>>> y = flow.stack([x1, x2], dim = -1)
>>> y.shape
oneflow.Size([1, 3, 5, 2])
oneflow.std()

返回输入张量在 dim 维度上每行的标准差。如果 dim 是一个维度列表,则对所有维度进行规约。

如果 keepdim 为真,输出张量与输入张量大小相同,除了在 dim 维度的大小变为1。否则, dim 将被压缩,导致输出张量拥有 1 (或者 len(dim)) 个更少的维度。

如果 unbiasedFalse ,则标准差将通过有差估算器计算。否则,贝塞尔校正将被使用。

参数:
  • input (Tensor): 输入张量

  • dim (int or tuple of python:ints): 维度或者被减少的多个维度

  • unbiased (bool): 是否使用无差估计

  • keepdim (bool): 输出张量是否保留 dim

示例:

>>> import oneflow as flow

>>> input = flow.tensor([1.0,2.0,3.0])
>>> output = flow.std(input, dim=0)
>>> output
tensor(1., dtype=oneflow.float32)
oneflow.sub(input, other)Tensor

计算 inputother 的差,支持 element-wise、标量和广播形式的减法。

公式为:

\[out = input - other\]
参数:
  • input (Tensor): 输入张量

  • other (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

# element-wise 减法
>>> input = flow.randn(2, 3, dtype=flow.float32)
>>> other = flow.randn(2, 3, dtype=flow.float32)
>>> out = flow.sub(input,other)
>>> out.shape
oneflow.Size([2, 3])

# 标量减法
>>> input = 5
>>> other = flow.randn(2, 3, dtype=flow.float32)
>>> out = flow.sub(input,other)
>>> out.shape
oneflow.Size([2, 3])

# 广播减法
>>> input = flow.randn(1, 1, dtype=flow.float32)
>>> other = flow.randn(2, 3, dtype=flow.float32)
>>> out = flow.sub(input,other)
>>> out.shape
oneflow.Size([2, 3])
oneflow.sum(input, dim=None, keepdim=False)Tensor

在给定的维度 dim 计算 input 每列的元素和。如果没有设定 dim ,则会计算 input 所有元素的和。

示例:

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

此函数等价于 NumPy 的 swapaxes 函数。

示例:

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

这个函数与 torch 的 swapdims 函数等价

示例:

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

要求 input 小于二维,并交换第零维和第一维。

零维和一维张量将返回其本身。对于二维张量,此算子等价于 transpose(input, 0, 1)

参数:
  • input (oneflow.Tensor) - 输入张量

示例:

> import oneflow as flow
> x = flow.rand()
> flow.t(x).shape
oneflow.Size([])
> x = flow.rand(3)
> flow.t(x).shape
oneflow.Size([3])
> x = flow.rand(2,3)
> flow.t(x).shape
oneflow.Size([3, 2])
oneflow.tan(x)Tensor

返回一个包含 x 中元素的正切值的新 tensor。

公式为:

\[\text{out}_{i} = \tan(\text{input}_{i})\]
参数:

x (Tensor): 输入张量

返回类型:

oneflow.tensor

示例:

>>> import math as m
>>> import oneflow as flow

>>> input = flow.tensor([-1/4*m.pi, 0, 1/4*m.pi], dtype=flow.float32)
>>> output = flow.tan(input)
>>> output
tensor([-1.,  0.,  1.], dtype=oneflow.float32)
oneflow.tanh(x)Tensor

公式为:

\[out = \frac{e^x-e^{-x}}{e^x+e^{-x}}\]

更多信息请参考 Tanh

oneflow.tensor()
用数据构造一个张量,如果设置了 placementsbp ,则返回consistent tensor,

否则返回一个 local tensor 。

参数:
  • data: 张量的初始数据。可以是列表、元组、NumPy ndarray、标量或张量。

关键词参数:
  • dtype (oneflow.dtype, 可选):返回张量的所需数据类型。默认值:如果没有,则从数据推断数据类型。

  • device (oneflow.device, 可选):返回张量的所需设备。如果 placement 和 sbp 为 None,则使用当前 cpu 作为默认设备。

  • placement (oneflow.placement, 可选):设置返回张量的 placement 属性。

  • sbp (oneflow.sbp 或 oneflow.sbp 中的元组, 可选):返回张量的所需 sbp。

  • requires_grad (bool, 可选):如果已经自动求导则记录对返回张量的操作。默认值:False。

注意:

关键词参数 device 与 placement 、 sbp 是互斥的。 consistent tensor只能由张量构造。

示例:

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

该算子通过对输入张量应用碎片化更新,创造一个新的张量。

除了会更新被分散到已存在的张量(而不是一个零张量)以外,该算子与 scatter_nd() 十分类似。

参数:
  • tensor: 被分散化的张量

  • indices: update 的索引。它的类型应该为 flow.int

  • update: 更新的张量

示例:

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

将一个张量分割成多个子张量,这些子张量都是输入的展开,按照 indices_or_sections 指定的索引或节数沿维度 dim 分割。

该文档参考了: https://pytorch.org/docs/1.10/generated/torch.tensor_split.html

参数:
  • input (Tensor) -输入张量。

  • indices_or_sections (int or a list) - 如果 indices_or_sections 是一个整数 n,那么输入将沿着 dim 维度被分割成 n 个部分。

    如果输入沿维度 dim 可被 n 整除,则每个部分的大小相等,即 input.size(dim) / n。 如果输入不被 n 整除,第一个 int(input.size(dim)% n) 部分的大小将是 int(input.size(dim) / n)+1,其余部分的大小将是 int(input.size(dim) / n)。 如果 indices_or_sections 是一个 ints 的列表或元组,那么输入将在列表、元组或张量中的每个索引处沿 dim 维度进行分割。 例如,indices_or_sections=[2, 3],dim=0,将产生张量 input[:2],input[2:3] 和 input[3:]。 如果 indices_or_sections 是一个张量,它在 CPU 上必须是一个零维或一维的长张量。

  • dim (int) - 用来分割张量的维度。

返回类型:

oneflow.TensorTuple: 输出 TensorTuple

示例:

>>> 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.tile(input, reps)Tensor

此接口与 PyTorch 一致。 文档参考自: https://pytorch.org/docs/stable/generated/torch.tile.html

通过重复 input 的元素构造一个新张量。 reps 参数指定每个维度的重复次数。

如果 reps 的长度小于 input 的维度,则在 reps 前添加 1 。直到 reps 的长度 等于 input 的维度。例如: input 的形状为 (8, 6, 4, 2) ,而 reps 为 (2, 2) , 则 reps 被认为是 (1, 1, 2, 2) 。

类似地,如果 input 的维度少于 reps 指定的维度,则 input 被视为在维度 0 处未压缩, 直到它的维度与 reps 指定的一样多。例如,如果 input 的形状为 (4, 2) 而 reps 为 (3, 3, 2, 2), 则视 input 形状为 (1, 1, 4, 2)。

Note

这个函数类似于 NumPy 的 tile 函数。

参数:
  • input (oneflow.tensor): 要重复元素的张量

  • reps (元组): 每个维度要重复的次数

示例:

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

查找指定轴上的 k 个最大条目的值和索引。

参数:
  • input (oneflow.Tensor) - 输入张量。

  • k (int) - top-k 的 k。

  • dim (int,可选) - 要排序用的维度。默认为最后一个维度(-1)。

  • largest (bool,可选) - 控制是否返回最大或最小的元素

  • sorted (bool,可选) - 控制是否按排序顺序返回元素(现在只支持 True!)。

返回类型:
  • Tuple (oneflow.Tensor, oneflow.Tensor(dtype=int32)) - 一个(values, indices)的元组,其中 indices 是原始输入张量中的元素的索引。

示例:

>>> 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(input, dim0, dim1)Tensor

返回一个 tensor ,它是 input 的转置版本。交换指定维度 dim0dim1

输出 tensor 与输入 tensor 共享内存, 所以改变其中一个的元素会改变另一个的元素。

参数:
  • input (oneflow.tensor): 输入张量

  • dim0 (int): 要转置的第一个维度。

  • dim1 (int): 要转置的第二个维度。

返回类型:

oneflow.tensor: 转置张量

示例:

>>> import oneflow as flow
>>> input = flow.randn(2, 6, 5, 3, dtype=flow.float32)
>>> out = flow.transpose(input, 0, 1).shape
>>> out
oneflow.Size([6, 2, 5, 3])
oneflow.tril(x, diagonal=0)Tensor

返回输入矩阵(二维张量)或矩阵批次的沿指定对角线的下三角部分,结果张量的其他元素设置为 0。

Note

  • 如果 diagonal = 0,返回张量的对角线是主对角线

  • 如果 diagonal > 0,返回张量的对角线在主对角线之上

  • 如果 diagonal < 0,返回张量的对角线在主对角线之下

参数:
  • x (Tensor): 输入张量

  • diagonal (Optional[Int64], 0): 要考虑的对角线

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> x = flow.ones(3, 3, dtype=flow.float32)
>>> flow.tril(x)
tensor([[1., 0., 0.],
        [1., 1., 0.],
        [1., 1., 1.]], dtype=oneflow.float32)
oneflow.unbind()

这个函数与 PyTorch 的 unbind 函数等价,可移除一个张量维度。

返回沿给定维度的移除的张量的所有切片的一个元组,即已经被移除的部分。

参数:
  • x (Tensor) - 需要 unbind 张量。

  • dim (int) - 移除的维度。

示例:

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

input 的某个指定位置增加一个大小为1的维度并返回。

返回的 tensor 与此 input 共享相同的基础数据。

参数 dim 应在范围 [-input.ndimension() - 1, input.ndimension() + 1] 内, 值为负的 dim 会导致 dim = dim + input.ndimension() + 1 上的 stack()

参数:
  • input (Tensor): 输入张量

  • dim (int): 插入维度的索引

示例:

>>> import oneflow as flow

>>> x = flow.randn(2, 3, 4)
>>> y = x.unsqueeze(2)
>>> y.shape
oneflow.Size([2, 3, 1, 4])
oneflow.var(input, dim=None, unbiased=True, keepdim=False)Tensor

返回给定维度 diminput 张量的每一行的方差。

如果 keepdimTrue ,输出张量与 input 的大小相同。除非维度 dim 的大小为 1 , 否则输出的维度将被压缩 (参见 flow.squeeze() ) 导致输出张量的维度少 1 (或 len(dim) )。

参数:
  • input (Tensor): 输入张量

  • dim (int 或者 tuple of python:ints): 要减少的一个或多个维度。默认为None

  • unbiased (bool, 可选): 是否使用贝塞尔校正 (\(\delta N = 1\)) 。 默认为 True

  • keepdim (bool, 可选): 输出张量是否保留了 input 的维度。 默认为 False

返回类型:

oneflow.tensor

示例:

>>> import oneflow as flow

>>> input = flow.tensor([[-0.8166, -1.3802, -0.3560]], dtype=flow.float32)
>>> output = flow.var(input, 1, True)
>>> output
tensor([0.2631], dtype=oneflow.float32)
oneflow.vsplit()

根据 indices_or_sections 的值,将输入的二维或更高维张量垂直地切分成多个张量。 每个切分出的张量都是输入张量的一个 view 。此算子等价于调用 torch.tensor_split(input, indices_or_sections, dim=0) (切分维度为 0 ),除了在 indices_or_sections 是 一个整型的情况下,此时它必须平均划分切分维度,否则将抛出一个运行时错误。此文档参考自:https://pytorch.org/docs/stable/generated/torch.vsplit.html#torch.vsplit

参数:
  • input (Tensor) - 输入张量

  • indices_or_sections (int 或者一个列表) - 如果 indices_or_sections 是一个整型 n , input 将沿着维度 dim 被切分为 n 个部分。

    如果在维度 dim 上 input 能被 n 整除,则每个部分的大小相同,都为 input.size(dim)/n。如果 input 无法被 n 整除,则前 int(input.size(dim) % n) 个部分(section)的大小都为 int(input.size(dim) / n) + 1, 而余下的部分大小则是 int(input.size(dim) / n)。如果 indices_or_sections 是一个整型的列表或元组,则输入将在维度 dim 上根据列表或元组内的每个索引被切分。 比如,若 indices_or_sections=[2, 3] 和 dim=0,输入张量将被切分为 input[:2], input[2:3], 和 input[3:]。如果 indices_or_sections 是一个张量,它必须是一个位于 cpu 的 零维或者一维张量。

返回值:

输出的 TensorTuple

返回类型:

oneflow.TensorTuple

示例:

> import oneflow as flow

> input = flow.rand(3,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)Tensor

返回一个 tensor 其元素为从 xy 中依据 condition 的真实值选择的元素, 如果 condition 中的元素大于 0 ,则取 x 中的元素,否则取 y 的元素。

Note

如果 x 为 None 并且 y 为 None ,则 flow.where(condition) 等同于 flow.nonzero(condition, as_tuple=True) 。

conditionxy 必须可互相广播。

参数:
  • condition (IntTensor): 如果不为 0 则 yield x ,否则 yield y

  • x (Tensor 或 Scalar): 当 condition 为 True 时,如果 x 为标量则为值,如果 x 不为标量则为在索引处选择的值

  • y (Tensor 或 Scalar): 当 condition 为 False 时,如果 x 为标量则为值,如果 x 不为标量则为在索引处选择的值

返回类型:

oneflow.tensor: 与 conditionxy 广播形状相同的 tensor 。

示例:

>>> import oneflow as flow

>>> x = flow.tensor([[-0.4620, 0.3139], [0.3898, -0.7197], [0.0478, -0.1657]], dtype=flow.float32)
>>> y = flow.ones(3, 2, dtype=flow.float32)
>>> condition = flow.tensor([[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, dtype=None, device=None, placement=None, sbp=None, requires_grad=False)Tensor

返回一个用标量值 0 填充的 tensor ,其形状由变量参数 size 定义。

参数:
  • size (整数或整数元组): 定义输出张量的形状。可以是可变数量的参数或是像列表或元组这样的集合。

  • dtype (flow.dtype, 可选): 返回张量的数据类型

  • device (flow.device, 可选): 返回的本地张量的所需设备。默认使用当前设备

  • placement (flow.placement, 可选): 设置返回张量的 placement 属性。如果为None,则构造 local tensor

  • sbp (flow.sbp.sbp 或 tuple of flow.sbp.sbp, 可选): 返回的global tensor的所需 sbp 描述符。如果为 None ,则返回的张量是使用参数 device 的本地张量

  • requires_grad (bool, 可选): 用 autograd 记录对返回张量的操作,默认为 False

示例:

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

返回一个元素全部为值为 0 的标量,形状和 x 相同的 tensor。 flow.zeros_like(x) 等价于 flow.zeros(x.shape, dtype=x.dtype)

参数:

x (Tensor): 输入的形状将决定输出的形状

示例:

>>> import oneflow as flow
>>> x = flow.randn(5, dtype=flow.float32)
>>> y = flow.zeros_like(x)
>>> y
tensor([0., 0., 0., 0., 0.], dtype=oneflow.float32)
oneflow.relu(input, inplace)Tensor

input 逐元素应用 ReLU 函数(Rectified Linear Unit,线性整流函数)。更多信息请参考 ReLU

参数:
  • input (Tensor)

  • inplace (Bool): 如果为 True ,则以原地算法执行此算子。默认为 False

示例:

>>> import oneflow as flow

>>> input = flow.tensor([1, -2, 3], dtype=flow.float32)
>>> output = flow.relu(input)
>>> output
tensor([1., 0., 3.], dtype=oneflow.float32)
oneflow.set_num_threads()

设置在 cpu 上用于 intraop 并行计算的线程数量。

Warning

为了保证正确数量的线程被使用, set_num_threads 必须在运行 eager , eager globe 或 ddp 之前被调用。