一、Tensor概念
- 标量:是一个常数,为0维张量
- 向量:是一行或者一列数组成,为1维张量
- 矩阵:包含行和列两个维度。是2维张量。
torch.Tensor包含的属性:
- dtype:张量的数据类型,如torch.FloatTensor
- shape:张量的形状,如(64, 3, 224, 224)
- device:张量所在的设备,GPU/CPU
- data:被包装的Tensor
- grad:data的梯度
- grad_fn:创建Tensor的Function,是自动求导的关键
- requires_grad:指示是否需要梯度
- is_leaf:指示是否是叶子结点
二、Tensor创建:直接创建
1. torch.tensor(data, dtype=None, device=None)
功能:从data创建tensor
参数:
- data:数据,可以是list,numpy
- dtype:数据类型,默认与data一致
- device:所在设备,cpu或gpu
- requires_grad:是否需要梯度
- pin_memory:是否存于锁页内存
import torch import numpy as np arr = np.ones((3, 3)) print("arr的数据类型:", arr.dtype) t = torch.tensor(arr) print("t的数据类型:", t.dtype) arr的数据类型: float64 t的数据类型: torch.float64
2. torch.from_numpy(ndarray)
功能: 从numpy创建tensor
参数:
- ndarray:numpy数据
**注意事项:**用此方法创建的tensor与原ndarray共享内存,当修改其中要给的数据,另外要给也将会被改动。
import torch import numpy as np # 创建ndarray数组 arr = np.array([[1, 2, 3], [4, 5, 6]]) # 转化为tensor t = torch.from_numpy(arr) print("numpy array:", arr) print('tensor', t) # 修改arr arr[0, 0] = 0 print("numpy array:", arr) print('tensor', t) numpy array: [[1 2 3] [4 5 6]] tensor tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.int32) numpy array: [[0 2 3] [4 5 6]] tensor tensor([[0, 2, 3], [4, 5, 6]], dtype=torch.int32)
三、Tensor创建:依据数值创建
1. torch.zeros()
功能: 依据size创建全0张量
参数:
- size:张量的形状
- out:输出的张量,将生成的张量复制给其他变量
- layout:内存中布局形式
- device:所在设备
- requires_grad:是否需要梯度
>>> out_t = torch.tensor([1]) >>> t = torch.zeros((3, 3), out=out_t) >>> print(t, '\n', out_t) tensor([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) tensor([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) >>> print(id(t), id(out_t), id(t) == id(out_t)) 2873909788608 2873909788608 True
2. torch.zeros_like()
功能: 依input形状创建全0张量
参数:
- input:创建与input同形状的全0张量
- dtype:数据类型
- layout:内存中布局形式
>>> input = torch.tensor((2, 2)) >>> torch.zeros_like(input) tensor([0, 0])
3. torch.ones()
功能: 创建全为1的张量
参数:
- size:张量的形状
- out:输出的张量,将生成的张量复制给其他变量
- layout:内存中布局形式
- device:所在设备
- requires_grad:是否需要梯度
4. torch.ones_like()
功能: 依input形状创建全1张量
参数:
- input:创建与input同形状的全0张量
- dtype:数据类型
- layout:内存中布局形式
5. torch.full()
功能: 依据fill_value的值,创建形状为size的张量。
参数:
- size:张量的形状
- fill_value:张量的值
>>> torch.full((3, 3), 2) tensor([[2., 2., 2.], [2., 2., 2.], [2., 2., 2.]])
6. torch.full_like()
功能: 创建值为fill_value,形状与input相同的张量
参数:
- input:张量的形状
- fill_value:张量的值
>>> input = torch.zeros((3, 3)) >>> torch.full_like(input, 7) tensor([[7., 7., 7.], [7., 7., 7.], [7., 7., 7.]])
7. torch.arange()
功能: 创建等差的1维张量
参数:
- start:数列起始值
- end:数列结束值
- step:数列公差,默认为1
注意事项: 数列区间为[start, end)
>>> t = torch.arange(2, 10, 2) >>> print(t) tensor([2, 4, 6, 8])
8. torch.linspace()
功能: 创建均分的1维张量
参数:
- start:数列起始值
- end:数列结束值
- step:数列长度
注意事项: 数值区间为[start, end]
>>> torch.linspace(1, 10, 10) tensor([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
9. torch.logspace()
功能: 创建对数均分的1维张量
参数:
- start:数列起始值
- end:数列结束值
- step:数列长度
- base:对数函数的底,默认为10
注意事项: 长度为steps,底为base
>>> torch.logspace(1, 10, 5) tensor([1.0000e+01, 1.7783e+03, 3.1623e+05, 5.6234e+07, 1.0000e+10])
10. torch.eye()
功能: 创建单位对角矩阵(2维张量)
参数:
- n:矩阵行数
- m:矩阵列数
注意事项: 默认为方阵
>>> torch.eye(4, 4) tensor([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]])
四、Tensor创建:依据概率创建
1. torch.normal()
功能: 生成正态分布(高斯分布)
参数:
- mean:均值
- std:标准差
# mean为张量,std为张量 >>> mean = torch.arange(1, 5, dtype=torch.float) >>> std = torch.arange(1, 5, dtype=torch.float) >>> t_normal = torch.normal(mean, std) >>> print("mean:{}\nstd:{}".format(mean, std)) mean:tensor([1., 2., 3., 4.]) std:tensor([1., 2., 3., 4.]) >>> print(t_normal) tensor([ 0.6897, -0.1125, 1.2535, -1.3677])
2. torch.randn()
功能: 生成标准正态分布
参数:
- size:张量的形状
>>> torch.randn((3, 3)) tensor([[ 1.8161, 0.3164, -0.8697], [-0.6198, -0.4942, 0.7151], [-2.1771, 1.7853, -0.0611]])
3. torch.rand()
功能: 在区间[0,1)上,生成均匀分布
参数:
- size:张量的形状
>>> torch.rand((2, 2)) tensor([[0.6209, 0.6984], [0.7671, 0.7405]])
4. torch.randint()
功能: 在区间[low,high)上,生成整数均匀分布
参数:
- size:张量的形状
>>> torch.randint(1, 10, size=(3, 3)) tensor([[7, 5, 7], [4, 1, 1], [9, 1, 3]])
5. torch.randperm()
功能:
参数:
- n:张量的长度
>>> torch.randperm(10) tensor([3, 8, 9, 1, 7, 2, 4, 0, 5, 6])
6. torch.bernoulli(input)
功能: 以input为概率,生成伯努利分布
参数:
- input:概率值
>>> torch.bernoulli(torch.tensor([0.3])) tensor([0.]) >>> torch.bernoulli(torch.tensor([0.6])) tensor([1.])
来源:51CTO
作者:勤奋的小学生
链接:https://blog.csdn.net/gyt15663668337/article/details/102774929