| 90 | |
| 91 | #注入到网络中的噪声 |
| 92 | class InjectNoise(torch.nn.Module): |
| 93 | def __init__(self,channel): |
| 94 | super(InjectNoise, self).__init__() |
| 95 | #torch.nn.Parameter()将一个不可训练的tensor转换成可以训练的类型parameter, |
| 96 | # 并将这个parameter绑定到这个module里面。即在定义网络时这个tensor就是一个可 |
| 97 | # 以训练的参数了。使用这个函数的目的也是想让某些变量在学习的过程中不断的修改其值以达到最优化 |
| 98 | self.weight = torch.nn.Parameter(torch.zeros(1,channel,1,1)) |
| 99 | |
| 100 | def forward(self,x): |
| 101 | #产生的噪声,并且噪声的维度和x的维度是一样的 |
| 102 | noise = torch.randn(size = (x.shape[0],1,x.shape[2],x.shape[3]),device=x.device) |
| 103 | #这里之所以使用self.weight * noise表示将noise设置为可训练的参数 |
| 104 | out = x + self.weight * noise |
| 105 | return out |
| 106 | |
| 107 | #是一个Batch Normazliation |
| 108 | class AdaIN(torch.nn.Module): |