类K.zeros_(x)的K.int_形

2024-04-20 08:53:31 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的自定义填充层:

   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)

docinstantiates 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)]

Tags: buildselfnoneinputoutputinitdefzeros
2条回答

应使用keras.backend.concatenate而不是keras.layers.concatenate

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):
        res = K.concatenate([x, K.zeros_like(x)], axis=-1)
        return res

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

没什么不对的。你知道吗

如果你能用axis=-1连接起来,你可以确定所有的三个第一维度是相等的。你知道吗

现在,在tensorflow和/或keras中可能有一些内在的怪癖,也许是为了让事情变得更快,也许是为了让它们灵活地适应不同的尺寸。没什么大不了的。你知道吗

如果想要得到当前值的真实形状,需要计算(K.eval())张量K.shape(x)。但评估不能在层内进行。必须像做预测一样去做。你知道吗

错误消息

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

相关问题 更多 >