问题
I have a Keras model where I'd like to add a constant to the predictions. After some googling, I've ended up with the following code, which does exactly what I want:
import numpy as np
from keras.layers import Input, Add
from keras.backend import variable
from keras.models import Model, load_model
inputs = Input(shape=(1,))
add_in = Input(tensor=variable([[5]]), name='add')
output = Add()([inputs, add_in])
model = Model([inputs, add_in], output)
model.compile(loss='mse', optimizer='adam')
X = np.array([1,2,3,4,5,6,7,8,9,10])
model.predict(X)
However, if I save and load this model, Keras seems to lose track of the constant:
p = 'k_model.hdf5'
model.save(p)
del model
model2 = load_model(p)
model2.predict(X)
Which returns:
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 2 array(s), but instead got the following list of 1 arrays:
How do I include the constants when saving/loading the Keras model?
回答1:
Since as you mentioned it is always a constant, it would not make sense to define a separate Input layer for it; specially considering that it is not an input of your model. I suggest you to use a Lambda layer instead:
import numpy as np
from keras.layers import Input, Lambda
from keras.models import Model, load_model
def add_five(a):
return a + 5
inputs = Input(shape=(1,))
output = Lambda(add_five)(inputs)
model = Model(inputs, output)
model.compile(loss='mse', optimizer='adam')
X = np.array([1,2,3,4,5,6,7,8,9,10])
model.predict(X)
Output:
array([[ 6.],
[ 7.],
[ 8.],
[ 9.],
[10.],
[11.],
[12.],
[13.],
[14.],
[15.]], dtype=float32)
And there would be no problem when you save and reload the model since the add_five
function has been stored in the model file.
Update: you can extend this to the case where each input sample consists of more than one element. For example, if the input shape is (2,)
and you want to add 5 to the first element and 10 to the second element of each sample, you can easily modify the add_five
function and redefine it like this:
def add_constants(a):
return a + [5, 10]
# ... the same as above (just change the function name and input shape)
X = np.array([1,2,3,4,5,6,7,8,9,10]).reshape(5, 2)
model.predict(X)
Output:
# X
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10]])
# predictions
array([[ 6., 12.],
[ 8., 14.],
[10., 16.],
[12., 18.],
[14., 20.]], dtype=float32)
来源:https://stackoverflow.com/questions/52413371/save-load-a-keras-model-with-constants