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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With