我引用了这个链接:Keras custom loss function: Accessing current input pattern。
但我得到了错误:“TypeError:无法将符号Keras输入/输出转换为数字数组。此错误可能表明您正在尝试将符号值传递给NumPy调用,而这是不受支持的。或者,您可能正在尝试将Keras符号输入/输出传递给未注册调度的that,从而阻止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) 发布于 2021-02-27 01:06:50
在tf 2.0中,默认情况下启用了eager模式。由于上面的示例是当前编写的,所以不可能在急切模式下获得此功能。我认为有一些方法可以在渴望模式下通过一些更高级的编程来实现。但除此之外,关闭eager模式并在图形模式下运行是一件很简单的事情:
from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()发布于 2021-04-26 06:50:40
当我收到类似的错误时,我执行了以下操作:
del model之前:
model = Model(input_tensor, out)它解决了我的问题,你可以试一试。我很想知道它是否解决了您的问题:)。
https://stackoverflow.com/questions/66287143
复制相似问题