【基础】-tensor(张量)
一.创建一个tensor
1.只分配空间
x1 = torch.Tensor(5, 3) # 数据是随机的,只是分配了可见,没有初始化,所及数据是对应空间里的数据
print(x1)
输出:
tensor([[7.3470e-39, 8.9082e-39, 8.9082e-39],
[1.0194e-38, 9.1837e-39, 4.6837e-39],
[9.2755e-39, 1.0837e-38, 8.4490e-39],
[9.9184e-39, 9.9184e-39, 9.0000e-39],
[1.0561e-38, 1.0653e-38, 4.1327e-39]])
2.0-1分布
x2 = torch.rand(5, 3) # 使用[0,1]分布(默认的)随机初始化数据,构建(5, 3)shape的数组
print(x2)
输出:
tensor([[0.0786, 0.0817, 0.5277],
[0.3758, 0.9402, 0.2716],
[0.0723, 0.3258, 0.7880],
[0.9141, 0.7395, 0.1126],
[0.9843, 0.5128, 0.9107]])
3.直接创建
x3 = torch.tensor([ [1,2,3,4],
[5,6,7,8]])
print(x3)
输出:
tensor([[1, 2, 3, 4],
[5, 6, 7, 8]])
4.同一个数值
x4 = torch.full((5, 3), 6)
print(x4)
输出:
tensor([[6., 6., 6.],
[6., 6., 6.],
[6., 6., 6.],
[6., 6., 6.],
[6., 6., 6.]])
5.在一个区间内按一定步长
x5 = torch.arange(0, 100, 10) # 从0到99,按步长为10进行生成
print(x5)
输出:
tensor([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
或者:
在一定区间内均匀生成多少份,步长自动计算
x6 = torch.linspace(0, 100, 10) # 在0到100均匀生成10份
print(x6)
输出:
tensor([ 0.0000, 11.1111, 22.2222, 33.3333, 44.4444, 55.5556, 66.6667,
77.7778, 88.8889, 100.0000])
这里可见看出来使用torch.linspace包含end,0到100分成10个数,相对于在0到100的线段上均匀定9个点,100/9 = 11.111111······,所以,步长为11.11111.
如果生成11份,步长就为10了。
输出:
tensor([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.])
6.特殊矩阵
# 特殊矩阵
x7 = torch.zeros(5, 3) # 全0矩阵
print(x7)
x8 = torch.ones(5, 3) # 全1矩阵
print(x8)
x9 = torch.eye(3, 3) # 单位阵
print(x9)
x10 = torch.eye(5, 3) # 当输入shape不是方阵时,一部分是单位阵,多出来的部分为0
print(x10)
输出:
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.],
[0., 0., 0.]])
7.生成一个与已知tensor的shape相同的tensor
x11 = torch.rand_like(x10)
print(x11)
输出:
tensor([[0.2761, 0.7960, 0.0800],
[0.3035, 0.3525, 0.5574],
[0.3570, 0.5426, 0.4790],
[0.3504, 0.3996, 0.1984],
[0.5939, 0.3260, 0.6721]])
8.从numpy转
x12 = np.random.rand(5, 3)
print(x12)
x12 = torch.from_numpy(x12)
print(x12)
输出:
[[0.14858865 0.18512316 0.97759539]
[0.96559993 0.75191884 0.1561388 ]
[0.71575248 0.88542421 0.29086326]
[0.67362585 0.00512253 0.34022816]
[0.69759491 0.25110932 0.71962754]]
tensor([[0.1486, 0.1851, 0.9776],
[0.9656, 0.7519, 0.1561],
[0.7158, 0.8854, 0.2909],
[0.6736, 0.0051, 0.3402],
[0.6976, 0.2511, 0.7196]], dtype=torch.float64)
二.tensor的属性
x2 = torch.rand(5, 3) # 使用[0,1]分布(默认的)随机初始化数据,构建(5, 3)shape的数组
print(x2)
print(x2.size()) # (列,行)
print(x2.shape) # (列, 行)
print(x2.dtype) # 数据类型
输出:
tensor([[0.6725, 0.6270, 0.0352],
[0.0420, 0.4865, 0.7263],
[0.9950, 0.3957, 0.3868],
[0.3802, 0.3337, 0.0465],
[0.0089, 0.7211, 0.1279]])
torch.Size([5, 3])
torch.Size([5, 3])
torch.float32
三.tensor的操作
1.加
import torch
x1 = torch.randint(0, 10, (5, 3), dtype=torch.float)
print(x1)
x2 = torch.eye(5, 3)
print(x2)
# 1
print(x1 + x2)
# 2
print(torch.add(x1, x2))
# 3
print(x1.add(x2))
# 4
sum = torch.Tensor(5, 3) # 预先分配空间
torch.add(x1, x2, out=sum) # 加的结果保存在sum里
print(sum)
输出:
tensor([[5., 5., 8.],
[9., 3., 8.],
[9., 8., 3.],
[5., 5., 6.],
[6., 9., 5.]])
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.],
[0., 0., 0.]])
tensor([[6., 5., 8.],
[9., 4., 8.],
[9., 8., 4.],
[5., 5., 6.],
[6., 9., 5.]])
tensor([[6., 5., 8.],
[9., 4., 8.],
[9., 8., 4.],
[5., 5., 6.],
[6., 9., 5.]])
tensor([[6., 5., 8.],
[9., 4., 8.],
[9., 8., 4.],
[5., 5., 6.],
[6., 9., 5.]])
tensor([[6., 5., 8.],
[9., 4., 8.],
[9., 8., 4.],
[5., 5., 6.],
[6., 9., 5.]])
tensor1.add()和tensor1.add_()的区别
这个算是和python的特性一致,不加下划线的第一种就是普通的加法,不会改变tensor1的内容。而加了下划线,即第二种,加的结果会赋值给tensor1。
例子:
print(x1.add(x2))
print(x1)
print(x1.add_(x2))
print(x1)
输出:
tensor([[5., 8., 9.],
[2., 4., 3.],
[6., 2., 9.],
[3., 1., 3.],
[8., 5., 9.]])
tensor([[4., 8., 9.],
[2., 3., 3.],
[6., 2., 8.],
[3., 1., 3.],
[8., 5., 9.]])
tensor([[5., 8., 9.],
[2., 4., 3.],
[6., 2., 9.],
[3., 1., 3.],
[8., 5., 9.]])
tensor([[5., 8., 9.],
[2., 4., 3.],
[6., 2., 9.],
[3., 1., 3.],
[8., 5., 9.]])
其余运算
其他的减法,乘法,除法均可使用:-,*,/。同时也可以使用
torch.sub()
减法torch.mul()
乘法,只是简单的对应位置相乘,不是矩阵的乘法torch.div()
除法,对应位置相除
或者
tensor1.sub(tensor2)
tensor1.mul(tensor2)
tensor.div(tensor2)
上面三种加也可以加下划线,和add的例子相同。
其中,如果被除数为0,会出现这样的结果:
tensor([[9., inf, inf],
[inf, 6., inf],
[inf, inf, 0.],
[nan, inf, inf],
[inf, inf, inf]])
上面的三种运算中torch.sub()
这种也都可以添加out=
.
此外,还有很多其他的运算,基本都比较简单。
可能的一些错误
RuntimeError: expected backend CPU and dtype Float but got backend CPU and dtype Long
这种情况,根据提示信息就是数据的类型不对。
一般可以在创建tensor的时候添加dtype=torch.float
类似的语句来改。也可以使用tensor1.float()
来修改
查看tensor的数据类型时:使用tensor1.dtype