我想在 Keras 中实现 ResNet 网络,并根据原始论文,在特征/ channel 尺寸不匹配时添加零条目的快捷连接:
When the dimensions increase (dotted line shortcuts in Fig. 3), we consider two options: (A) The shortcut still performs identity mapping, with extra zero entries padded for increasing dimensions ... http://arxiv.org/pdf/1512.03385v1.pdf
但是无法实现它,而且我似乎无法在网络或源代码上找到答案。我发现的所有实现都使用 1x1 卷积技巧在尺寸不匹配时进行快捷连接。
我想要实现的层基本上会将输入张量与全零张量的张量连接起来,以补偿维度不匹配。
这个想法是这样的,但我无法让它发挥作用:
def zero_pad(x, shape):
return K.concatenate([x, K.zeros(shape)], axis=1)
有人知道如何实现这样的层吗?
非常感谢
最佳答案
问题已在 github 上得到解答: https://github.com/fchollet/keras/issues/2608
它会是这样的:
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Lambda
from keras import backend as K
def zeropad(x):
y = K.zeros_like(x)
return K.concatenate([x, y], axis=1)
def zeropad_output_shape(input_shape):
shape = list(input_shape)
assert len(shape) == 4
shape[1] *= 2
return tuple(shape)
def shortcut(input_layer, nb_filters, output_shape, zeros_upsample=True):
# TODO: Figure out why zeros_upsample doesn't work in Theano
if zeros_upsample:
x = MaxPooling2D(pool_size=(1,1),
strides=(2,2),
border_mode='same')(input_layer)
x = Lambda(zeropad, output_shape=zeropad_output_shape)(x)
else:
# Options B, C in ResNet paper...
关于python - 当 channel 数增加时,ResNet 快捷连接的零填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37031106/