Keras 错误:无法将符号 Keras 输入/输出转换为 numpy 数组
Posted
技术标签:
【中文标题】Keras 错误:无法将符号 Keras 输入/输出转换为 numpy 数组【英文标题】:Keras error: Cannot convert a symbolic Keras input/output to a numpy array 【发布时间】:2021-12-17 06:40:44 【问题描述】:我有来自 RaGan(相对论平均 Gan)的这段代码(部分代码):
def get_ragan_network(generator,discriminator,optimizer):
imgs_hr = Input(image_shape)
generated_hr = Input(image_shape)
Discriminator_real_out = discriminator(imgs_hr)
Discriminator_fake_out = discriminator(generated_hr)
Real_Fake_relativistic_average_out = tf.add(Discriminator_real_out,-(K.mean(Discriminator_fake_out, axis=0)))
Fake_Real_relativistic_average_out = tf.add(Discriminator_fake_out,-(K.mean(Discriminator_real_out, axis=0)))
epsilon=0.000001
def relativistic_discriminator_loss(y_true, y_pred):
if isinstance(Real_Fake_relativistic_average_out, np.ndarray):
return -(K.mean(K.log(K.sigmoid(Real_Fake_relativistic_average_out)+epsilon ),axis=0)
+K.mean(K.log(1-K.sigmoid(Fake_Real_relativistic_average_out)+epsilon),axis=0))
else:
return -(K.mean(K.log(K.sigmoid(Real_Fake_relativistic_average_out)+epsilon ),axis=0)
+K.mean(K.log(1-K.sigmoid(Fake_Real_relativistic_average_out)+epsilon),axis=0))
model = Model([generated_hr,imgs_hr],[Discriminator_real_out,Discriminator_fake_out])
model.compile(optimizer=optimizer, loss=[relativistic_discriminator_loss,None])
return model
但是当我执行代码时,我得到了这个错误:
Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.
我不知道如何解决。
Numpy 版本 = 1.19
张量流版本 = 2.6
【问题讨论】:
【参考方案1】:我认为错误来自这一行:isinstance(Real_Fake_relativistic_average_out, np.ndarray)
,因为Real_Fake_relativistic_average_out
来自鉴别器输出,而np.ndarray
是一个 numpy 对象。显然,条件 if else 语句是无用的。
【讨论】:
不不,这不是错误,如果我删除条件 if else 我得到相同的错误。我已经从 DRaGan、RaGan 等中探索了其他旧示例......并且都给了我同样的错误。我认为问题可能是numpy版本或tensorflow版本,但我需要找到一个没有降级版本的解决方案。以上是关于Keras 错误:无法将符号 Keras 输入/输出转换为 numpy 数组的主要内容,如果未能解决你的问题,请参考以下文章
无法向自定义损失输入数据:渴望执行函数的输入不能是 Keras 符号张量