[英]Train a model with 2 stacked models in it keras
I have the folowing models that i want to train (See image below):我有以下要训练的模型(见下图):
The model has an input of 20. The model A has an input of 10 (the first 10 elements of the initial input), the model B has an input of 10 (the last 10 elements of the initial input) finally the input of the model C is the concatenation of the output of the models A and B. The model has an input of 20. The model A has an input of 10 (the first 10 elements of the initial input), the model B has an input of 10 (the last 10 elements of the initial input) finally the input of the model C 是模型 A 和模型 B 的 output 的串联。
How can I train this 3 models at the same time in Keras?如何在 Keras 中同时训练这 3 个模型? Can I merge it in one big model?我可以将它合并到一个大 model 中吗? (I only have data to train the big model) (我只有训练大模型的数据)
Can I merge it in one big model?我可以将它合并到一个大 model 中吗?
Yes!是的!
How can I train this 3 models at the same time in Keras?如何在 Keras 中同时训练这 3 个模型?
I will give you pointers:我给你指点:
Lets assume that you have your three models defined, and named model_A, model_B and model_C.让我们假设您定义了三个模型,并命名为 model_A、model_B 和 model_C。 You can now define you complete model somewhat like this (I did not check the exact code):您现在可以定义完整的 model 有点像这样(我没有检查确切的代码):
def complete_model(model_A, model_B, model_C):
input_1 = layers.Input(shape=(10,))
input_2 = layers.Input(shape=(10,))
model_A_output = model_A(input_1)
model_B_output = model_B(input_2)
concatenated = tf.concat([model_A_output, model_B_output], axis=-1)
model_C_output = model_C(concatenated)
model = Model(inputs=[input_1, input_2], outputs=model_C_output)
model.compile(loss=losses.MSE)
model.summary()
return model
This requires you to give two-dimensional inputs, so you have to do some numpy slicing to preprocess your inputs.这需要您提供二维输入,因此您必须进行一些 numpy 切片来预处理您的输入。
If you still want your one-dimensional inputs, you can just define a single input layer with shape (20,) and then use the tf.split function to split it in half and feed it into the next networks.如果您仍然想要一维输入,您可以定义一个形状为 (20,) 的输入层,然后使用 tf.split function 将其分成两半并将其输入下一个网络。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.