Keras: Saving Model with Unused Inputs Removes Them

Created on 14 Dec 2017  路  1Comment  路  Source: keras-team/keras

The issue is that saving a model (hdf5) with unused inputs and reloading them causes those inputs to disappear. The behavior is problematic because building a standard API for inference for going from inputs _x, y_ to an output _z_ often requires comparing models that use only _x_ or only _y_ to ones using both. Since this auto-pruning takes place the interface on the inferences side needs to inspect the model to see what inputs it has rather than having a standard format.

Example

import keras
import numpy as np
from keras.models import Model
from keras.layers import Input
in_1 = Input(shape = (None, 1), name = 'input_1')
in_2 = Input(shape = (None, 2), name = 'input_2')
simple_model = Model(inputs = [in_1, in_2], outputs = [in_1])
simple_model.predict([np.ones((1, 3, 1)), np.zeros((1, 3, 2))])
array([[[ 1.],
        [ 1.],
        [ 1.]]], dtype=float32)

However, saving and reloading (in the same instance) causes problems

from keras.models import load_model
simple_model.save('simple_model.h5')
loaded_simple_model = load_model('simple_model.h5')
loaded_simple_model.predict([np.ones((1, 3, 1)), np.zeros((1, 3, 2))])
ValueError: Error when checking model : the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 2 arrays: [array([[[ 1.],
        [ 1.],
        [ 1.]]]), array([[[ 0.,  0.],
        [ 0.,  0.],
        [ 0.,  0.]]])]...

Inspecting the models closer shows that the saved loses an input

simple_model.inputs
[<tf.Tensor 'input_1_1:0' shape=(?, ?, 1) dtype=float32>,
 <tf.Tensor 'input_2_1:0' shape=(?, ?, 2) dtype=float32>]
loaded_simple_model.inputs
[<tf.Tensor 'input_1_3:0' shape=(?, ?, 1) dtype=float32>]

Thank you!

  • [x] Check that you are up-to-date with the master branch of Keras. You can update with:
    pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps

  • [x] If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.

  • [x] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

Most helpful comment

A temporary fix is below, but not particularly elegant

from keras.models import Model
from keras.layers import Input, Lambda
in_1 = Input(shape = (None, 1), name = 'input_1')
in_2 = Input(shape = (None, 2), name = 'input_2')
l_output = Lambda(lambda x: x[0])([in_1, in_2])
simple_model = Model(inputs = [in_1, in_2], outputs = [l_output])

>All comments

A temporary fix is below, but not particularly elegant

from keras.models import Model
from keras.layers import Input, Lambda
in_1 = Input(shape = (None, 1), name = 'input_1')
in_2 = Input(shape = (None, 2), name = 'input_2')
l_output = Lambda(lambda x: x[0])([in_1, in_2])
simple_model = Model(inputs = [in_1, in_2], outputs = [l_output])
Was this page helpful?
0 / 5 - 0 ratings

Related issues

anjishnu picture anjishnu  路  3Comments

braingineer picture braingineer  路  3Comments

nryant picture nryant  路  3Comments

kylemcdonald picture kylemcdonald  路  3Comments

amityaffliction picture amityaffliction  路  3Comments