1、TORCH.TENSOR

torch.Tensor 是一个多维矩阵,包含单一数据类型的元素。

Data types

Torch 定义了 10 种具有 CPU 和 GPU 变体的张量类型,如下所示:

Data type

dtype

CPU tensor

GPU tensor

32-bit floating point

torch.float32 or torch.float

torch.FloatTensor

torch.cuda.FloatTensor

64-bit floating point

torch.float64 or torch.double

torch.DoubleTensor

torch.cuda.DoubleTensor

16-bit floating point 1

torch.float16 or torch.half

torch.HalfTensor

torch.cuda.HalfTensor

16-bit floating point 2

torch.bfloat16

torch.BFloat16Tensor

torch.cuda.BFloat16Tensor

32-bit complex

torch.complex32

  

64-bit complex

torch.complex64

  

128-bit complex

torch.complex128 or torch.cdouble

  

8-bit integer (unsigned)

torch.uint8

torch.ByteTensor

torch.cuda.ByteTensor

8-bit integer (signed)

torch.int8

torch.CharTensor

torch.cuda.CharTensor

16-bit integer (signed)

torch.int16 or torch.short

torch.ShortTensor

torch.cuda.ShortTensor

32-bit integer (signed)

torch.int32 or torch.int

torch.IntTensor

torch.cuda.IntTensor

64-bit integer (signed)

torch.int64 or torch.long

torch.LongTensor

torch.cuda.LongTensor

Boolean

torch.bool

torch.BoolTensor

torch.cuda.BoolTensor

quantized 8-bit integer (unsigned)

torch.quint8

torch.ByteTensor

/

quantized 8-bit integer (signed)

torch.qint8

torch.CharTensor

/

quantized 32-bit integer (signed)

torch.qfint32

torch.IntTensor

/

quantized 4-bit integer (unsigned) 3

torch.quint4x2

torch.ByteTensor

/

1.有时称为 binary16:使用 1 个符号、5 个指数和 10 个有效位。 当精度以牺牲范围为代价很重要时很有用。

2.有时称为脑浮点:使用 1 个符号、8 个指数和 7 个有效位。 当范围很重要时很有用,因为它具有与 float32 相同数量的指数位。

3.量化的 4 位整数存储为 8 位有符号整数。 目前只支持 EmbeddingBag 操作符。

torch.Tensor 是默认张量类型(torch.FloatTensor)的别名。

Initializing and basic operations

可以使用 torch.tensor() 构造函数从 Python 列表或序列构造张量:

>>> torch.tensor([[1., -1.], [1., -1.]])
tensor([[ 1.0000, -1.0000],
        [ 1.0000, -1.0000]])
>>> torch.tensor(np.array([[1, 2, 3], [4, 5, 6]]))
tensor([[ 1,  2,  3],
        [ 4,  5,  6]])

WARNING:torch.tensor() 总是复制数据。 如果您有张量数据并且只想更改其 requires_grad 标志,请使用 requires_grad_() 或 detach() 以避免复制。 如果您有一个 numpy 数组并希望避免复制,请使用 torch.as_tensor()。

可以通过将 torch.dtype 和/或 torch.device 传递给构造函数或张量创建操作来构造特定数据类型的张量:

>>> torch.zeros([2, 4], dtype=torch.int32)
tensor([[ 0,  0,  0,  0],
        [ 0,  0,  0,  0]], dtype=torch.int32)
>>> cuda0 = torch.device('cuda:0')
>>> torch.ones([2, 4], dtype=torch.float64, device=cuda0)
tensor([[ 1.0000,  1.0000,  1.0000,  1.0000],
        [ 1.0000,  1.0000,  1.0000,  1.0000]], dtype=torch.float64, device='cuda:0')

有关构建张量的更多信息,请参阅创建操作

可以使用 Python 的索引和切片符号访问和修改张量的内容:

>>> x = torch.tensor([[1, 2, 3], [4, 5, 6]])
>>> print(x[1][2])
tensor(6)
>>> x[0][1] = 8
>>> print(x)
tensor([[ 1,  8,  3],
        [ 4,  5,  6]])

使用 torch.Tensor.item() 从包含单个值的张量中获取 Python 数字:

>>> x = torch.tensor([[1]])
>>> x
tensor([[ 1]])
>>> x.item()
1
>>> x = torch.tensor(2.5)
>>> x
tensor(2.5000)
>>> x.item()
2.5

有关索引的更多信息,请参阅索引、切片、连接、变异操作。

可以使用 requires_grad=True 创建张量,以便 torch.autograd 记录对它们的操作以进行自动微分。

>>> x = torch.tensor([[1., -1.], [1., 1.]], requires_grad=True)
>>> out = x.pow(2).sum()
>>> out.backward()
>>> x.grad
tensor([[ 2.0000, -2.0000],
        [ 2.0000,  2.0000]])

每个张量都有一个关联的 torch.Storage,它保存着它的数据。 张量类还提供存储的多维跨步视图,并在其上定义数字操作。

有关张量视图的更多信息,请参阅Tensor Views。

有关 torch.Tensor 的 torch.dtype、torch.device 和 torch.layout 属性的更多信息,请参阅 Tensor Attributes。

改变张量的方法用下划线后缀标记。 例如, torch.FloatTensor.abs_() 就地计算绝对值并返回修改后的张量,而 torch.FloatTensor.abs() 在新张量中计算结果。

要更改现有张量的 torch.device 和/或 torch.dtype,请考虑在张量上使用 to() 方法。

WARNING:torch.Tensor 的当前实现引入了内存开销,因此它可能会导致在具有许多小张量的应用程序中意外高内存使用。 如果是这种情况,请考虑使用一种大型结构。

Tensor class reference

CLASStorch.Tensor

根据您的用例,有几种主要的方法可以创建张量。

.要使用预先存在的数据创建张量,请使用 torch.tensor()。

.要创建具有特定大小的张量,请使用 torch.* 张量创建操作(请参阅Creation Ops)。

.要创建与另一个张量具有相同大小(和相似类型)的张量,请使用 torch.*_like 张量创建操作(请参阅创建操作)。

.要创建与另一个张量类型相似但大小不同的张量,请使用 tensor.new_* 创建操作。

Tensor.T

这个张量的维度是否颠倒了。

如果 n 是 x 中的维数,则 x.T 等效于 x.permute(n-1, n-2, ..., 0)。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值