4

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 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

3 Comments

I want to have two inputs in the new model (the initial input of the base model:input_1 + the new defined input_2.
use input_1=baseModel.input
Great! Thank you.
2

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.