Keras: How can I predict from a subset of the layers of a trained model?

Created on 23 Dec 2016  路  2Comments  路  Source: keras-team/keras

Let's say I have a model:

model = Sequential()
model.add(Dense(input_dim=8, output_dim=10,name='first'))
model.add(Dense(output_dim=10,name='second'))
model.add(Dense(output_dim=10,name='third'))
model.add(Dense(output_dim=10,name='fourth'))
model.add(Dense(output_dim=10,name='fifth'))
model.compile(loss='mse',optimizer='rmsprop')

This model is then trained, and has weights. I now want to predict only from layers 'second' to 'fourth'. How can I do that?

If I do

minput = model.get_layer('second')
moutput = model.get_layer('fourth')
testmodel = Model(input=minput, output=moutput)

But I get an error

TypeError: Input tensors to a Model must be Keras tensors. Found: None (missing Keras metadata).

Thanks!

stale

Most helpful comment

One way is to reconstruct that portion of the graph.

minput = model.get_layer('second')
mhidden = model.get_layer('third')
moutput = model.get_layer('fourth')
testmodel = Model(input=minput.input, output=moutput(mhidden(minput(minput.input))))

A cleaner way would be to construct that portion of the graph separately.

middle = Sequential(name="middle")
middle.add(Dense(input_dim=10, output_dim=10, name='third'))
middle.add(Dense(output_dim=10,name='fourth'))
middle.add(Dense(output_dim=10,name='fifth'))

Then use that portion in the full model:

model = Sequential()
model.add(Dense(input_dim=8, output_dim=10,name='first'))
model.add(Dense(output_dim=10,name='second'))
model.add(middle)
model.compile(loss='mse',optimizer='rmsprop')

Cheers,
Ben

All 2 comments

One way is to reconstruct that portion of the graph.

minput = model.get_layer('second')
mhidden = model.get_layer('third')
moutput = model.get_layer('fourth')
testmodel = Model(input=minput.input, output=moutput(mhidden(minput(minput.input))))

A cleaner way would be to construct that portion of the graph separately.

middle = Sequential(name="middle")
middle.add(Dense(input_dim=10, output_dim=10, name='third'))
middle.add(Dense(output_dim=10,name='fourth'))
middle.add(Dense(output_dim=10,name='fifth'))

Then use that portion in the full model:

model = Sequential()
model.add(Dense(input_dim=8, output_dim=10,name='first'))
model.add(Dense(output_dim=10,name='second'))
model.add(middle)
model.compile(loss='mse',optimizer='rmsprop')

Cheers,
Ben

have tried the first method:

minput = model.get_layer('second')
mhidden = model.get_layer('third')
moutput = model.get_layer('fourth')
testmodel = Model(input=minput.input, output=moutput(mhidden(minput(minput.input))))

But still get the error:

TypeError: Input layers to a Model must be InputLayer objects.

And for the another two methods provided by you, I think you haven't notice Ben said: "This model is then trained, and has weights"

Was this page helpful?
0 / 5 - 0 ratings