调用函数何时以及如何在Keras的模型子类中工作?

2024-10-01 17:40:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在《Scikit Learn、Keras和Tensorflow的机器学习实践》(Hand on Machine Learning with Scikit Learn、Keras和Tensorflow)中读到了关于使用子类API构建动态模型的内容,主要涉及编写一个子类,其中包含两种方法:构造函数和调用函数。构造函数相当容易理解。但是,我在理解调用函数在构建模型时的确切工作时间和工作方式时遇到了问题

我使用了书中的代码,并做了如下实验(使用sklearn的加利福尼亚住房数据集):

class WideAndDeepModel(keras.Model):
    def __init__(self, units=30, activation='relu', **kwargs):
        super().__init__(**kwargs)
        self.hidden1 = keras.layers.Dense(units, activation=activation)
        self.hidden2 = keras.layers.Dense(units, activation=activation)
        self.main_output = keras.layers.Dense(1)
        self.aux_output = keras.layers.Dense(1)
    def call(self, inputs):
        print('call function running')
        input_A, input_B = inputs
        hidden1 = self.hidden1(input_B)
        hidden2 = self.hidden2(hidden1)
        concat = keras.layers.concatenate([input_A, hidden2])
        main_output = self.main_output(concat)
        aux_output = self.aux_output(hidden2)
        return main_output, aux_output

model = WideAndDeepModel()
model.compile(loss=['mse','mse'], loss_weights=[0.9,0.1], optimizer='sgd')
history = model.fit([X_train_A, X_train_B],[y_train, y_train], epochs=20, validation_data=([X_val_A, X_val_B], [y_val, y_val]))

以下是培训期间的输出:

Epoch 1/20
***call function running***
***call function running***
353/363 [============================>.] - ETA: 0s - loss: 1.6398 - output_1_loss: 1.5468 - output_2_loss: 2.4769
***call function running***
363/363 [==============================] - 1s 1ms/step - loss: 1.6224 - output_1_loss: 1.5296 - output_2_loss: 2.4571 - val_loss: 4.3588 - val_output_1_loss: 4.7174 - val_output_2_loss: 1.1308
Epoch 2/20
363/363 [==============================] - 0s 1ms/step - loss: 0.6073 - output_1_loss: 0.5492 - output_2_loss: 1.1297 - val_loss: 75.1126 - val_output_1_loss: 81.6632 - val_output_2_loss: 16.1572
...

调用函数在第一个历元的训练开始时运行两次,然后几乎在第一个历元结束时运行。在那之后就再也不用跑了

在我看来,虽然层是在构造函数的早期实例化的,但层之间的连接(在调用函数中定义)是在相当晚的时候建立的(在培训开始时)。在我看来,层之间没有所谓的连接的逻辑实体,连接只是一个按特定顺序将一个层的输出传递给另一个层的过程。我的理解正确吗

第二个问题是,为什么在培训的早期阶段调用函数会运行三次,而不是一次


Tags: selfoutputmainlayersfunctionvalcallactivation
2条回答

首先要注意,使用Python打印语句不是一个好主意,因为使用TysFooFor使用C++来更快地并行地运行训练,并且省略了打印语句,所以使用^ {CD1>}和^ {CD2>}不是调试模型的好主意。 但让我回到你的问题上来。值得一提的是,TensorFlow和Keras模型具有惰性行为,这意味着当您实例化模型model = WideAndDeepModel()时,尚未创建模型权重,无论您是第一次调用model.call()还是model.build()方法,都将创建模型权重。因此,在Python中,您的模型曾经被用来创建模型权重,一次用于启动训练过程并构建C++对象(图),一次用于启动验证。之后,所有计算都将在C++中执行,并且您没有看到任何打印语句。

注意:如果要以图形模式打印某些内容,可以使用tf.print

the layers are instantiated early in the constructor function

正确的

the connection between the layers are established quite late

同样正确的是,当您第一次调用model.build()或调用模型时,会初始化权重,正如您在guide子类Keras layers中看到的:

class Linear(keras.layers.Layer):
    def __init__(self, units=32):
        super(Linear, self).__init__()
        self.units = units

    def build(self, input_shape):
        self.w = self.add_weight(
            shape=(input_shape[-1], self.units),
            initializer="random_normal",
            trainable=True,
        )
        self.b = self.add_weight(
            shape=(self.units,), initializer="random_normal", trainable=True
        )

    def call(self, inputs):
        return tf.matmul(inputs, self.w) + self.b

why the call function gets run three times at the early stage

第一次可能是第一次调用模型,并实例化权重。然后另一次构建Tensorflow图,这是运行Tensorflow模型的非Python代码。该模型被调用一次以创建此图,并且进一步的调用在Python之外,因此打印函数不再是它的一部分。您可以使用model.compile(..., run_eagerly=True)更改此行为。最后,第三次将是第一次传递验证数据

相关问题 更多 >

    热门问题