keras损失函数(来自keras输入)

keras损失函数(来自keras输入),keras,Keras,我引用了以下链接: 但我得到了一个错误:“TypeError:无法将符号Keras输入/输出转换为numpy数组。此错误可能表示您试图将符号值传递给NumPy调用,这是不受支持的。或者,您可能试图将Keras符号输入/输出传递给不注册分派的tfapi,从而阻止Keras自动将API调用转换为功能模型中的lambda层。" 这是源代码:发生了什么 def custom_loss_wrapper(input_tensor): def custom_loss(y_true, y_pred):

我引用了以下链接:

但我得到了一个错误:“TypeError:无法将符号Keras输入/输出转换为numpy数组。此错误可能表示您试图将符号值传递给NumPy调用,这是不受支持的。或者,您可能试图将Keras符号输入/输出传递给不注册分派的tfapi,从而阻止Keras自动将API调用转换为功能模型中的lambda层。"

这是源代码:发生了什么

def custom_loss_wrapper(input_tensor):
    def custom_loss(y_true, y_pred):
        return K.binary_crossentropy(y_true, y_pred) + K.mean(input_tensor)
    return custom_loss

input_tensor = Input(shape=(10,))
hidden = Dense(100, activation='relu')(input_tensor)
out = Dense(1, activation='sigmoid')(hidden)
model = Model(input_tensor, out)
model.compile(loss=custom_loss_wrapper(input_tensor), optimizer='adam')

X = np.random.rand(1000, 10)
y = np.random.rand(1000, 1)
model.train_on_batch(X, y)  

在tf 2.0中,默认情况下启用了“急切模式”。无法在“急切模式”中获得此功能,因为上面的示例目前已编写完毕。我认为有一些更高级的编程方法可以在“急切模式”中实现此功能。但除此之外,关闭“急切模式”并在图形模式下运行是很简单的:

from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()

当我收到类似错误时,我执行了以下操作:

del model
之前:

model = Model(input_tensor, out)
它解决了我的问题,你可以试一试。 我很想知道它是否能解决您的问题:)