我在可视化CNN某层的feature map 的时候使用了这Pytorch torchvision.utils.make_grid 方法,结果在输出的时候发现有几个图完全黑了(数值过小)。我觉得不应该呀,Pytorch torchvision.utils.make_grid 有个 scale_each 函数,有它就说明将每个图单独正则化到了(0,1)区间。后来发现,原来哪些正常的图的范围是(-0.1 ~ 0.4),正则化没有问题,但是由于第3-5个图的范围是(1e-7 ~ 1e-9),相对于正则化代码的1e-5 太小了。原本这个 1e-5只是防止除以0加上的,现在反倒是影响到了正常的正则化
# 正则化源码
if normalize is True:
tensor = tensor.clone() # avoid modifying tensor in-place
if range is not None:
assert isinstance(range, tuple), \
"range has to be a tuple (min, max) if specified. min and max are numbers"
def norm_ip(img, min, max):
img.clamp_(min=min, max=max)
img.add_(-min).div_(max - min + 1e-5)
def norm_range(t, range):
if range is not None:
norm_ip(t, range[0], range[1])
else:
norm_ip(t, float(t.min()), float(t.max()))
if scale_each is True:
for t in tensor: # loop over mini-batch dimension
norm_range(t, range)
else:
norm_range(tensor, range)