最近在看tensorflow.keras的多输入多输出时,碰到了这个问题,在这里记录一下。
首先给出报错代码:
# 两个输入
main_input = tf.keras.layers.Input(shape=(784,),dtype=tf.float32,name="main_input")
auxiliary_input = tf.keras.layers.Input(shape=784,),dtype=tf.float32,name="auxiliary_input")
## 网络1
x = tf.keras.layers.Dense(128,activation="relu")(main_input)
dense_out = tf.keras.layers.Dense(10,activation="softmax")(x)
output1 = tf.keras.layers.Dense(10,activation="softmax")(dense_out) ## 输出1
## 网络2
x = tf.keras.backend.concatenate([dense_out, auxiliary_input])
x = tf.keras.layers.Dense(128,activation="relu")(x)
x = tf.keras.layers.Lambda(tf.keras.layers.Dropout(0.5))(x)
x = tf.keras.layers.Dense(128,activation="relu")(x)
main_output = tf.keras.layers.Dense(10,activation="softmax")(x) # 输出2
model = tf.keras