Pytorch基础
程序员文章站
2022-06-26 11:30:31
...
1、Tensor的数据类型
#torch.FloatTensor
#用于生成数据类型为浮点型的tensor;可以是列表,也可以是一个维度值
import torch
a = torch.FloatTensor(2,3)
b = torch.FloatTensor([2,3,4,5])
print(a)
print(b)
tensor([[4.3911e-05, 2.5837e-06, 1.4013e-45],
[0.0000e+00, 1.4013e-45, 0.0000e+00]])
tensor([2., 3., 4., 5.])
#torch.IntTensor
#用于生成数据类型为整型的tensor;可以是列表也可以是一个维度值
import torch
a = torch.IntTensor(2,3)
b = torch.IntTensor([2,3,4,5])
print(a)
print(b)
tensor([[0, 0, 1],
[0, 1, 0]], dtype=torch.int32)
tensor([2, 3, 4, 5], dtype=torch.int32)
#torch.rand
#用于生成数据类型为浮点型维度指定的随机Tensor,数据再0~1区间均匀分布
import torch
a = torch.rand(2,3)
print(a)
tensor([[0.6461, 0.9960, 0.6996],
[0.8205, 0.9701, 0.1077]])
#torch.randn
#用于生成数据类型为浮点型且维度指定的随机Tensor,随机生成的浮点数的值满足均值为0、方差为1的正太分布
import torch
a = torch.randn(2,3)
print(a)
tensor([[ 2.5009, 1.9747, -0.7621],
[-1.5001, -1.9020, -0.7367]])
#torch.range(start,end,stride)
#这个版本使用会提示出错,可以使用torch.arange()
#数据类型为浮点型,起始值、范围的结束值和步长,步长用于指定从起始值到结束值得每步得数据间隔
import torch
a = torch.arange(1,20,1)
print(a)
tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19])
#torch.zeros
#用于生成数据类型为浮点型且维度指定的Tensor,全为0
import torch
a = torch.zeros(2,3)
print(a)
tensor([[0., 0., 0.],
[0., 0., 0.]])
2、Tensor运算
可以对tensor进行一些变量运算,来组合一些简单或者复杂的算法
#torch.abs
#绝对值操作
import torch
a = torch.randn(2,3)
print(a)
b = torch.abs(a)
print(b)
tensor([[ 0.4567, -0.7084, 0.6732],
[-0.1820, -0.5106, -0.2402]])
tensor([[0.4567, 0.7084, 0.6732],
[0.1820, 0.5106, 0.2402]])
#torch.add:
#将两个变量进行相加操作,可以都是变量,或者一个变量一个标量
import torch
a = torch.randn(2,3)
print(a)
b = torch.add(2,3)
print(b)
c = torch.randn(2,3)
print(c)
d = torch.add(c,10);
print(d)
e = torch.add(c,a)
print(e)
tensor([[-0.5469, -0.1745, -0.1805],
[ 1.2738, 1.2487, 1.0765]])
tensor(5)
tensor([[ 0.5091, -0.2509, 1.0934],
[ 0.5372, -0.3160, -0.4407]])
tensor([[10.5091, 9.7491, 11.0934],
[10.5372, 9.6840, 9.5593]])
tensor([[-0.0379, -0.4255, 0.9129],
[ 1.8110, 0.9328, 0.6358]])
#torch.clamp(待裁剪的变量,上边界,下边界)
#对参数在指定范围进行裁剪操作
import torch
a = torch.randn(2,3)
print(a)
b = torch.clamp(a,-2,0.1)
print(b)
tensor([[ 0.2326, -0.9193, -0.2051],
[-1.1646, 0.4585, -0.7207]])
tensor([[ 0.1000, -0.9193, -0.2051],
[-1.1646, 0.1000, -0.7207]])
#torch.div
#将参数传递到torch.div后返回参数求商的结果
import torch
a = torch.randn(2,3)
print(a)
b = torch.randn(2,3)
print(b)
c = torch.div(a,b)
print(c)
d = torch.randn(2,3)
print(d)
e = torch.div(d,10)
print(e)
tensor([[ 0.8628, -0.6982, 1.8959],
[ 2.0429, -1.3072, 0.6143]])
tensor([[ 1.1124, -0.8019, -0.6956],
[-1.3661, 0.7264, -2.0969]])
tensor([[ 0.7756, 0.8707, -2.7255],
[-1.4955, -1.7997, -0.2929]])
tensor([[ 1.0224, -1.5853, 2.2047],
[-0.0252, -0.0237, -0.4011]])
tensor([[ 0.1022, -0.1585, 0.2205],
[-0.0025, -0.0024, -0.0401]])
#torch.mul
#返回乘积的结果
import torch
a = torch.randn(2,3)
print(a)
b = torch.randn(2,3)
print(b)
c = torch.mul(a,b)
print(c)
d = torch.randn(2,3)
print(d)
e = torch.mul(d,10)
print(e)
tensor([[0.5938, 0.9085, 1.9268],
[1.8739, 1.5410, 0.4270]])
tensor([[ 0.4848, -0.4941, 1.5471],
[ 0.2296, 1.1991, 0.9234]])
tensor([[ 0.2879, -0.4489, 2.9809],
[ 0.4303, 1.8478, 0.3943]])
tensor([[-0.0193, 0.6371, -0.9642],
[-1.1746, 1.3586, -0.8532]])
tensor([[ -0.1926, 6.3712, -9.6417],
[-11.7459, 13.5864, -8.5325]])
#torch.pow
#返回输入参数的求幂结果作为输出
import torch
a = torch.IntTensor([2,3,4])
print(a)
b = torch.pow(a,2)
print(b)
tensor([2, 3, 4], dtype=torch.int32)
tensor([ 4, 9, 16], dtype=torch.int32)
#torch.mm
#返回矩阵相乘的结果
#两者的维度必须满足矩阵乘积的条件
import torch
a = torch.randn(2,3)
print(a)
b = torch.randn(3,2)
print(b)
c = torch.mm(a,b)
print(c)
tensor([[-0.9642, 1.4570, -0.0293],
[-0.2923, 0.4503, -0.5410]])
tensor([[-0.9322, -0.8108],
[ 1.3484, 0.8090],
[ 0.8773, 0.2482]])
tensor([[2.8378, 1.9531],
[0.4051, 0.4670]])
#torch.mv(矩阵,向量)
#返回输入参数的求积结果
import torch
a = torch.randn(2,3)
print(a)
b = torch.randn(3)
print(b)
c = torch.mv(a,b)
print(c)
tensor([[ 1.5491, -0.0762, -2.0008],
[ 1.1197, -1.2172, -0.0780]])
tensor([0.6538, 0.2944, 0.7295])
tensor([-0.4692, 0.3168])
上一篇: 【PyTorch基础】