oneflow.Tensor¶
OneFlow Tensor Class¶
-
class
oneflow.Tensor¶ -
property
T¶ 此张量所有维度转置后的张量。
如果 n 是 x 的维数,x.T 等同于 x.permute(n-1, n-2, …, 0) 。
-
abs()¶
-
acos()¶
-
acosh()¶
-
add(input, other) → Tensor¶ 计算 input 和 other 的和。支持 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])
-
add_(other, *, alpha=1)¶ oneflow.Tensor.add()的 Inplace 版本。
-
addcmul(tensor1, tensor2, *, value=1)¶
-
addcmul_(tensor1, tensor2, *, value=1)¶ oneflow.Tensor.addcmul()的 In-place 版本。
-
addmm(mat1, mat2, alpha=1, beta=1)¶
-
amax(dim=None, keepdim=False)¶
-
amin(dim=None, keepdim=False)¶
-
arccos()¶
-
arccosh()¶
-
arcsin()¶
-
arcsinh()¶
-
arctan()¶
-
arctanh()¶
-
argmax(dim=None, keepdim=None)¶
-
argmin(dim=None, keepdim=None)¶
-
argsort(dim=None, descending=None)¶ 这个算子将输入的张量按指定的 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)
-
argwhere()¶
-
asin()¶
-
asinh()¶
-
atan()¶
-
atan2(other)¶
-
atanh()¶
-
backward(gradient=None, retain_graph=False, create_graph=False)¶ 该接口与PyTorch一致。 该文件的参考来源是: https://pytorch.org/docs/stable/generated/torch.Tensor.backward.html#torch.Tensor.backward.
计算当前张量即 graph leaves 的梯度。
Graph 是用链式规则来区分的。如果张量是非标量(即它的数据有一个以上的元素)并且需要梯度,则该函数还需要指定梯度。它应该是一个类型和位置相匹配的张量,它包含了被微分函数(即 self)的梯度。
这个函数在 leaves 中累积梯度 - 你可能需要在调用它之前将 .grad 的属性归零或将它们设置为 None。关于累积梯度的内存结构的细节,参阅默认梯度结构。
- 注意:
如果你在用户指定的 CUDA 流上下文中运行任何前向操作、创建梯度或后向调用,参阅后向传递的流语义。
- 注意:
当提供了输入,并且给定的输入不是 leaf 时,当前的实现将调用它的 grad_fn(尽管严格来说不需要获得这个梯度)。这是一个实现的细节,用户不应该依赖它。更多细节见 https://github.com/pytorch/pytorch/pull/60521#issuecomment-867061780 。
- 参数:
gradient (Tensor or None) - 张量的梯度。如果它是一个张量,除非 create_graph 为 True ,否则它将被自动转换为一个不需要 grad 的张量。对于标量张量或不需要 grad 的张量,可以指定为 None 值。如果一个 None 值是可以接受的,那么这个参数是可选的。
retain_graph (bool, optional) - 如果为 False ,用于计算 grads 的 graph 将被释放。请注意,在几乎所有的情况下,不需要将这个选项设置为 True ,通常可以用一种更有效的方式来解决。默认为 create_graph 的值。
create_graph (bool, optional) - 如果为 True, 将构建导数的 graph ,允许计算高阶导数。默认为False。
-
bmm(other)¶
-
byte()¶ self.byte() 和 self.to(oneflow.uint8) 是等价的。 参考
oneflow.Tensor.to()
-
cast(dtype)¶
-
ceil()¶
-
chunk(chunks=None, dim=None)¶
-
clamp(min=None, max=None)¶ 参考
oneflow.clamp().
-
clamp_(min=None, max=None)¶ oneflow.Tensor.clamp()的 Inplace 版本。
-
clip(min=None, max=None)¶
-
clip_(min=None, max=None)¶
-
clone()¶
-
copy_(other: Union[oneflow.Tensor, numpy.ndarray])¶ 该接口与 PyTorch 一致。
Tensor.copy_(src, non_blocking=False) → Tensor
将 src 中的元素复制到 self 张量中,并返回 self 。
src 张量必须可以与 self 张量一起广播。它可以是不同的数据类型,或者位于不同的设备上。
参数:
src (Tensor) - 要复制的源张量
non_blocking (bool) - 如果为 True ,并且是在 CPU 和 GPU 之间的拷贝,那么相对于主机来说,拷贝可能会异步发生。对于其他情况,这个参数没有影响。
-
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)
-
cosh()¶
-
cpu()¶ 返回此对象在 CPU 内存中的副本。
如果此对象已在 CPU 内存中且位于正确的设备上,则不会执行复制,而是返回原始对象。
示例:
>>> import oneflow as flow >>> input = flow.tensor([1, 2, 3, 4, 5], device=flow.device("cuda")) >>> output = input.cpu() >>> output.device device(type='cpu', index=0)
-
cuda(device=None)¶ 返回此对象在 CUDA 内存中的副本。
如果此对象已在 CUDA 内存中且位于正确的设备上,则不会执行复制,而是返回原始对象。
- 参数:
device (flow.device): 目标 GPU 设备。默认为当前 CUDA 设备。
示例:
>>> import oneflow as flow >>> input = flow.Tensor([1, 2, 3, 4, 5]) >>> output = input.cuda() >>> output.device device(type='cuda', index=0)
-
data¶
-
detach()¶
-
device¶ The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.Tensor.device.html.
Is the
oneflow.devicewhere this Tensor is, which is invalid for global tensor.
-
diag(diagonal=0)¶
-
diagonal(offset=0, dim1=0, dim2=1)¶
-
dim()¶ Tensor.dim() → int
返回 self 张量的维数。
-
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])
-
div_(value) → Tensor¶ oneflow.Tensor.div()的 Inplace 版本。
-
double()¶ Tensor.double() 等价于 Tensor.to(flow.float64) 。 参见
oneflow.to。- 参数:
input (Tensor): 输入张量
示例:
>>> import oneflow as flow >>> input = flow.randint(0, 5, (3,3)) >>> input = input.double() >>> input.dtype oneflow.float64
-
dtype¶
-
element_size()¶ Tensor.element_size() → int
返回单个元素的字节大小。
-
eq(other)¶ 参考
oneflow.eq()
-
erfinv()¶
-
erfinv_()¶ oneflow.erfinv()的 Inplace 版本。
-
exp()¶
-
expand_as(other) → Tensor¶ 将输入张量扩展到与
other相同的大小。self.expand_as(other)等价于self.expand(other.size())。更多有关
expand的细节请参考expand()。- 参数:
other (
oneflow.Tensor): 返回张量与other大小相同。
-
expm1()¶
-
fill_(value)¶ Tensor.fill_(value) → Tensor
用指定的值填充 self 张量。
-
flip(dims)¶
-
float(input)¶ Tensor.float() 等价于 Tensor.to(flow.float32) 。 参见
oneflow.to。- 参数:
input (Tensor): 输入张量
示例:
>>> import oneflow as flow >>> input = flow.randint(0, 5, (3,3)) >>> input = input.float() >>> input.dtype oneflow.float32
-
floor()¶
-
floor_()¶ oneflow.floor()的 Inplace 版本。
-
ge(other)¶ 参考
oneflow.ge()
-
gelu()¶
-
get_device() -> Device ordinal (Integer)¶ 对于 CUDA 张量,该函数返回张量所在 GPU 的设备序号。对于 CPU 张量,会产生一个错误。
-
global_to_global(placement=None, sbp=None, *, grad_sbp=None, check_meta=False) → Tensor¶ 执行张量 placement 和/或 sbp 转换。
Note
这个张量必须是 global tensor。
至少需要 placement 和 sbp 中的一个操作。
如果 placement 和 sbp 都与这个张量的 placement 和 sbp 相同,那么返回自己的这个张量。
- 参数:
placement (flow.placement, optional) - 返回 global tensor 的所需 placement,默认值:None。
sbp (flow.sbp.sbp or tuple of flow.sbp.sbp, optional) - 返回 global tensor 的所需 sbp,默认值:None。
- 关键参数:
- grad_sbp (flow.sbp.sbp or tuple of flow.sbp.sbp, optional) - 手动指定在后向传播中该张量梯度张量的 sbp。
如果为 None,将自动推断出梯度张量的 sbp。默认值:None。
- check_meta (bool, optional) - 注明是否要检查元信息。如果设置为 True,则检查每个 rank 上的输入元信息
(placement 和 sbp)的一致性。默认值:False。
>>> # 在 2 个 rank 上各自运行。 >>> import oneflow as flow >>> input = flow.tensor([0., 1.], dtype=flow.float32, placement=flow.placement("cpu", ranks=[0, 1]), sbp=[flow.sbp.broadcast]) >>> output = input.global_to_global(placement=flow.placement("cpu", ranks=[0, 1]), sbp=[flow.sbp.split(0)]) >>> print(output.size()) >>> print(output)
>>> # 在 rank 0 上运行的结果。 oneflow.Size([2]) tensor([0., 1.], placement=oneflow.placement(type="cpu", ranks=[0, 1]), sbp=(oneflow.sbp.split(axis=0),), dtype=oneflow.float32)
>>> # 在 rank 1 上运行的结果。 oneflow.Size([2]) tensor([0., 1.], placement=oneflow.placement(type="cpu", ranks=[0, 1]), sbp=(oneflow.sbp.split(axis=0),), dtype=oneflow.float32)
-
grad¶ Return the gradient calculated by autograd functions. This property is None by default.
-
grad_fn¶ Return the function that created this tensor if it’s
requires_gradis True.
-
gt(other)¶ 参考
oneflow.gt()
-
half()¶ self.half() 和 self.to(dtype=oneflow.float16) 是等价的。
-
int()¶ Tensor.int() 等价于 Tensor.to(flow.int32) 。 参见
oneflow.to。- 参数:
input (Tensor): 输入张量
示例:
>>> import oneflow as flow >>> input = flow.randn(1, 2, 3, dtype=flow.float32) >>> input = input.int() >>> input.dtype oneflow.int32
-
is_contiguous() → bool¶ Returns True if self tensor is contiguous in memory.
-
is_cuda¶ Is True if the Tensor is stored on the GPU, False otherwise.
-
is_floating_point()¶
-
is_global¶ Return whether this Tensor is a global tensor.
-
is_lazy¶ Return whether this Tensor is a lazy tensor.
-
is_leaf¶ Compatible with PyTorch.
All Tensors that have
requires_gradwhich isFalsewill be leaf Tensors by convention.For Tensor that have
requires_gradwhich isTrue, they will be leaf Tensors if they were created by source operations.Only leaf Tensors will have their
gradpopulated during a call tobackward(). To getgradpopulated for non-leaf Tensors, you can useretain_grad().For example:
>>> import oneflow as flow >>> a = flow.rand(10, requires_grad=False) >>> a.is_leaf True >>> a = flow.rand(10, requires_grad=True) >>> a.is_leaf True >>> b = a.cuda() >>> b.is_leaf False >>> c = a + 2 >>> c.is_leaf False
-
item()¶ 将 tensor 的值作为标准 Python 数字返回。仅适用于只有一个元素的 tensor 。
其他情况请参考
oneflow.tolist。这个操作不可导。
- 参数:
input (Tensor): 输入张量
示例:
>>> import oneflow as flow >>> x = flow.tensor([1.0]) >>> x.item() 1.0
-
le(other)¶ 返回 \(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, 1, 4], dtype=flow.float32) >>> out = flow.le(input1, input2) >>> out tensor([ True, False, True], dtype=oneflow.bool)
-
local_to_global(placement=None, sbp=None, *, check_meta=Ture) → Tensor¶ 从一个 local tensor 构造一个 global tensor。
Note
这个张量必须是 local tensor。
placement 和 sbp 属性都需要。
返回的 global tensor 将该张量作为其在当前 rank 中的 local tensor。
通常数据是不会被广播的,但是当 sbp 为
oneflow.sbp.broadcast时,rank 0 的数据将被广播到其他等级。- 参数:
placement (flow.placement, optional) - 返回 global tensor 的所需 placement,默认值:None。
sbp (flow.sbp.sbp or tuple of flow.sbp.sbp, optional) - 返回 global tensor 的所需 sbp,默认值:None。
- 关键参数:
- check_meta (bool, optional) - 注明当从 local tensor 构建 global tensor 时是否要检查元信息。只有设置为 False 时,每
个 rank 上输入的 local tensor的形状和类型都相同,如果设置为 False,则加速执行 local_to_global。默认值:True。
>>> # 在 2 个 rank 上各自运行。 >>> import oneflow as flow >>> input = flow.tensor([0., 1.], dtype=flow.float32) >>> output = input.local_to_global(placement=flow.placement("cpu", ranks=[0, 1]), sbp=[flow.sbp.split(0)], check_meta=False) >>> print(output.size()) >>> print(output)
>>> # 在 rank 0 上运行的结果。 oneflow.Size([4]) tensor([0., 1., 0., 1.], placement=oneflow.placement(type="cpu", ranks=[0, 1]), sbp=(oneflow.sbp.split(axis=0),), dtype=oneflow.float32)
>>> # 在 rank 1 上运行的结果。 oneflow.Size([4]) tensor([0., 1., 0., 1.], placement=oneflow.placement(type="cpu", ranks=[0, 1]), sbp=(oneflow.sbp.split(axis=0),), dtype=oneflow.float32)
-
log()¶
-
log1p()¶
-
long(input) → Tensor¶ Tensor.long() 等价于 Tensor.to(flow.int64) 。 参考
oneflow.to()。- 参数:
input (Tensor): 输入张量
示例:
>>> import oneflow as flow >>> input = flow.randn(1, 2, 3, dtype=flow.float32) >>> input = input.long() >>> input.dtype oneflow.int64
-
lt(other)¶ 参考
oneflow.lt()
-
masked_fill(mask, fill_value)¶
-
masked_select(mask)¶
-
matmul(other)¶
-
mish()¶
-
mul_(value) → Tensor¶ oneflow.Tensor.mul()的 Inplace 版本。
-
narrow(dimension, start, length)¶
-
property
ndim¶
-
ndimension()¶ Tensor.dim() → int
返回 self 张量的维数。
-
ne(other)¶ 参考
oneflow.ne()
-
negative()¶
-
nelement()¶ Tensor.nelement() → int
numel() 的别名。
-
new_empty(*size, dtype=None, device=None, placement=None, sbp=None, requires_grad=False) → Tensor¶ 返回一个充满未初始化数据大小为
size的张量。默认情况下,返回的张量具有与此张量相同的flow.dtype和flow.device。- 参数:
size (int…) - 一个 list,tuple 或整数的 flow.Size,定义输出张量的形状。
dtype (flow.dtype, optional) - 返回张量的所需类型,默认值如果为 None,则返回与此张量相同的 flow.dtype。
device (flow.device, optional) - 返回张量的所需设备,默认值如果为 None,则返回与此张量相同的 flow.device。
placement (flow.placement, optional) - 返回的 global tensor 的所需的 placement,默认值如果为 None,则返回的张量是使用参数 device 的 local tensor。
sbp (flow.sbp.sbp or tuple of flow.sbp.sbp, optional) -返回的 global tensor 所需的 sbp 描述,默认值如果为 None,则返回的张量是使用参数 device 的 local tensor。
requires_grad (bool, optional) - 如果 autograd 应该在返回的张量上记录算子,默认值:None。
示例:
>>> import oneflow as flow >>> x = flow.ones(()) >>> y = x.new_empty((2, 2)) >>> y.shape oneflow.Size([2, 2])
-
new_zeros(size=None, dtype=None, device=None, placement=None, sbp=None, requires_grad=False) → Tensor¶ 返回一个大小为 0 的张量。默认情况下,返回的张量与该张量具有相同的 flow.dtype 和 flow.device。
- 参数:
size (int…) - 一个 list,tuple 或整数的 flow.Size,定义输出张量的形状。
dtype (flow.dtype, optional) - 返回张量的所需类型,默认值如果为 None,则返回与此张量相同的 flow.dtype。
device (flow.device, optional) - 返回张量的所需设备,默认值如果为 None,则返回与此张量相同的 flow.device。
placement (flow.placement, optional) - 返回的 global tensor 的所需的 placement,默认值如果为 None,则返回的张量是使用参数 device 的 local tensor。
sbp (flow.sbp.sbp or tuple of flow.sbp.sbp, optional) -返回的 global tensor 所需的 sbp 描述,默认值如果为 None,则返回的张量是使用参数 device 的 local tensor。
requires_grad (bool, optional) - 如果 autograd 应该在返回的张量上记录算子,默认值:None。
For example:
>>> import numpy as np >>> import oneflow as flow >>> x = flow.Tensor(np.ones((1, 2, 3))) >>> y = x.new_zeros((2, 2)) >>> y tensor([[0., 0.], [0., 0.]], dtype=oneflow.float32)
-
norm(p=None, dim=None, keepdim=False, dtype=None)¶
-
numel()¶
-
numpy()¶ Tensor.numpy() → numpy.ndarray
将 self 张量作为一个 NumPy ndarray 返回。这个张量和返回的 ndarray 共享相同的底层存储。对 self 张量的变化将反映在 ndarray 中,反之亦然。
-
permute(*dims)¶
-
placement¶ Is the
oneflow.placementwhere this Tensor is, which is invalid for local tensor.
-
pow(b)¶
-
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)
-
register_hook(hook)¶ Registers a backward hook.
The hook will be called every time a gradient with respect to the Tensor is computed. The hook should have the following signature:
hook(grad) -> Tensor or None
The hook should not modify its argument, but it can optionally return a new gradient which will be used in place of
grad.For example:
>>> import oneflow as flow >>> x = flow.ones(5, requires_grad=True) >>> def hook(grad): ... return grad * 2 >>> x.register_hook(hook) >>> y = x * 2 >>> y.sum().backward() >>> x.grad tensor([4., 4., 4., 4., 4.], dtype=oneflow.float32)
-
relu()¶
-
requires_grad¶ Compatible with PyTorch.
Is
Trueif gradient need to be computed for this Tensor,Falseotherwise.
-
requires_grad_(requires_grad=True) → Tensor¶ Compatible with PyTorch.
- Parameters
requires_grad (bool) – Change the requires_grad flag for this Tensor. Default is
True.
For example:
>>> import oneflow as flow >>> a = flow.rand(10, requires_grad=False) >>> a.requires_grad False >>> a = a.requires_grad_(requires_grad=True) >>> a.requires_grad True
-
reshape(*shape)¶
-
retain_grad()¶ Compatible with PyTorch.
Enables this Tensor to have their
gradpopulated duringbackward(). This is a no-op for leaf tensors.
-
roll(shifts, dims=None)¶
-
round()¶
-
rsqrt()¶ See
oneflow.rsqrt()
-
sbp¶ Is the
oneflow.sbprepresenting that how the data of the global tensor is distributed, which is invalid for local tensor.
-
selu()¶
-
shape¶
-
sigmoid()¶
-
sign()¶
-
silu()¶
-
sin_()¶ oneflow.sin()的 In-place 版本。
-
sinh()¶
-
size(idx=None)¶ 该接口与 PyTorch 一致。
返回 self 张量的大小。如果没有指定 dim ,返回值是 oneflow.Size ,是 tuple 的子类。如果指定了 dim ,返回一个持有该维度大小的 int 。
- 参数:
idx (int, optional) - 用于检索尺寸的维度。
-
softmax(dim=None)¶
-
softplus()¶
-
softsign()¶
-
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)
-
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))
-
sqrt()¶
-
square()¶
-
squeeze(dim=None)¶
-
std(dim=None, unbiased=True, keepdim=False)¶
-
storage_offset() → Tensor¶ Returns self tensor’s offset in the underlying storage in terms of number of storage elements (not bytes).
Example:
>>> import oneflow as flow >>> x = flow.tensor([1, 2, 3, 4, 5]) >>> x.storage_offset() 0
-
stride()¶
-
sub(input, other) → Tensor¶ 计算 input 和 other 的差,支持 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])
-
sub_(value) → Tensor¶ oneflow.Tensor.sub()的 Inplace 版本。
-
swapaxes(dim0, dim1)¶
-
swapdims(dim0, dim1)¶
-
t()¶ Tensor.t() → Tensor
参考
oneflow.t()
-
tan()¶
-
tanh()¶
-
to(*args, **kwargs)¶ - 执行 Tensor dtype 或设备转换。
flow.dtype 和 flow.device 是由 input.to(*args, **kwargs) 的参数推断出来的。
Note
如果
input张量已经 有正确的flow.dtype和flow.device,那么将返回input。 否则,返回的张量是所需的input副本。- 参数:
input (oneflow.Tensor) - 一个输入张量。
args (oneflow.Tensor or oneflow.device or oneflow.dtype) - 位置参数。
kwargs (oneflow.device or oneflow.dtype) - 键值参数。
- 返回:
oneflow.Tensor: 一个张量。
例如:
>>> import numpy as np >>> import oneflow as flow >>> arr = np.random.randint(1, 9, size=(1, 2, 3, 4)) >>> input = flow.Tensor(arr) >>> output = input.to(dtype=flow.float32) >>> np.array_equal(arr.astype(np.float32), output.numpy()) True
-
to_consistent(*args, **kwargs)¶ 这个接口将不再有效,请使用
oneflow.Tensor.to_global()。
-
to_global(placement=None, sbp=None, grad_sbp=None) → Tensor¶ 将 local tensor 转换为 global tensor 或者将 global tensor 转化为 具有不同 sbp 或 placement 的另一个 global tensor 。
- 参数:
input (Tensor): 输入张量
placement (flow.placement, 可选): 设置返回张量的 placement 属性。如果为None,则
input必须为 global tensor ,输出的 placement 将被设置为input的 placement。默认:Nonesbp (flow.sbp.sbp 或 flow.sbp.sbp 的元组, 可选): 返回的 global tensor 的 sbp 属性。如果为 None ,输入张量必须是 global tensor 的并使用它自己的 sbp 。默认:None
示例:
>>> import oneflow as flow >>> input = flow.tensor([0.5, 0.6, 0.7], dtype=flow.float32) >>> placement = flow.placement("cpu", {0:range(1)}) >>> output_tensor = input.to_global(placement, [flow.sbp.split(0)]) >>> output_tensor.is_global True
-
to_local(input) → Tensor¶ 返回 global tensor
input的 local tensor 。- 参数:
input (Tensor): 输入张量
示例:
>>> import oneflow as flow >>> input = flow.tensor([0.5, 0.6, 0.7], dtype=flow.float32) >>> placement = flow.placement("cpu", {0:range(1)}) >>> global_tensor = input.to_global(placement, [flow.sbp.split(0)]) >>> global_tensor.to_local() tensor([0.5000, 0.6000, 0.7000], dtype=oneflow.float32)
-
tolist(input) → Tensor¶ 将 tensor 作为(嵌套)列表返回。对于标量,返回一个标准的 Python 数字,和 oneflow.Tensor.item() 一样。 必要情况下 tensor 会被自动移动到 GPU 。
此操作不可导的。
- 参数:
input (Tensor): 输入张量
For example:
>>> import oneflow as flow >>> input = flow.tensor([[1,2,3], [4,5,6]]) >>> input.tolist() [[1, 2, 3], [4, 5, 6]]
-
topk(k, dim=None, largest=True, sorted=True) → Tesnor¶ 查找指定维度上最大或最小的
k个值和其索引。- 参数:
input (oneflow.Tensor): 输入张量
k (int): 最大最小值的数量
dim (int, 可选的): 要排序的维度,默认为(dim = -1)
largest (bool, 可选的): 控制查找的是最大值还是最小值
sorted (bool, 可选的): 是否返回排好序的元素(目前只支持 True)
- 返回类型:
Tuple(oneflow.Tensor, oneflow.Tensor(dtype=int32)): 一个形状为 (values, indices) 的元组。
indices为输出元素在输入张量中的索引。
示例:
>>> import oneflow as flow >>> x = flow.tensor([[1, 3, 8, 7, 2], [1, 9, 4, 3, 2]], dtype=flow.float32) >>> (values, indices) = flow.topk(flow.Tensor(x), k=3, dim=1) >>> values tensor([[8., 7., 3.], [9., 4., 3.]], dtype=oneflow.float32) >>> indices tensor([[2, 3, 1], [1, 2, 3]], dtype=oneflow.int64) >>> values.shape oneflow.Size([2, 3]) >>> indices.shape oneflow.Size([2, 3]) >>> (values, indices) = flow.topk(flow.Tensor(x), k=2, dim=1, largest=False) >>> values tensor([[1., 2.], [1., 2.]], dtype=oneflow.float32) >>> indices tensor([[0, 4], [0, 4]], dtype=oneflow.int64) >>> values.shape oneflow.Size([2, 2]) >>> indices.shape oneflow.Size([2, 2])
-
transpose(dim0, dim1)¶
-
tril(diagonal=0)¶
-
triu(diagonal=0)¶ 参考
oneflow.triu()
-
type_as(target) → Tensor¶ 将
input的数据类型转换为target的数据类型。 如果input的数据类型已经和target的数据类型一致,则不做操作。- 参数:
input (Tensor): 输入张量
target (Tensor): 具有要转换类型的张量。
示例:
>>> import oneflow as flow >>> input = flow.randn(1, 2, 3, dtype = flow.float32) >>> target = flow.randint(0, 4, (5, 6)) >>> input = input.type_as(target) >>> input.dtype oneflow.int64
-
unbind(dim=0)¶
-
unfold(dimension, size, step)¶ 该接口与PyTorch一致。 该文件的参考来源是: https://pytorch.org/docs/stable/generated/torch.Tensor.unfold.html#torch.Tensor.unfold.
返回一个原始张量的视图,其中包含在维度 dimension 中来自 self 张量大小 size 的所有片段。
两片段之间的步长由 step 给出。
如果 sizedim 是 self 维度 dimension 的大小,那么返回的张量中的维度大小将是(sizedim - size / step + 1)。返回的张量将是(sizedim - size)/ step + 1。
在返回的张量中附加一个大小为 size 的额外维度。
- 参数:
dimension (int) - 展开的维度
size (int) - 展开的每一片段的大小
step (int) - 每片段之间的步骤
例如:
>>> import numpy as np >>> import oneflow as flow >>> x = flow.arange(1., 8) >>> x tensor([1, 2, 3, 4, 5, 6, 7], dtype=oneflow.int64) >>> x.unfold(0, 2, 1) tensor([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]], dtype=oneflow.int64) >>> x.unfold(0, 2, 2) tensor([[1, 2], [3, 4], [5, 6]], dtype=oneflow.int64)
-
uniform_(a=0, b=1)¶ Tensor.uniform_(from=0, to=1) → Tensor
用从连续均匀分布中采样的数字填充 self 张量。
\[P(x)=1/(to-from)\]
-
unsqueeze(dim)¶
-
var(dim=None, unbiased=True, keepdim=False)¶
-
view(input, *shape) → Tensor¶ 此接口与 PyTorch 一致。 文档参考自:https://pytorch.org/docs/stable/generated/torch.Tensor.view.html
返回一个新的 tensor ,其数据与
input相同,但形状shape不同。返回的 tensor 与
input共享相同的数据并且必须具有相同数量的元素,但是形状可以不同。 对于要被查看的 tensor ,新的视图大小必须与其原始大小和 step 兼容,每个新视角的维度必须为原始维度的子空间, 或者为跨越原始维度 \(d, d+1, \dots, d+k\) 的 span 满足以下类似邻接条件 \(\forall i = d, \dots, d+k-1\) 。\[\text{stride}[i] = \text{stride}[i+1] \times \text{size}[i+1]\]否则将无法以视图查看 为形状
shape且不复制input(例如通过contiguous())。 当不清楚是否可以执行view()时,建议使用reshape(),因为reshape()在兼容的时候返回input的视图,不兼容的时候复制input(相当于调用contiguous())。- 参数:
input (Tensor)
shape (flow.Size 或 int…)
- 返回类型:
与
input数据类型相同的 tensor
示例:
>>> 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 = input.view(2, 2, 2, -1).shape >>> y oneflow.Size([2, 2, 2, 2])
-
view_as(other) → Tensor¶ 将这个张量扩大到与
other相同的大小。self.view_as(other)与self.view(other.size())是等价的。查阅
view()来获得更多关于view的信息。- 参数:
other (
oneflow.Tensor) - 结果张量与other的大小相同。
-
where(condition, x=None, y=None) → Tensor¶ 返回一个 tensor 其元素为从
x或y中依据condition的真实值选择的元素, 如果condition中的元素大于 0 ,则取x中的元素,否则取y的元素。Note
如果
x为 None 并且y为 None ,则 flow.where(condition) 等同于 flow.nonzero(condition, as_tuple=True) 。condition、x、y必须可互相广播。- 参数:
condition (IntTensor): 如果不为 0 则 yield x ,否则 yield y
x (Tensor 或 Scalar): 当
condition为 True 时,如果x为标量则为值,如果x不为标量则为在索引处选择的值y (Tensor 或 Scalar): 当
condition为 False 时,如果x为标量则为值,如果x不为标量则为在索引处选择的值
- 返回类型:
oneflow.tensor: 与
condition、x、y广播形状相同的 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)
-
property