Keras: Conversion of a sequential model to a functional model with Keras 2.2.0

Created on 9 Jun 2018  路  1Comment  路  Source: keras-team/keras

Up to Keras version 2.1.6 one was able to "convert" a sequential model to a functional model by accessing the underlying model.model.
Since version 2.2.0 this is no longer possible.

Can it still be done in some other way?

(In case you wonder why I would like to do something like this, I'm maintaining a library that relies on this conversion. :wink:)

Most helpful comment

Solution:

from keras import layers, models

input_layer = layers.Input(batch_shape=seqmodel.layers[0].input_shape)
prev_layer = input_layer
for layer in seqmodel.layers:
    layer._inbound_nodes = []
    prev_layer = layer(prev_layer)

funcmodel = models.Model([input_layer], [prev_layer])

source: https://stackoverflow.com/a/50937113/1866775

>All comments

Solution:

from keras import layers, models

input_layer = layers.Input(batch_shape=seqmodel.layers[0].input_shape)
prev_layer = input_layer
for layer in seqmodel.layers:
    layer._inbound_nodes = []
    prev_layer = layer(prev_layer)

funcmodel = models.Model([input_layer], [prev_layer])

source: https://stackoverflow.com/a/50937113/1866775

Was this page helpful?
0 / 5 - 0 ratings