1.张量基本操作简介与创建
1.1 torch.zeros
import torch
out_t =torch.tensor([1])
t = torch.zeros((4,4),out=out_t)
print('t :', t, '\n','out_t :', out_t)
print(id(t),id(out_t))
output
t : tensor([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) out_t : tensor([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) 2777186850912 2777186850912
out_t 与t 共用内存
1.2 torch.ones
import torch
out_t =torch.tensor([1])
t = torch.ones((3,3),out=out_t)
print('t :', t, '\n', out_t)
print(id(t),id(out_t))
output t : tensor([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) tensor([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) 2777187916752 2777187916752
1.3 torch.full
import torch
out_t =torch.tensor([1])
t = torch.full((3,3),4,out=out_t)
print('t :', t, '\n', out_t)
print(id(t),id(out_t))
output
t : tensor([[4, 4, 4], [4, 4, 4], [4, 4, 4]]) tensor([[4, 4, 4], [4, 4, 4], [4, 4, 4]]) 2776837717504 2776837717504
1.4 torch.full_like
import torch
input = torch.empty(2,3)
t = torch.full_like(input,4)
print('t :', t, '\n', input)
output:
t : tensor([[4., 4., 4.], [4., 4., 4.]]) tensor([[7.5670e-44, 8.1275e-44, 6.7262e-44], [6.7262e-44, 8.1275e-44, 7.2868e-44]])
1.5 torch.arange 目的创建等差数列
import torch
t = torch.arange(1,21,2)
print('t :', t)
start = 1
end < 21
step =2
output
t : tensor([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19])
1.6 torch.linsapce 目的创建等差数列
import torch
t = torch.linspace(1,20,39)
print('t :', t)
start = 1
end <= 20 <<<< 注意,是闭合
step =39 代表 点数 共计 39点
t : tensor([ 1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000, 4.5000, 5.0000, 5.5000, 6.0000, 6.5000, 7.0000, 7.5000, 8.0000, 8.5000, 9.0000, 9.5000, 10.0000, 10.5000, 11.0000, 11.5000, 12.0000, 12.5000, 13.0000, 13.5000, 14.0000, 14.5000, 15.0000, 15.5000, 16.0000, 16.5000, 17.0000, 17.5000, 18.0000, 18.5000, 19.0000, 19.5000, 20.0000])
import torch
t = torch.linspace(1,40,40)
print('t :', t)
t : tensor([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., 40.])
1.7 torch.eye() 对角矩阵
import torch
t = torch.eye(5,6)
print('output t :', t)
row * col output t : tensor([[1., 0., 0., 0., 0., 0.], [0., 1., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0.], [0., 0., 0., 1., 0., 0.], [0., 0., 0., 0., 1., 0.]])
import torch
t = torch.eye(5)
print('output t :', t)
可省略 col output t : tensor([[1., 0., 0., 0., 0.], [0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.], [0., 0., 0., 1., 0.], [0., 0., 0., 0., 1.]])
1.8 torch.normal() 正态分布抽样,有多种方法
import torch
std = torch.arange(1,5,1,dtype = torch.float)
mean = torch.arange(1,5,1,dtype = torch.float)
t = torch.normal(mean,std)
print('output std :', std,'mean',mean,'t:',t)
output std : tensor([1., 2., 3., 4.]) mean tensor([1., 2., 3., 4.]) t: tensor([2.2844, 5.3268, 7.8541, 8.5367])
import torch
std = torch.arange(1,5,1,dtype = torch.float)
mean = torch.arange(1,5,1,dtype = torch.float)
t = torch.normal(0.,1.,size=(10,))
print('output std :', std,'mean',mean,'t:',t)
output std : tensor([1., 2., 3., 4.]) mean tensor([1., 2., 3., 4.]) t: tensor([ 0.6912, -0.0292, -1.9124, 0.4636, -1.4063, 0.3485, -1.1667, -0.3790, 1.1603, 0.4437])
import torch
std = torch.arange(1,5,1,dtype = torch.float)
mean = 1
t = torch.normal(mean,std)
print('output std :', std,'mean',mean,'t:',t)
output std : tensor([1., 2., 3., 4.]) mean 1 t: tensor([ 1.4478, 2.1242, -1.8429, -2.5465])
1.9 torch.randn() rand + normal
import torch
t = torch.randn(size=(4,))
print('output t:',t)
output t: tensor([ 0.3946, 1.7896, -0.5532, 0.3768])
t = torch.randn(size=(4,)) 与 t = torch.normal(0.,1.,size=(4,)) 是一样的
import torch
t = torch.rand(size=[40,])
print('output t:',t)
import torch
t = torch.rand(size=[40,])
print('output t:',t)
output t: tensor([0.3416, 0.5088, 0.2788, 0.0533, 0.7062, 0.0581, 0.7742, 0.2045, 0.1741, 0.8933, 0.1506, 0.4629, 0.8797, 0.1002, 0.5638, 0.5921, 0.0862, 0.8362, 0.8205, 0.6718, 0.8183, 0.0334, 0.7365, 0.1660, 0.8979, 0.1517, 0.3308, 0.2383, 0.1495, 0.9984, 0.4724, 0.7952, 0.6377, 0.3555, 0.8515, 0.2382, 0.7898, 0.4520, 0.4491, 0.8748])
import torch
input = torch.empty(2,3)
t = torch.rand_like(input)
print('output t:',t)
output t: tensor([[0.9993, 0.2470, 0.4895], [0.2251, 0.1244, 0.0183]])
还有 torch.randint() , torch.randint_like()
import torch
t = torch.randint(1,20,size=[40,])
print('output t:',t)
output t: tensor([ 8, 15, 3, 18, 17, 19, 2, 10, 6, 13, 10, 3, 2, 7, 15, 11, 7, 12, 14, 3, 3, 1, 18, 15, 10, 10, 2, 3, 11, 14, 18, 4, 17, 7, 14, 15, 7, 5, 10, 16])
import torch
t = torch.randperm(20)
print('output t:',t)
output t: tensor([12, 8, 17, 15, 10, 5, 11, 16, 7, 2, 6, 9, 4, 13, 18, 3, 1, 19, 0, 14])
生成 0-19 乱序的整数索引
import torch
input = torch.empty(2,10)
t = torch.bernoulli(input,0.2)
print('output t :', t)
output t : tensor([[1., 0., 0., 0., 0., 0., 1., 1., 1., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0., 1.]])
生成 2x10 矩阵 0,1 概率0.2