tf.Keras自定义图层的输出形状为None

时间:2020-03-09 10:14:37

标签: tensorflow keras

我正在构建自定义层,之后添加密集层时遇到输出形状问题。该层的输出形状似乎没有定义,即使我明确地定义了它也是如此。这是重现此问题的最少代码:

import tensorflow as tf
from tensorflow import keras

class fakeLayer(keras.layers.Layer):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

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

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

    def call(self, inputs):
        return(tf.reshape(inputs , self.compute_output_shape(tf.shape(inputs))))


inp = keras.layers.Input((32,32,3))
x = keras.layers.Conv2D(16, (3,3))(inp)
x = fakeLayer()(x)
# x = keras.layers.Flatten()(x)
# x = keras.layers.Dense(1)(x)
model = keras.models.Model(inputs= inp, outputs = x)

print(model.summary())

可以这样:

WARNING:tensorflow:Entity <bound method fakeLayer.call of <__main__.fakeLayer object at 0x0000021A7370E470>> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: 
WARNING: Entity <bound method fakeLayer.call of <__main__.fakeLayer object at 0x0000021A7370E470>> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: 
Model: "model_9"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_37 (InputLayer)        [(None, 32, 32, 3)]       0         
_________________________________________________________________
conv2d_10 (Conv2D)           (None, 30, 30, 16)        448       
_________________________________________________________________
fake_layer_28 (fakeLayer)    (None, None, None)        0         
=================================================================
Total params: 448
Trainable params: 448
Non-trainable params: 0
_________________________________________________________________
None

我们可以在这里看到model.summary()无法获得图层的输出形状。

因此,我们在取消注释平整且密集的层时会得到此错误:

WARNING:tensorflow:Entity <bound method fakeLayer.call of <__main__.fakeLayer object at 0x0000021A737535C0>> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: 
WARNING: Entity <bound method fakeLayer.call of <__main__.fakeLayer object at 0x0000021A737535C0>> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: 
Traceback (most recent call last):

  File "C:\Users\integration\Documents\Scripts\minimalBug.py", line 31, in <module>
    x = keras.layers.Dense(1)(x)

  File "C:\Users\integration\.conda\envs\py36\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 817, in __call__
    self._maybe_build(inputs)

  File "C:\Users\integration\.conda\envs\py36\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 2141, in _maybe_build
    self.build(input_shapes)

  File "C:\Users\integration\.conda\envs\py36\lib\site-packages\tensorflow_core\python\keras\layers\core.py", line 1015, in build
    raise ValueError('The last dimension of the inputs to `Dense` '

ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`.

2 个答案:

答案 0 :(得分:1)

我不知道它是否有作用,但是您在这里有双括号,不知道为什么:

def compute_output_shape(self, input_shape):
    #wrong: return ((input_shape[0], input_shape[1]* input_shape[2], input_shape[3]))
    return (input_shape[0], input_shape[1]* input_shape[2], input_shape[3])

现在,真正的问题出在call上,您在其中混合了许多不同的东西。

  • tf.shape返回一个张量,而不是元组
  • compute_output_shape返回一个元组
  • 如果您给compute_output_shape提供张量,它将返回一个张量元组(永远不要这样做!)
  • tf.reshape接受“元组”或“张量”,不接受张量元组
  • Keras形状是包含None值的元组,reshape接受具有-1值而不是None的元组。

解决方案:

def call(self, inputs):
    keras_shape = keras.backend.int_shape(inputs) #tuple containing None and numbers
    tf_shape_tuple = tuple(-1 if s is None else s for s in keras_shape) #None -> -1

    return tf.reshape(inputs , tf_shape_tuple)

答案 1 :(得分:0)

我会使用 tf.shape(x) 来解决 None 形状 (source) 的问题:

def call(self, inputs):
    in_shape = tf.shape(inputs)
    return tf.reshape(inputs , (in_shape[0], in_shape[1]*in_shape[2], in_shape[3]))

对于计算输出形状,我首先将输入形状重新定义为:

from tensorflow.python.framework import tensor_shape

def compute_output_shape(self, input_shape):
    input_shape = tensor_shape.TensorShape(input_shape).as_list()
    return (input_shape[0], input_shape[1]* input_shape[2], input_shape[3])