Pytorch基础语法:张量tensor

TORCH.TENSOR

torch.Tensor is a multi-dimensional matrix containing elements of a single data type.

张量是一个包含单一数据类型元素的多维矩阵。

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

Data typedtypeCPU tensorGPU 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

Sometimes referred to as binary16: uses 1 sign, 5 exponent, and 10 significand bits. Useful when precision is important at the expense of range.

有时也称为binary16:使用1个符号,5个指数和10个有效位。当以牺牲距离为代价求精度时很有用。

2

Sometimes referred to as Brain Floating Point: uses 1 sign, 8 exponent, and 7 significand bits. Useful when range is important, since it has the same number of exponent bits as float32

有时被称为脑浮点数:使用1个符号,8个指数,7个有效位:Four pieces当range很重要时很有用,因为它的指数位数与float32相同

3

quantized 4-bit integer is stored as a 8-bit signed integer. Currently it’s only supported in EmbeddingBag operator.

torch.Tensor is an alias for the default tensor type (torch.FloatTensor).

量化的4位整数存储为8位有符号整数。目前它只支持在embedded bag操作系统。torch.tensor是默认张量类型(torch.FloatTensor)。

初始化和基本操作

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

import torch
# print(torch.cuda.is_available())
print(torch.tensor([[1., -1.], [1., -1.]]))
print(torch.tensor(np.array([[1, 2, 3], [4, 5, 6]])))

运行结果:
D:\conda_env\python.exe D:/session9/test.py
tensor([[ 1., -1.],
Traceback (most recent call last):
        [ 1., -1.]])
  File "D:/session9/test.py", line 4, in <module>
    print(torch.tensor(np.array([[1, 2, 3], [4, 5, 6]])))
NameError: name 'np' is not defined

Process finished with exit code 1

出现错误:NameError: name 'np' is not defined

解决办法:使用import导入该模块进行使用,为了方便编写代码,在导入时同时给numpy一个别名。之后在使用该模块时,可以使用缩写的别名进行调用。

import numpy as np

注意:

torch.tensor() always copies data. If you have a Tensor data and just want to change its requires_grad flag, use requires_grad_() or detach() to avoid a copy. If you have a numpy array and want to avoid a copy, use torch.as_tensor().

A tensor of specific data type can be constructed by passing a torch.dtype and/or a torch.device to a constructor or tensor creation op:

特定数据类型的张量可以通过传递一个torch.dtyper来构造或一个torch.device到构造函数或张量创建操作:

import torch
import numpy as np
# print(torch.cuda.is_available())
print(torch.tensor([[1., -1.], [1., -1.]]))
print(torch.tensor(np.array([[1, 2, 3], [4, 5, 6]])))
print(torch.zeros([2, 4], dtype=torch.int32))#定义一个两行四列的零矩阵
cuda0 = torch.device('cuda:0')
print(torch.ones([2, 4], dtype=torch.float64, device=cuda0))

运行结果:
D:\conda_env\python.exe D:/session9/test.py
tensor([[ 1., -1.],
        [ 1., -1.]])
tensor([[1, 2, 3],
        [4, 5, 6]], dtype=torch.int32)
tensor([[0, 0, 0, 0],
        [0, 0, 0, 0]], dtype=torch.int32)
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.]], device='cuda:0', dtype=torch.float64)

Process finished with exit code 0

如果你想了解 torch.device(“cuda”) 与 torch.device(“cuda:0”)之间的不同,点击下面链接:

Difference between torch.device("cuda") and torch.device("cuda:0") - PyTorch Forums

有关构建张量的更多信息,请参见  Creation Ops

张量的内容可以使用Python的索引和切片表示法来访问和修改:

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

运行结果:
D:\conda_env\python.exe D:/session9/test.py
tensor(6)

Process finished with exit code 0
import torch
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(x[1][2])
x[0][1] = 8
print(x)

运行结果:
D:\conda_env\python.exe D:/session9/test.py
tensor(6)
tensor([[1, 8, 3],
        [4, 5, 6]])

Process finished with exit code 0

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

import torch
x = torch.tensor([[1]])
print(x)
print(x.item())

运行结果:
D:\conda_env\python.exe D:/session9/test.py
tensor([[1]])
1

Process finished with exit code 0
import torch
x = torch.tensor(2.5)
print(x)
print(x.item())

运行结果:
D:\conda_env\python.exe D:/session9/test.py
tensor(2.5000)
2.5

Process finished with exit code 0

有关索引的更多信息,请参阅有关索引的更多信息,请参阅Indexing, Slicing, Joining, Mutating Ops

A tensor can be created with requires_grad=True so that torch.autograd records operations on them for automatic differentiation.

一个张量可以用requires_grade - true创建,这样火炬。Autograd记录对它们的操作,以便自动区分。

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

运行结果:
D:\conda_env\python.exe D:/session9/test.py
tensor([[ 2., -4.],
        [ 2.,  2.]])

Process finished with exit code 0

Each tensor has an associated torch.Storage, which holds its data. The tensor class also provides multi-dimensional, strided view of a storage and defines numeric operations on it.

每个张量都有一个相关的torch.Storage,用来保存数据。张量类还提供了多维的、跨越式的存储视图,并定义了对它的数值运算

备注:有关张量视图的更多信息,请参见 Tensor Views.

请注意了解更多关于torch.dtypetorch.device,和torch.layout。了解 torch.Tensor的属性参见 Tensor Attributes.

注意:改变张量的方法用下划线后缀标记。例如,torch.FloatTensor.abs()会计算che的绝对值并返回修改后的张量,而torch.FloatTensor.abs()会计算一个新张量的结果

说明:改变一个现有张量的torch.device and/or torch.dtype, 考虑在tenson上使用to()方法

警告:目前执行的torch.Tensor引入了内存开销,因此它可能会在有许多微小张量的应用程序中导致意想不到的高内存使用量。如果这是你的情况,考虑使用一个大型结构。

张量类引用

CLASS torch.Tensor

有几种主要的方法来创建一个张量,这取决于你的用例:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zkaisen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值