PyTorch torch.Tensor

本文详细介绍了PyTorch中的Tensor概念,包括不同数据类型和设备的Tensor类型,如何使用Python列表或NumPy数组创建Tensor,以及如何进行索引、切片和数值操作。同时,还讲解了如何使用requires_grad属性实现自动求导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PyTorch torch.Tensor

torch.Tensor
https://pytorch.org/docs/stable/tensors.html

A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.
torch.Tensor 是一种包含单一数据类型元素的多维矩阵。

1. Data types

Torch defines tensor types with the following data types:

Data typedtype
32-bit floating pointtorch.float32 or torch.float
64-bit floating pointtorch.float64 or torch.double
16-bit floating point (1)torch.float16 or torch.half
16-bit floating point (2)torch.bfloat16
32-bit complextorch.complex32 or torch.chalf
64-bit complextorch.complex64 or torch.cfloat
128-bit complextorch.complex128 or torch.cdouble
8-bit integer (unsigned)torch.uint8
16-bit integer (unsigned)torch.uint16 (limited support) (4)
32-bit integer (unsigned)torch.uint32 (limited support) (4)
64-bit integer (unsigned)torch.uint64 (limited support) (4)
8-bit integer (signed)torch.int8
16-bit integer (signed)torch.int16 or torch.short
32-bit integer (signed)torch.int32 or torch.int
64-bit integer (signed)torch.int64 or torch.long
Booleantorch.bool
quantized 8-bit integer (unsigned)torch.quint8
quantized 8-bit integer (signed)torch.qint8
quantized 32-bit integer (signed)torch.qint32
quantized 4-bit integer (unsigned) (3)torch.quint4x2
8-bit floating point, e4m3 (5)torch.float8_e4m3fn (limited support)
8-bit floating point, e5m2 (5)torch.float8_e5m2 (limited support)
Data typedtypeCPU tensorGPU tensor
32-bit floating pointtorch.float32 or torch.floattorch.FloatTensortorch.cuda.FloatTensor
64-bit floating pointtorch.float64 or torch.doubletorch.DoubleTensortorch.cuda.DoubleTensor
16-bit floating pointtorch.float16 or torch.halftorch.HalfTensortorch.cuda.HalfTensor
8-bit integer (unsigned)torch.uint8torch.ByteTensortorch.cuda.ByteTensor
8-bit integer (signed)torch.int8torch.CharTensortorch.cuda.CharTensor
16-bit integer (signed)torch.int16 or torch.shorttorch.ShortTensortorch.cuda.ShortTensor
32-bit integer (signed)torch.int32 or torch.inttorch.IntTensortorch.cuda.IntTensor
64-bit integer (signed)torch.int64 or torch.longtorch.LongTensortorch.cuda.LongTensor
Booleantorch.booltorch.BoolTensortorch.cuda.BoolTensor

torch.Tensor is an alias for the default tensor type (torch.FloatTensor).
torch.Tensor 是默认的 tensor 类型 (torch.FloatTensor) 的简称。

A tensor can be constructed from a Python list or sequence using the torch.tensor() constructor:
Tensor 可以用 torch.tensor() 转换 Python 的 list 或序列​​生成:

(pt-1.4_py-3.6) yongqiang@yongqiang:~$ python
Python 3.6.10 |Anaconda, Inc.| (default, May  8 2020, 02:54:21)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import numpy as np
>>>
>>> torch.tensor([[1., -1.], [1., -1.]])
tensor([[ 1., -1.],
        [ 1., -1.]])
>>>
>>> torch.tensor(np.array([[1, 2, 3], [4, 5, 6]]))
tensor([[1, 2, 3],
        [4, 5, 6]])
>>>
>>> exit()
(pt-1.4_py-3.6) yongqiang@yongqiang:~$

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().
torch.tensor() 总是拷贝 data。如果你有一个 Tensor data 并且仅仅想改变它的 requires_grad 属性, 可用 requires_grad_() or detach() 来避免拷贝。如果你有一个 numpy 数组并且想避免拷贝, 请使用 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:
指定数据类型的 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')

The contents of a tensor can be accessed and modified using Python’s indexing and slicing notation:
Tensor 的内容可以通过 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]])

Use torch.Tensor.item() to get a Python number from a tensor containing a single value:
使用 torch.Tensor.item() 从只有一个值的 Tensor 中获取 Python Number:

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

A tensor can be created with requires_grad=True so that torch.autograd records operations on them for automatic differentiation.
Tensor 可以通过参数 requires_grad=True 创建, 这样 torch.autograd 会记录相关的运算实现自动求导。

(pt-1.4_py-3.6) yongqiang@yongqiang:~$ python
Python 3.6.10 |Anaconda, Inc.| (default, May  8 2020, 02:54:21)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> data = torch.tensor([[1., -1.], [1., 1.]], requires_grad=True)
>>> data
tensor([[ 1., -1.],
        [ 1.,  1.]], requires_grad=True)
>>>
>>> data_pow = data.pow(2)
>>> data_pow
tensor([[1., 1.],
        [1., 1.]], grad_fn=<PowBackward0>)
>>>
>>> data_sum = data_pow.sum()
>>> data_sum
tensor(4., grad_fn=<SumBackward0>)
>>>
>>> data_sum.backward()
>>>
>>> data.grad
tensor([[ 2., -2.],
        [ 2.,  2.]])
>>> exit()
(pt-1.4_py-3.6) yongqiang@yongqiang:~$

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.
每一个 tensor 都有一个相应的 torch.Storage 保存其数据。tensor 类提供了一个多维的、strided 视图,并定义了数值操作。

Methods which mutate a tensor are marked with an underscore suffix. For example, torch.FloatTensor.abs_() computes the absolute value in-place and returns the modified tensor, while torch.FloatTensor.abs() computes the result in a new tensor.
修改 tensor 的方法可以用一个下划线后缀来标示。比如 torch.FloatTensor.abs_() 会在原地计算绝对值并返回修改的张量,而 torch.FloatTensor.abs() 将会在新张量中计算结果.

To change an existing tensor’s torch.device and/or torch.dtype, consider using to() method on the tensor.
为了改变已有的 tensor 的 torch.device 和/或 torch.dtype,考虑使用 to() 方法。

Current implementation of torch.Tensor introduces memory overhead, thus it might lead to unexpectedly high memory usage in the applications with many tiny tensors. If this is your case, consider using one large structure.
Torch.Tensor 的当前实现会引入内存开销,因此可能会在具有许多微小张量的应用程序中导致意外的高内存使用率。如果是这种情况,请考虑使用一种大型结构。

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

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

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

打赏作者

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

抵扣说明:

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

余额充值