子类化Sequential()keras模型

时间:2019-11-27 14:35:57

标签: tensorflow keras

我想继承一个顺序模型,以便能够编写自定义call()并处理命名输入。但是,对我来说,__init__函数的很小改动已经带来了一些意外的行为。如果我尝试在子类中添加新成员并在调用super().__init__()之后对其进行初始化,则该模型将无法自动构建。

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, Activation, MaxPooling2D, Dense, Flatten
import tensorflow as tf
class Sequential2(Sequential):

    def __init__(self):
        super(Sequential2, self).__init__()
        self.custom_member = []

    def get_my_custom_member(self):
        return self.custom_member

model = Sequential2()

if tf.keras.backend.image_data_format() == 'channels_first':
    input_shape = (1, 28, 28)
else:
    assert tf.keras.backend.image_data_format() == 'channels_last'
    input_shape = (28, 28, 1)

layers = [Conv2D(32, (3, 3), input_shape=input_shape)]

for layer in layers:
    model.add(layer)

model.add(Dense(10))
model.add(Activation('relu'))

model.summary()

失败,输出:ValueError: This model has not yet been built. Build the model first by calling `build()` or calling `fit()` with some data, or specify an `input_shape` argument in the first layer(s) for automatic build.

但是,如果忽略了self.custom_member = [],则会按预期运行。

我在这里想念什么? (已使用Tensorflow 1.14测试)

1 个答案:

答案 0 :(得分:1)

此问题已在 TF 2.2 中修复。您可以参考如下所示的工作代码

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, Activation, MaxPooling2D, Dense, Flatten

import tensorflow as tf
print(tf.__version__)

class Sequential2(Sequential):

    def __init__(self):
        super(Sequential2, self).__init__()
        self.custom_member = []

    def get_my_custom_member(self):
        return self.custom_member

model = Sequential2()

if tf.keras.backend.image_data_format() == 'channels_first':
    input_shape = (1, 28, 28)
else:
    assert tf.keras.backend.image_data_format() == 'channels_last'
    input_shape = (28, 28, 1)

layers = [Conv2D(32, (3, 3), input_shape=input_shape)]

for layer in layers:
    model.add(layer)

model.add(Dense(10))
model.add(Activation('relu'))

model.summary()

输出:

2.2.0
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 26, 26, 32)        320       
_________________________________________________________________
dense (Dense)                (None, 26, 26, 10)        330       
_________________________________________________________________
activation (Activation)      (None, 26, 26, 10)        0         
=================================================================
Total params: 650
Trainable params: 650
Non-trainable params: 0
_________________________________________________________________