4
votes

I trained a base functional-API Keras model and now I want to re-use its output as input into a new model, while re-using its weights as well. On the new model I want to add one more input and multiply it with the output of the base model. So in the new model I want to have two inputs (including the one of the base model + the new added one) and a new output consisting of element-wise multiplication of the base model output with the new input.

The base model looks as this:

Layer (type) Output Shape Param #
input_1 (InputLayer) (None, 30, 1) 0
_________________________________________________________________ lstm_1 (LSTM) (None, 64) 16896
_________________________________________________________________ dropout_1 (Dropout) (None, 64) 0
_________________________________________________________________ dense_1 (Dense) (None, 96) 6240
_________________________________________________________________ dropout_2 (Dropout) (None, 96) 0
_________________________________________________________________ dense_2 (Dense) (None, 30) 2910

And the code I tried (but not working) is:

newModel = baseModel

base_output = baseModel.get_layer('dense_2').output
input_2 = Input(shape=(n_steps_in, n_features))

multiply = Multiply()([base_output,input_2])

new_output = Dense(30)(multiply)

newModel = Model(inputs=[input_1,input_2], outputs=new_output)

newModel.summary()

I receive the error: "TypeError: Input layers to a Model must be InputLayer objects. Received inputs: [, ]. Input 0 (0-based) originates from layer type Dense.". Any advice on what I am missing? Thanks in advance.

2

2 Answers

5
votes

IN the line

newModel = Model(inputs=[input_1,input_2], outputs=new_output)

you have "input_1" where have you defined it. The error is because this varaible is undefined

As per you case you should use

input_1=baseModel.input
2
votes

You are missing the input from your baseModel. Try:

base_input = baseModel.input
base_output = baseModel.get_layer('dense_2').output
input_2 = Input(shape=(n_steps_in, n_features))

multiply = Multiply()([base_output,input_2])

new_output = Dense(30)(multiply)

newModel = Model(inputs=[base_input, input_2], outputs=new_output)

newModel.summary()