遇到的PyTorch API

1. torch.chunk

torch.chunk(input, chunks, dim=0) → List of Tensors

将input tensor划分成特定的块数,每个块都是input tensor的一个视图,最后一个块可能会小一点,因为不能被dim整除。

  • input 输入tensor
  • chunks 返回多少个块
  • dim 沿着哪个维度进行切分
>>> import torch
>>> a = torch.zeros([6, 8 , 2, 2])
>>> x,y = a.chunk(chunks=2,dim=2)
>>> x.shape
torch.Size([6, 8, 1, 2])
>>> y.shape
torch.Size([6, 8, 1, 2])
>>>

2. nn.GroupNorm

BN是Batch维度上的归一化,GN就是Group维度上的归一化。

y = x − E [ x ] Var ⁡ [ x ] + ϵ ∗ γ + β y=\frac{x-\mathrm{E}[x]}{\sqrt{\operatorname{Var}[x]+\epsilon}} * \gamma+\beta y=Var[x]+ϵ xE[x]γ+β

torch.nn.GroupNorm(num_groups: int, num_channels: int, eps: float = 1e-05, affine: bool = True)

  • num_groups :组个数
  • num_channels: 输入tensor的channel个数
  • eps: 参数默认为1e-05
  • affine: 当设置为true,这个模块有可学习的每个通道的仿射参数初始化为1(权重)和0(偏差)。
>>> import torch
>>> a = torch.zeros([3,128,2,2])
>>> import torch.nn as nn
>>> m = nn.GroupNorm(4, 128, 1e-5, True)
>>> m(a).shape
torch.Size([3, 128, 2, 2])
>>>

3. torch.permute

返回维度排列后的原始张量的视图。

>>> x = torch.randn(2, 3, 5)
>>> x.size()
torch.Size([2, 3, 5])
>>> x.permute(2, 0, 1).size()
torch.Size([5, 2, 3])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

*pprp*

如果有帮助可以打赏一杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值