python - 尝试使用 tensorflow 自定义回调进行中间层预测时出现 "Layer is not connected, no input to return"错误

标签 python tensorflow keras

我正在尝试使用自定义回调在训练期间访问模型中间层的预测。以下实际代码的精简版本演示了该问题。

import tensorflow as tf
import numpy as np

class Model(tf.keras.Model):
    def __init__(self, input_shape=None, name="cus_model", **kwargs):
        super(Model, self).__init__(name=name, **kwargs)
        
    def build(self, input_shape):
        self.dense1 = tf.keras.layers.Dense(input_shape=input_shape, units=32)
        
    def call(self, input_tensor):
        return self.dense1(input_tensor)

class CustomCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        get_output = tf.keras.backend.function(
            inputs = self.model.layers[0].input,
            outputs = self.model.layers[0].output
        )
        print("Layer output: ",get_output.outputs)

X = np.ones((8,16))
y = np.sum(X, axis=1)

model = Model()
model.compile(optimizer='adam',loss='mean_squared_error', metrics='accuracy')
model.fit(X,y, epochs=8, callbacks=[CustomCallback()])
回调是按照此 answer 中的建议编写的。得到以下错误:
<ipython-input-3-635fd53dbffc> in on_epoch_end(self, epoch, logs)
     12     def on_epoch_end(self, epoch, logs=None):
     13         get_output = tf.keras.backend.function(
---> 14             inputs = self.model.layers[0].input,
     15             outputs = self.model.layers[0].output
     16         )
.
.
AttributeError: Layer dense is not connected, no input to return.
这是什么原因造成的?如何解决?

最佳答案

由于相同的错误,我也无法获得 self.layers[0].input,但也许您可以像这样直接调用 Model 中定义的函数:

class Model(tf.keras.Model):
    def __init__(self, input_shape=None, name="cus_model", **kwargs):
        super(Model, self).__init__(name=name, **kwargs)
        if not input_shape:
            input_shape = (10,)
        self.dense1 = tf.keras.layers.Dense(input_shape=input_shape, units=32)
        self.dev_dataset = np.ones((8,16))

    def call(self, input_tensor):
        return self.dense1(input_tensor)


class CustomCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        self.model.call(self.model.dev_dataset)


X = np.ones((8,16))
y = np.sum(X, axis=1)

model = Model()
model.compile(optimizer='adam',loss='mean_squared_error', metrics='accuracy')
model.fit(X,y, epochs=1, callbacks=[CustomCallback()])

关于python - 尝试使用 tensorflow 自定义回调进行中间层预测时出现 "Layer is not connected, no input to return"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62654908/

相关文章:

tensorflow - 在 Tensorflow 中恢复检查点时如何获取 global_step?

python - 为什么这个 Keras Conv2D 层与输入不兼容?

python - 使用(常量)参数保存/加载 Keras 模型

python-3.x - 使用 Lambda 层作为输出时如何获得关于参数的梯度

python-3.x - 如何在keras预处理器中传递数组而不是路径

python - 可调用问题,检测到的不仅仅是 __call__

python - Heroku 未被识别为内部或外部命令 (Windows)

machine-learning - NN 输入层中的单元数量可以与数据中的特征数量不同吗?

python - Django 自动转义在 render_to_string(template) 中不起作用?

python - Selenium 可以很好地检测弹出对话框,但无法处理它