Pytorch资料

Pytorch

知识点

  1. Pytorch和Tensorflow的区别: 动态图还是静态图的计算。
    • Pytorch是动态图,可以在中间步骤调试。
    • Tensorflow是静态图。先定义好公式,然后给输入之后,直接进行计算。不方便调试。
      在这里插入图片描述
  2. 创建tensor
torch.from_numpy(array)
torch.tensor() #直接输入数据,float型
torch.FloatTensor(a,b,c) #一般输入维度,值为随机float型,初始化不稳定
torch.IntTensor() #同floatTensor,值为随机int型
torch.set_default_tensor_type(torch.DoubleTensor)
torch.Tensor() #默认类型
  1. 随机初始化tensor
a = torch.rand(3,3)
torch.rand_like(a)
torch.randint(1,10,[3,3]) #min,max,shape
torch.randn(3,3) #normal dist
torch.full([2,3],7) #shape value
torch.normal(mean=torch.full([10],0),std=torch.arange(1,0,-0.1)) #need to reshape
torch.linspace(0,10,steps=4)
torch.logspace(0,-1,steps=10) # 10的次幂
torch.ones([3,3])
torch.zeros([3,3])
torch.eye(3) #3*3对角为1的tensor
torch.randperm(10) #random shuffle
  1. 切片和索引
a = torch.tensor(4,3,28,28)
a[0].shape -> torch.size([3,28,28])
a[0,0].shape -> torch.size([28,28])
a[:2].shape -> torch.size([2,3,28,28])
a[:2,:-1,:,:].shape -> torch.size([2,1,28,28])
a.index_select(0,torch.tensor([0,2])).shape
-> torch.size([2,3,28,28])
a.index_select(1,torch.tensor([1,2])).shape
-> torch.size([4,2,28,28])
a[...].shape -> torch.size([4,3,28,28])
a[0,...].shape -> torch.size([3,28,28])
a[:,1,...].shape -> torch.size([4,28,28])
a[...,:2].shape -> torch.size([4,3,28,2])
mask = a.ge(0.5) # >=
torch.masked_select(a,mask) # mask output
torch.take(a,torch.tensor([0,2,3])) #打平后选取相应的元素

  1. 维度变换
a.shape -> torch.size([4,1,28,28])
a.view(4,28*28).shape -> torch.size([4,784]) # view=reshape

a.unsqueeze(0).shape -> torch.size([1,4,1,28,28])
a.unsqueeze(-5).shape -> torch.size([1,4,1,28,28])

a.squeeze(0).shape -> torch.size([1,28,28])
a.squeeze(-1).shape -> torch.size([4,1,28])
a.expand(4,3,28,28).shape -> torch.size([4,3,28,28])
a.expand(-1,5,-1,-1).shape -> torch.size([4,5,28,28]) # -1保持不变

a.repeat(2,5,2,2).shape -> torch.size([8,15,56,56])

a = torch.rand(3,4)
a.t().shape -> torch.size([4,3]) #2dim only

a.transpose(1,3).shape -> torch.size([4,28,28,1])
a.permute(0,2,3,1).shape -> torch.size([4,28,28,1])

  1. 合并与分割
a = torch.rand(4,32,8)
b = torch.rand(5,32,8)
torch.cat([a,b],dim=0).shape -> torch.size([9,32,8]) 
a = torch.rand(4,32,8)
b = torch.rand(4,32,8)
c = torch.stack([a,b],dim=0).shape -> torch.size([2,4,32,8]) #create new dim
aa, bb = c.split([1,1],dim=0) <=> aa, bb = c.chunk(2,dim=0)
aa.shape -> torch.size([1,4,32,8])
bb.shape -> torch.size([1,4,32,8])
  1. 数学计算
torch.add()
torch.sub()
torch.mul()
torch.div()
torch.matmul(a,b) <=> a@b
a = torch.full([2,2],3)
a = a.pow(2)
a.sqrt()
torch.exp()
torch.log()
a = torch.tensor(3.14)
a.ceil() a.floor() a.trunc() a.frac()

  1. 属性统计
a = torch.arange(8).view(2,4).float()
a.min() a.sum() a.mean() a.prod()
a.argmax() a.argmin()
a.shape -> torch.size([4,10])
a.max(dim=1,keepdim=True)
a.topk(n,dim=1,largest=True/False) # false from minimum
a.kthvalue(n,dim=1) #第k小
torch.eq()
torch.gt()
torch.where(cond>0.5,a,b) #cond满足条件的位置使用a的相应元素,不满足的地方选择b的相应元素
  1. 非常好的pytorch教程。
    https://github.com/yunjey/pytorch-tutorial
    在这里插入图片描述

  2. Vizdom使用
    terminal 输入 python -m visdom.server

viz =  visdom.Visdom()
db = Pokemon('./dataset',224,'train')
x,y = next(iter(db))
viz.image(x, win='sample_x', opts=dict(title='sample_x'))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值