代码之家  ›  专栏  ›  技术社区  ›  mrgloom

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

  •  0
  • mrgloom  · 技术社区  · 7 年前

    这是我的自定义填充层:

       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)]
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Community Mohan Dere    5 年前

    没有什么不对的。

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

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

    如果希望得到当前值的真实形状,则需要计算( K.eval() ) K.shape(x) 张量。但评估不能在层内完成。这必须像预测一样进行。

    . 你应该使用 keras.backend.concatenate([...], axis=-1)

        2
  •  0
  •   mrgloom    7 年前

    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
    
    推荐文章