首页 > 解决方案 > K.zeros_like(x) 的 K.int_shape

问题描述

这是我的自定义填充层:

   class CustomZeroPadding2D(Layer):
        def __init__(self, **kwargs):
            super(CustomZeroPadding2D, self).__init__(**kwargs)

        def build(self, input_shape):
            super(CustomZeroPadding2D, self).build(input_shape)

        def call(self, x):
            print('K.int_shape(x)', K.int_shape(x))
            print('K.int_shape(K.zeros_like(x))', K.int_shape(K.zeros_like(x)))
            res = concatenate([x, K.zeros_like(x)], axis=-1)
            return res

        def compute_output_shape(self, input_shape):
            output_shape = (input_shape[0], input_shape[1], input_shape[2]*2)
            return output_shape

由于某些原因:

K.int_shape(x) (None, 128, 128, 7)

K.int_shape(K.zeros_like(x)) (None, None, None, 7)

doc instantiates an all-zeros variable of the same shape as another tensor中,有什么问题?

更新:

串联不起作用的问题:

ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 128, 128, 7), (None, None, None, 7)]

标签: pythonkerasdeep-learning

解决方案


没有任何错误。

如果您能够与 连接axis=-1,则可以确定所有三个第一个维度都是相等的。

现在,在 tensorflow 和/或 keras 中可能存在内部怪癖,也许是为了让事情变得更快,也许是为了让它们灵活地适应可变大小。没什么大不了的。

如果您想要当前值的真实形状,则需要评估 ( K.eval())K.shape(x)张量。但是评估不能在层内进行。它必须像预测一样进行。

错误信息

您正在使用连接。你应该使用keras.backend.concatenate([...], axis=-1)


推荐阅读