keras模型输出形状中的方括号

时间:2020-08-05 19:28:55

标签: keras

我最近在查看模型摘要时遇到了这个问题。

enter image description here

我想知道[(None, 16)](None, 16)有什么区别?为什么输入层具有这种输入形状?

来源:model.summary() can't print output shape while using subclass model

1 个答案:

答案 0 :(得分:0)

问题在于您如何定义input_shape。 python中的单个元素元组实际上是标量值,如下所示-

input_shape0 = 32
input_shape1 = (32)
input_shape2 = (32,)
print(input_shape0, input_shape1, input_shape2)
32 32 (32,)

由于Keras函数API Input需要一个输入形状作为元组,因此您必须以(n,)而不是n的形式传递它。

奇怪的是,您得到一个方括号,因为当我运行完全相同的代码时,会出现错误。

TypeError                                 Traceback (most recent call last)
<ipython-input-828-b564be68c80d> in <module>
     33 
     34 if __name__ == '__main__':
---> 35     mlp = MLP((16))
     36     mlp.summary()

<ipython-input-828-b564be68c80d> in __init__(self, input_shape, **kwargs)
      6         super(MLP, self).__init__(**kwargs)
      7         # Add input layer
----> 8         self.input_layer = klayers.Input(input_shape)
      9 
     10         self.dense_1 = klayers.Dense(64, activation='relu')

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_layer.py in Input(shape, batch_size, name, dtype, sparse, tensor, **kwargs)
    229       dtype=dtype,
    230       sparse=sparse,
--> 231       input_tensor=tensor)
    232   # Return tensor including `_keras_history`.
    233   # Note that in this case train_output and test_output are the same pointer.

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_layer.py in __init__(self, input_shape, batch_size, dtype, input_tensor, sparse, name, **kwargs)
     89     if input_tensor is None:
     90       if input_shape is not None:
---> 91         batch_input_shape = (batch_size,) + tuple(input_shape)
     92       else:
     93         batch_input_shape = None

TypeError: 'int' object is not iterable

因此,正确的方法(应该同时修正您的模型摘要如下-

from tensorflow import keras
from tensorflow.keras import layers as klayers

class MLP(keras.Model):
    def __init__(self, input_shape=(32,), **kwargs):
        super(MLP, self).__init__(**kwargs)
        # Add input layer
        self.input_layer = klayers.Input(input_shape)

        self.dense_1 = klayers.Dense(64, activation='relu')
        self.dense_2 = klayers.Dense(10)

        # Get output layer with `call` method
        self.out = self.call(self.input_layer)

        # Reinitial
        super(MLP, self).__init__(
            inputs=self.input_layer,
            outputs=self.out,
            **kwargs)

    def build(self):
        # Initialize the graph
        self._is_graph_network = True
        self._init_graph_network(
            inputs=self.input_layer,
            outputs=self.out
        )

    def call(self, inputs):
        x = self.dense_1(inputs)
        return self.dense_2(x)

if __name__ == '__main__':
    mlp = MLP((16,))
    mlp.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_19 (InputLayer)        (None, 16)                0         
_________________________________________________________________
dense_8 (Dense)              (None, 64)                1088      
_________________________________________________________________
dense_9 (Dense)              (None, 10)                650       
=================================================================
Total params: 1,738
Trainable params: 1,738
Non-trainable params: 0
_________________________________________________________________