“Layer is not connected” issue while accessing intermediate layer from within the custom callback if model is built by sub-classing

怎甘沉沦 提交于 2020-07-07 14:26:26

问题


I've a simple model and need access of intermediate layers within a custom callback to get intermediate predictions.

import tensorflow as tf
import numpy as np

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

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[1].output
        )
        print("\nLayer output: ", get_output(X))

If I build the model by sub-classing like below:

class Model(tf.keras.Model):
    def build(self, input_shape):
        self.dense1 = tf.keras.layers.Dense(units=32)
        self.dense2 = tf.keras.layers.Dense(units=1)
        
    def call(self, input_tensor):
        x = self.dense1(input_tensor)
        x = self.dense2(x)
        return x

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

I get following error:

<ipython-input-8-bab75191182e> in on_epoch_end(self, epoch, logs)
      2     def on_epoch_end(self, epoch, logs=None):
      3         get_output = tf.keras.backend.function(
----> 4             inputs = self.model.layers[0].input,
      5             outputs = self.model.layers[1].output
      6         )
.
.
AttributeError: Layer dense is not connected, no input to return.

whereas if I build model using functional API like below, it works as expected.

initial = tf.keras.layers.Input((16,))
x = tf.keras.layers.Dense(units=32)(initial)
final = tf.keras.layers.Dense(units=1)(x)

model = tf.keras.Model(initial, final)
model.compile(optimizer='adam',loss='mean_squared_error', metrics='accuracy')
model.fit(X,y, epochs=2, callbacks=[CustomCallback()])

What's causing the error in case of model sub-classing?

TF version: 2.2.0

来源:https://stackoverflow.com/questions/62668398/layer-is-not-connected-issue-while-accessing-intermediate-layer-from-within-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!