2021SC@SDUSC
- 一、分析的代码片段
- 1、代码展示
- 2、代码作用
- 3、代码原理细究
- 1、Tensor,张量,
- 2、几个名词
- 二、类NormStyleCode在项目中的具体应用
- 使用部分:
- 三、感悟
class NormStyleCode(nn.Module):
def forward(self, x):
"""Normalize the style codes. //规范化样式代码
Args:
x (Tensor): Style codes with shape (b, c).
Returns:
Tensor: Normalized tensor.
"""
return x * torch.rsqrt(torch.mean(x**2, dim=1, keepdim=True) + 1e-8)
2、代码作用
如同注释说的,是将张量Tensor规范化,,方便后续的图片处理
3、代码原理细究 1、Tensor,张量,x (Tensor): Style codes with shape (b, c).
1) Tensor 实际上就是多维数组;深度学习的基础。
2)shape(b,c),
shape: number of rows and columns,shape是Tensord的一 个属性
3)参考:https://zhuanlan.zhihu.com/p/48982978
x * torch.rsqrt(torch.mean(x**2, dim=1, keepdim=True) + 1e-8)
//作用:具体将Tensor x 规范化的公式
1)torch:是个包,包含Tensor的数据结构及基于其上的多种数学操作
2)rsqrt()
简单来说就是对每个元素取平方根后再取倒数,
参考:[https://blog.csdn.net/weixin_47156261/article/details/116711216]
3)mean()
torch._C._VariableFunctions @overload def mean(input: Tensor,
dim: Sequence[str | ellipsis | None],
keepdim: bool = False,
'''dim 表示的是 沿着 dim 维度进行 求平均值'''
二、类NormStyleCode在项目中的具体应用
使用部分:
方法forward(self, x):在其他部分都有重载,是个非常重要的函数
1、通过代码提示、注释和网上的一些资料,还是可以大概知道代码段的具体
作用,没有想象中那么难。
2、整体性:要在project层级上看待某个类及其方法的作用。



