使用子类模型时,model.summary()无法打印输出形状

Gar*_*ary 8 python keras tensorflow tf.keras

这是创建keras模型的两种方法,但是output shapes两种方法的汇总结果不同。显然,前者可以打印更多信息,并且可以更轻松地检查网络的正确性。

import tensorflow as tf
from tensorflow.keras import Input, layers, Model

class subclass(Model):
    def __init__(self):
        super(subclass, self).__init__()
        self.conv = layers.Conv2D(28, 3, strides=1)

    def call(self, x):
        return self.conv(x)


def func_api():
    x = Input(shape=(24, 24, 3))
    y = layers.Conv2D(28, 3, strides=1)(x)
    return Model(inputs=[x], outputs=[y])

if __name__ == '__main__':
    func = func_api()
    func.summary()

    sub = subclass()
    sub.build(input_shape=(None, 24, 24, 3))
    sub.summary()
Run Code Online (Sandbox Code Playgroud)

输出?

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 24, 24, 3)         0         
_________________________________________________________________
conv2d (Conv2D)              (None, 22, 22, 28)        784       
=================================================================
Total params: 784
Trainable params: 784
Non-trainable params: 0
_________________________________________________________________
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            multiple                  784       
=================================================================
Total params: 784
Trainable params: 784
Non-trainable params: 0
_________________________________________________________________
Run Code Online (Sandbox Code Playgroud)

因此,我应该如何使用子类方法来获取output shapesummary()?

jhi*_*han 9

我解决问题的方式与Elazar提到的非常相似。重写类中的函数summary() subclass。然后你可以在使用模型子类化时直接调用summary():

class subclass(Model):
    def __init__(self):
        ...
    def call(self, x):
        ...

    def summary(self):
        x = Input(shape=(24, 24, 3))
        model = Model(inputs=[x], outputs=self.call(x))
        return model.summary()

if __name__ == '__main__':
    sub = subclass()
    sub.summary()
Run Code Online (Sandbox Code Playgroud)


Gar*_*ary 6

我已经使用这种方法解决了这个问题,我不知道是否有更简单的方法。

class subclass(Model):
    def __init__(self):
        ...
    def call(self, x):
        ...

    def model(self):
        x = Input(shape=(24, 24, 3))
        return Model(inputs=[x], outputs=self.call(x))



if __name__ == '__main__':
    sub = subclass()
    sub.model().summary()
Run Code Online (Sandbox Code Playgroud)

  • @Samuel通过评估“outputs=self.call(x)”,将调用“subclass.call(self, x)”方法。这会触发封装实例中的形状计算。此外,返回的“Model”实例还计算其自己的形状,并在“.summary()”中报告。这种方法的主要问题是输入形状是恒定的 `shape=(24, 24, 3)`,所以如果您需要动态解决方案,这将不起作用。 (4认同)
  • 你能解释一下为什么这有效吗?特别是“outputs=self.call(x)”部分。 (2认同)

han*_*728 5

我想关键点是_init_graph_network类中的方法Network,它是Model. _init_graph_network如果在调用方法时指定inputsoutputs参数,将被调用__init__

所以会有两种可能的方法:

  1. 手动调用_init_graph_network方法构建模型图。
  2. 用输入层和输出重新初始化。

并且这两种方法都需要输入层和输出(从 需要self.call)。

现在调用summary将给出确切的输出形状。但是它会显示Input层,它不是子类模型的一部分。

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()
Run Code Online (Sandbox Code Playgroud)

输出将是:

Model: "mlp_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 16)]              0         
_________________________________________________________________
dense (Dense)                (None, 64)                1088      
_________________________________________________________________
dense_1 (Dense)              (None, 10)                650       
=================================================================
Total params: 1,738
Trainable params: 1,738
Non-trainable params: 0
_________________________________________________________________
Run Code Online (Sandbox Code Playgroud)


小智 5

我分析了Adi Shumely的答案:

  • 不需要添加 Input_shape,因为您在 build() 中将其设置为参数
  • 添加输入层对模型没有任何作用,它作为参数传递给 call() 方法
  • 添加所谓的输出并不是我所看到的方式。它所做的唯一也是最重要的事情是调用 call() 方法。

因此,我提出这个解决方案,不需要对模型进行任何修改,只需要通过添加对 call() 方法的调用来改进模型,因为它是在调用 summaries() 方法之前构建的具有输入张量的模型。我尝试了自己的模型以及此提要中提供的三个模型,到目前为止它有效。

来自此提要的第一篇文章:

import tensorflow as tf
from tensorflow.keras import Input, layers, Model

class subclass(Model):
    def __init__(self):
        super(subclass, self).__init__()
        self.conv = layers.Conv2D(28, 3, strides=1)

    def call(self, x):
        return self.conv(x)

if __name__ == '__main__':
    sub = subclass()
    sub.build(input_shape=(None, 24, 24, 3))

    # Adding this call to the call() method solves it all
    sub.call(Input(shape=(24, 24, 3)))

    # And the summary() outputs all the information
    sub.summary()
Run Code Online (Sandbox Code Playgroud)

来自提要的第二个帖子

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

class MLP(keras.Model):
    def __init__(self, **kwargs):
        super(MLP, self).__init__(**kwargs)
        self.dense_1 = klayers.Dense(64, activation='relu')
        self.dense_2 = klayers.Dense(10)

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

if __name__ == '__main__':
    mlp = MLP()
    mlp.build(input_shape=(None, 16))
    mlp.call(klayers.Input(shape=(16)))
    mlp.summary()
Run Code Online (Sandbox Code Playgroud)

从提要的最后一篇文章开始

import tensorflow as tf
class MyModel(tf.keras.Model):
    def __init__(self, **kwargs):
        super(MyModel, self).__init__(**kwargs) 
        self.dense10 = tf.keras.layers.Dense(10, activation=tf.keras.activations.softmax)    
        self.dense20 = tf.keras.layers.Dense(20, activation=tf.keras.activations.softmax)
    
    def call(self, inputs):
        x =  self.dense10(inputs)
        y_pred =  self.dense20(x)
        return y_pred

model = MyModel()
model.build(input_shape = (None, 32, 32, 1))
model.call(tf.keras.layers.Input(shape = (32, 32, 1)))
model.summary()
Run Code Online (Sandbox Code Playgroud)