ValueError: Output tensors to a Model must be the output of a TensorFlow `Layer`

在学习tensorflow.keras的多输入多输出时遇到错误:ValueError: Output tensors to a Model must be the output of a TensorFlow `Layer`。错误源于中间操作没有Keras层的历史信息。解决方案是使用tf.keras.layers.Lambda封装tf.keras.backend.concatenate函数。参考了相关博客文章进行问题解决。
摘要由CSDN通过智能技术生成

最近在看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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值