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 type | dtype |
---|---|
32-bit floating point | torch.float32 or torch.float |
64-bit floating point | torch.float64 or torch.double |
16-bit floating point (1) | torch.float16 or torch.half |
16-bit floating point (2) | torch.bfloat16 |
32-bit complex | torch.complex32 or torch.chalf |
64-bit complex | torch.complex64 or torch.cfloat |
128-bit complex | torch.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 |
Boolean | torch.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.quint4x 2 |
8-bit floating point, e4m3 (5) | torch.float8_e4m3fn (limited support) |
8-bit floating point, e5m2 (5) | torch.float8_e5m2 (limited support) |
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 | torch.float16 or torch.half | torch.HalfTensor | torch.cuda.HalfTensor |
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 |
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/