简体   繁体   English

如何用多个输入训练 Keras model?

[英]How to train Keras model with multiple inputs?

I want to train a Keras model with multiple inputs.我想训练一个具有多个输入的 Keras model。 I want to use the first input for training the model, but the second and the third inputs are variables which must be used during the training of the first input.我想使用第一个输入来训练 model,但第二个和第三个输入是在第一个输入的训练期间必须使用的变量。 I could not use the tf.Variable for using the second and the third inputs which are fixed during the training because it showed the error: You must feed a placeholder .我无法使用tf.Variable来使用在训练期间固定的第二个和第三个输入,因为它显示错误: You must feed a placeholder So, I want to feed them using tf.keras .所以,我想用tf.keras喂它们。 I changed the shape of the second and the third inputs to be similar to the first one.我将第二个和第三个输入的形状更改为与第一个相似。 Then, I concatenate them using:然后,我使用以下方法连接它们:

inputs = tf.keras.layers.concatenate([data, masks_rep, ind_rep], axis=2)

Then, for retrieving them in the model, I wrote:然后,为了在 model 中检索它们,我写道:

Inputs_t = tf.keras.Input(shape=(max_length, 3*charset_length))
x, m_k_expand, ind_k_expand = tf.split(Inputs_t, num_or_size_splits=3,  axis=1)

But it changes the dimensions of the layers from charset_length to 3*charset_length .但它将图层的尺寸从charset_length更改为3*charset_length How can I solve this problem?我怎么解决这个问题?

We can use the subclasses to use additional variables by using tf.Variable .我们可以通过tf.Variable使用子类来使用其他变量。 tf.Variable easily leads bugs when it is used with tf.keras.layers . tf.Variabletf.keras.layers使用时很容易导致错误。 But using subclasses prevents bugs.但是使用子类可以防止错误。 More examples are provided here: enter link description here此处提供了更多示例:在此处输入链接描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM