如果将仅包含零的数组输入到Conv2D图层,则输出也应仅包含零。在TensorFlow中就是这种情况。但是,在PyTorch中不是。这是一些非常简单的Python示例代码来演示这一点。在这种情况下,为什么PyTorch输出非零数字?
import torch
import numpy as np
image = np.zeros((3,3,3), dtype=np.float32)
batch = np.asarray([image])
a = torch.nn.Conv2d(3,3,1)
b = a(torch.tensor(batch).permute(0,3,1,2))
print(b.permute(0,2,3,1))
与Tensorflow不同,PyTorch使用非零值初始化偏差(请参见源代码):
def reset_parameters(self) -> None:
init.kaiming_uniform_(self.weight, a=math.sqrt(5))
if self.bias is not None:
fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)
bound = 1 / math.sqrt(fan_in)
init.uniform_(self.bias, -bound, bound)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句