torch.Tensor - v1.5.0
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
是一種包含單一數據類型元素的多維矩陣。
Torch defines nine CPU tensor types and nine GPU tensor types:
Torch 定義了九種 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 | 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()
方法。