Reputation: 71
I am using Keras with Tensorflow to implement my model (M). Lets suppose that I have the following input features F = {x,y, a1, a2, a3, ..., an} I want to build a deep model (M1) using only x and y. Then, the output of (M1) with all the remaining features (a1, a2, ..., an) will be the input of another model (M2).
x,y --> M1 --> z, a1, a2, ..., an --> M2 --> final output
How can I build such model in Keras?
Upvotes: 0
Views: 192
Reputation: 2378
Use Keras functional api.
It's not entirely clear to me whether you mean to have a second model that is only trained on the output of first model, or something that could make both models trained jointly.
If you mean for M1 and M2 to be trained separately, then assuming x, y, a
are your input ndarray
s you can do something like that:
input_x = Input(shape=...)
input_y = Input(shape=...)
...
M1 = ... # your first model
m1_output = M1.output # assuming M1 outputs only one tensor
m2_input = Input(batch_shape=m1_output.shape) # the part that you can feed outputs from M1
m2_output = ...
M2 = Model(inputs=[m2_input,a], outputs=[m2_output])
You could also train both parts simultaneously, then it's also covered in Functional API's documentation. You'd need to define M2 like this:
M2 = Model(inputs=M1.inputs + [a], outputs=M1.outputs + [m2_output])
Of course you'd have to work out the losses accordingly.
Upvotes: 1