<p dir="auto">I want to have 2 parallel VGG models that share an input. I can do this with:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="modelA = vgg16.VGG16(include_top=True,weights='imagenet')
modelB = vgg16.VGG16(include_top=True,weights='imagenet')
y = modelB(modelA.input)
modelC = Model(inputs=modelA.input,outputs=[modelA.output,y])"><pre class="notranslate"><code class="notranslate">modelA = vgg16.VGG16(include_top=True,weights='imagenet')
modelB = vgg16.VGG16(include_top=True,weights='imagenet')
y = modelB(modelA.input)
modelC = Model(inputs=modelA.input,outputs=[modelA.output,y])
</code></pre></div>
<p dir="auto">The top of the plotted model looks like:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/7025717/27660801-7f89bb10-5c1d-11e7-92ec-964f3c73907d.png"><img src="https://user-images.githubusercontent.com/7025717/27660801-7f89bb10-5c1d-11e7-92ec-964f3c73907d.png" alt="mergedvgg" style="max-width: 100%;"></a><br>
The layers in the left-hand side behave as normal and can be accessed with modelC.layers[1] through modelC.layers[22].</p>
<p dir="auto">The right-hand side treats the entire 2nd VGG model as being compressed into one layer, accessed in modelC.layers[23]. This seems okay because this layer 23 has its own array of layers, which seem accessible as normal, e.g. modelC.layers[23].layers[2].name will give 'block1_conv2'.</p>
<p dir="auto">... eventually, I am using the mean output of a layer to compute a gradient. This works fine with the left VGG model:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="loss = K.mean(modelC.layers[2].output[:,:,:,:])"><pre class="notranslate"><code class="notranslate">loss = K.mean(modelC.layers[2].output[:,:,:,:])
</code></pre></div>
<p dir="auto">but if I try to access a layer in the right VGG model:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="loss = K.mean(modelC.layers[23].layers[2].output[:,:,:,:])"><pre class="notranslate"><code class="notranslate">loss = K.mean(modelC.layers[23].layers[2].output[:,:,:,:])
</code></pre></div>
<p dir="auto">I get a 'Disconnected Input' error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="File &quot;C:\Users\Sam\Anaconda2\lib\site-packages\theano\gradient.py&quot;, line 526, in handle_disconnected
    raise DisconnectedInputError(message)
DisconnectedInputError:  "><pre class="notranslate"><code class="notranslate">File "C:\Users\Sam\Anaconda2\lib\site-packages\theano\gradient.py", line 526, in handle_disconnected
    raise DisconnectedInputError(message)
DisconnectedInputError:  
</code></pre></div>
<p dir="auto">I don't know if this is a bug because it seems like it should work. An alternative solution would be to be able to incorporate the 2nd VGG model without it being compressed, but I cannot find a way to do that with the functional API.</p>
<p dir="auto">I run into the above warning when saving sub-classed models with <code class="notranslate">model.save()</code>.</p>
<p dir="auto">After some investigation, the error seems to occur when using sub-classed layers in combination with sub-classed models.</p>
<p dir="auto">See the code below for a minimal example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import tensorflow as tf

class CustomLayer(tf.keras.layers.Layer):
    def __init__(self, units=2):
        super(CustomLayer, self).__init__()

        self.units = units

        self.layer = tf.keras.layers.Dense(units)

    def get_config(self):
        config = super(CustomLayer, self).get_config()
        config['units'] = self.units

        return config

    def call(self, x, **kwargs):
        x = self.layer(x)
        return x


class CustomModel(tf.keras.Model):
    def __init__(self, hidden_units):
        super(CustomModel, self).__init__()
        self.hidden_units = hidden_units
        self.dense_layers = [CustomLayer(u) for u in hidden_units]

    def call(self, inputs):
        x = inputs
        for layer in self.dense_layers:
            x = layer(x)
        return x

    def get_config(self):
        return {&quot;hidden_units&quot;: self.hidden_units}

    @classmethod
    def from_config(cls, config):
        return cls(**config)


model = CustomModel([16, 16, 10])
print(model(tf.random.uniform((1, 5))).shape)
model.compile()
model.save(&quot;my_model&quot;, )
print('---')
loaded_model = tf.keras.models.load_model('my_model')"><pre class="notranslate"><code class="notranslate">import tensorflow as tf

class CustomLayer(tf.keras.layers.Layer):
    def __init__(self, units=2):
        super(CustomLayer, self).__init__()

        self.units = units

        self.layer = tf.keras.layers.Dense(units)

    def get_config(self):
        config = super(CustomLayer, self).get_config()
        config['units'] = self.units

        return config

    def call(self, x, **kwargs):
        x = self.layer(x)
        return x


class CustomModel(tf.keras.Model):
    def __init__(self, hidden_units):
        super(CustomModel, self).__init__()
        self.hidden_units = hidden_units
        self.dense_layers = [CustomLayer(u) for u in hidden_units]

    def call(self, inputs):
        x = inputs
        for layer in self.dense_layers:
            x = layer(x)
        return x

    def get_config(self):
        return {"hidden_units": self.hidden_units}

    @classmethod
    def from_config(cls, config):
        return cls(**config)


model = CustomModel([16, 16, 10])
print(model(tf.random.uniform((1, 5))).shape)
model.compile()
model.save("my_model", )
print('---')
loaded_model = tf.keras.models.load_model('my_model')
</code></pre></div>
<p dir="auto">Or check out this <a href="https://colab.research.google.com/gist/mhorlacher/e172e2dc8ec10fc790cf1c3fa1931e8a/untitled8.ipynb" rel="nofollow">gist</a>.</p>
<p dir="auto">My TF version is <code class="notranslate">2.8.0</code>.</p>