Home > Article > Backend Development > Detailed example of tensor data structure in Pytorch
【Related recommendations: Python3 video tutorial】
torch.Tensor
is a multidimensional matrix containing elements of a single data type, similar to numpy's array.
Tensor can be generated using torch.tensor() to convert Python's list or
sequence data. The generated one is dtype and the default is
torch.FloatTensor.
Note1. A Tensor of a specified data type can be generated by passing parameterstorch.tensor()
always copies data. If you have a Tensor data and just want to change its
requires_gradproperties, use
requires_grad_()or
detach()to avoid copying. If you have a
numpyarray and want to avoid a copy, use
torch.as_tensor().
torch.dtype and/or
torch.device to the constructor:
Note that in order to change the torch.device and/or torch.dtype of an existing tensor, consider using the2, Tensor Content can be accessed and modified through Python indexing or slicing:to()
method.
>>> torch.ones([2,3], dtype=torch.float64, device="cuda:0") tensor([[1., 1., 1.], [1., 1., 1.]], device='cuda:0', dtype=torch.float64) >>> torch.ones([2,3], dtype=torch.float32) tensor([[1., 1., 1.], [1., 1., 1.]])
>>> matrix = torch.tensor([[2,3,4],[5,6,7]]) >>> print(matrix[1][2]) tensor(7) >>> matrix[1][2] = 9 >>> print(matrix) tensor([[2, 3, 4], [5, 6, 9]])3, using
torch.Tensor.item() or
int() methods from
only Get the Python Number from the Tensor of a value:
>>> x = torch.tensor([[4.5]]) >>> x tensor([[4.5000]]) >>> x.item() 4.5 >>> int(x) 44. Tensor can be created with the parameter
requires_grad=True, so
torch.autograd will record the relevant The operation realizes automatic derivation:
>>> 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]])5, each tensor has a corresponding
torch.Storage to save its data. The tensor class provides a multidimensional, strided view and defines numerical operations.
torch. Tensor is the abbreviation of the default tensor type (
torch.FloatTensor), which is the
32 bit floating point data type.
matrix = torch.tensor([[[1,2,3,4],[5,6,7,8]], [[5,4,6,7], [5,6,8,9]]], dtype = torch.float64) print(matrix) # 打印 tensor print(matrix.dtype) # 打印 tensor 数据类型 print(matrix.dim()) # 打印 tensor 维度 print(matrix.size()) # 打印 tensor 尺寸 print(matrix.shape) # 打印 tensor 尺寸 matrix2 = matrix.view(4, 2, 2) # 改变 tensor 尺寸 print(matrix2)The program output is as follows:
contiguous), while reshape() ) At the same time, you can also operate on tensors that do not meet the continuity conditions. When the tensor continuity condition (
contiguous) is satisfied, the result returned by a.reshape() is the same as a.view(), and no new memory space will be opened; if
contiguous is not satisfied When using the view() method directly, it will fail.
reshape() is still useful, but it will re-open the memory space and will not share the memory with the previous tensor, that is, it will return a
"copy" (Equivalent to calling the contiguous() method first and then using the
view() method).
For more understanding, refer to this article
.numpy() method to get a numpy array from a Tensor, or you can use
torch.from_numpy to get a Tensor from a numpy array. The Tensor and numpy arrays associated with these two methods share data memory. You can use the
clone method of the tensor to copy the tensor to break this association.
arr = np.random.rand(4,5) print(type(arr)) tensor1 = torch.from_numpy(arr) print(type(tensor1)) arr1 = tensor1.numpy() print(type(arr1)) """ <class 'numpy.ndarray'> <class 'torch.Tensor'> <class 'numpy.ndarray'> """2,
item() method and
tolist() method can convert tensors into Python numbers and lists of numbers
# item方法和tolist方法可以将张量转换成Python数值和数值列表 scalar = torch.tensor(5) # 标量 s = scalar.item() print(s) print(type(s)) tensor = torch.rand(3,2) # 矩阵 t = tensor.tolist() print(t) print(type(t)) """ 1.0 <class 'float'> [[0.8211846351623535, 0.20020723342895508], [0.011571824550628662, 0.2906131148338318]] <class 'list'> """
创建 tensor ,可以传入数据或者维度,torch.tensor() 方法只能传入数据,torch.Tensor() 方法既可以传入数据也可以传维度,强烈建议 tensor() 传数据,Tensor() 传维度,否则易搞混。
方法名 | 方法功能 | 备注 |
---|---|---|
torch.rand(*sizes, out=None) → Tensor |
返回一个张量,包含了从区间 [0, 1) 的均匀分布中抽取的一组随机数。张量的形状由参数sizes定义。 |
推荐 |
torch.randn(*sizes, out=None) → Tensor |
返回一个张量,包含了从标准正态分布(均值为0,方差为1,即高斯白噪声)中抽取的一组随机数。张量的形状由参数sizes定义。 | 不推荐 |
torch.normal(means, std, out=None) → Tensor |
返回一个张量,包含了从指定均值 means 和标准差 std 的离散正态分布中抽取的一组随机数。标准差 std 是一个张量,包含每个输出元素相关的正态分布标准差。 |
多种形式,建议看源码 |
torch.rand_like(a) |
根据数据 a 的 shape 来生成随机数据 |
不常用 |
torch.randint(low=0, high, size) |
生成指定范围(low, hight )和 size 的随机整数数据 |
常用 |
torch.full([2, 2], 4) |
生成给定维度,全部数据相等的数据 | 不常用 |
torch.arange(start=0, end, step=1, *, out=None) |
生成指定间隔的数据 | 易用常用 |
torch.ones(*size, *, out=None) |
生成给定 size 且值全为1 的矩阵数据 | 简单 |
zeros()/zeros_like()/eye() |
全 0 的 tensor 和 对角矩阵 |
简单 |
样例代码:
>>> torch.rand([1,1,3,3]) tensor([[[[0.3005, 0.6891, 0.4628], [0.4808, 0.8968, 0.5237], [0.4417, 0.2479, 0.0175]]]]) >>> torch.normal(2, 3, size=(1, 4)) tensor([[3.6851, 3.2853, 1.8538, 3.5181]]) >>> torch.full([2, 2], 4) tensor([[4, 4], [4, 4]]) >>> torch.arange(0,10,2) tensor([0, 2, 4, 6, 8]) >>> torch.eye(3,3) tensor([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
【相关推荐:Python3视频教程 】
The above is the detailed content of Detailed example of tensor data structure in Pytorch. For more information, please follow other related articles on the PHP Chinese website!