Pytorch学习笔记
防止自己写不动代码的时候,啥都没了
一、Tensor关系
二、Tensor创建方式
1、直接法
import numpy as np
import torch
flag=True
if flag:
arr=np.ones((3,3))
print('ndarray的数据类型:',arr.dtype)
t=torch.tensor(arr,device='cuda')
print(t)
结果:
ndarray的数据类型: float64
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], device='cuda:0', dtype=torch.float64)
2、依据数值创建
1)通过torch.from_numpy 创建张量
if flag:
arr=np.array([[1,2,3],[4,5,6]])
t=torch.from_numpy(arr)
print('numpy array: ',arr)
print('tensor:',t)
结果:
ndarray的数据类型: float64
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], device='cuda:0', dtype=torch.float64)
2)通过torch.zeros创建张量
flag=True
if flag:
out_t=torch.tensor([1])
t=torch.zeros((3,3),out=out_t)
print(t,'\n',out_t)
print(id(t),id(out_t),id(t)==id(out_t)) #查看变量内存地址
结果:
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
2921141821912 2921141821912 True
3)通过torch.full创建全1的张量
flag=True
if flag:
out_t=torch.tensor([1])
t=torch.zeros((3,3),out=out_t)
print(t,'\n',out_t)
print(id(t),id(out_t),id(t)==id(out_t)) #查看变量内存地址
结果:
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
2921141821912 2921141821912 True
4)通过torch.arange() 创建等差的1维张量
flag=True
if flag:
t=torch.arange(2,10,2)
print(t)
结果:
tensor([2, 4, 6, 8])
5)通过torch.linspace() 创建均分数列张量
flag=True
if flag:
# t=torch.linspace(2,10,5) #5是步长
t=torch.linspace(2,10,6) #步长=(10-2)/(6-1)
print(t)
结果:
tensor([ 2.0000, 3.6000, 5.2000, 6.8000, 8.4000, 10.0000])
6)通过torch.eye() 对角矩阵
flag=True
if flag:
t=torch.eye(5) #5*5矩阵
print(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.]])
3、依据概率创建
1)通过torch.normal()生成符合正态分布的样本
(1)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))
print(t_normal)
结果:
mean:tensor([1., 2., 3., 4.])
std:tensor([1., 2., 3., 4.])
tensor([ 0.0372, -1.5665, 5.0527, 9.0701])
(2)mean:标量;std:张量
t_normal = torch.normal(0.,1.,size=(4,)) #标准正态采样 size是张量的大小
print(t_normal)
结果:
tensor([-1.0844, -0.2280, 2.2131, 0.6070])
(3)mean:张量;std:标量
mean=torch.arange(1,5,dtype=torch.float)
std=1
t_normal=torch.normal(mean,std)
print('mean:{}\nstd:{}'.format(mean,std)) #均值不同,标准差相同情况下采样
print(t_normal)
结果:
mean:tensor([1., 2., 3., 4.])
std:1
tensor([1.1042, 2.1616, 3.4111, 2.8586])
(4)mean:标量;std:张量
std=torch.arange(1,5,dtype=torch.float)
mean=1
t_normal=torch.normal(mean,std)
print('mean:{}\nstd:{}'.format(mean,std)) #均值不同,标准差相同情况进行采样
print(t_normal)
结果:
mean:1
std:tensor([1., 2., 3., 4.])
tensor([ 1.2390, -1.3126, -1.6846, 0.1629])
2)通过torch.randn()生成符合正态分布的样本
返回一个张量,包含了从标准正态分布(均值为0,方差为 1,即高斯白噪声)中抽取一组随机数,形状由可变参数sizes定义。
r=torch.rand(5) #5是定义形状,1列5行
print(r)
结果:
tensor([-1.1846, -0.1723, 0.3694, -1.2236, 1.1645])
(1)通过torch.randn_like()生成标准均匀分布
生成同形状的随机矩阵
x=torch.eye(4)
y = torch.randn_like(x, dtype=torch.float)
print('x: ',x)
print('y: ',y)
结果:
x: tensor([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
y: tensor([[ 0.3008, 2.0123, 0.3686, 1.1208],
[-1.1974, -0.3093, 1.1528, -0.4598],
[ 0.1969, -0.3700, 0.6235, -0.6877],
[-0.0422, 0.5305, -0.6940, 2.2808]])
3)通过torch.rand()生成均匀分布
返回一个张量,包含了从区间[0,1)的均匀分布中抽取的一组随机数,形状由可变参数sizes 定义。
r=torch.rand(5) #5是定义形状,1列5行
print(r)
print(r.shape) #torch.Size([5])
结果:
tensor([0.9576, 0.3601, 0.5376, 0.5844, 0.7239])
(2)通过torch.rand_like()生成均匀分布
根据已有的Tensor创建一个新的Tensor
x=torch.ones(5)
y=torch.rand_like(x,dtype=torch.float64)
print(x)
print(y)
结果:
x: tensor([1., 1., 1., 1., 1.])
y: tensor([0.0921, 0.1945, 0.4174, 0.9191, 0.8364], dtype=torch.float64)
(3)通过torch.randint()生成一定范围的随机整数
自定义最大值和最小值,根据形状创建随机数,范围是-5到5,10个随机数
x=torch.randint(-5,5,(10,))
print(x)
结果:
tensor([-2, -4, 4, 3, 2, 2, 2, -1, -3, 1])
4)通过torch.randperm()生成0到n-1的随机排列
r=torch.randperm(5) #5是定义形状,1列5行
print(r)
结果:
tensor([4, 0, 3, 1, 2])
5)通过torch.bernoulli()生成伯努利分布也就是0-1分布
需要注意,输入的参数必须是tensor
a = torch.Tensor(3, 3).uniform_(0, 1)
#uniform_是指生成一个0,1范围的随机矩阵
r=torch.bernoulli(a)
print(r)
结果:
tensor([[0., 0., 1.],
[1., 1., 1.],
[1., 0., 1.]])
心得:虽然我做完这里记录后,感觉好像没什么用,但是做了就是一种收获与回顾,即便以后忘了,也不后悔此时所谓的无用功