Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
116 views
in Technique[技术] by (71.8m points)

python - TF/Keras: how to stack model

I wonder how to stack model in Keras? I want to give Input to the 2 models, take the output to feed it in the Meta model, like this:

enter image description here

If I declare like this:

inputs = tf.keras.Input(...)
x = Dense()(inputs)
y = RNN()(inputs)

meta = Add()([x, y])
meta = Dense()(meta)
model = tf.keras.Model(inputs=inputs, outputs=meta)

That piece of code is still ONE model, just inputs taking parallel paths.

P/S: I already read this answer, but I think it is just inputs taking parallel paths: How to vertically stack trained models in keras?

question from:https://stackoverflow.com/questions/65930567/tf-keras-how-to-stack-model

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Keras Models are Layers as well. So just create your models separately, and combine them in your meta model.

# model1
a = Input(shape=(...))
b = Dense()(a)
model1 = Model(inputs=a, outputs=b)

# model2
a = Input(shape=(...))
b = RNN(...)(a)
model2 = Model(inputs=a, outputs=b)

# combine them in meta model
inputs = tf.keras.Input(...)
x = model1(inputs)
y = model2(inputs)

meta = Add()([x, y])
meta = Dense()(meta)
model = tf.keras.Model(inputs=inputs, outputs=meta)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...