[英]Keras Add_loss throwing Operator Not allowed in Graph error
I created a basic loss function that takes the CDF (cumsum of pdf) and does a mean_squared error between the two.我创建了一个基本的损失函数,它采用 CDF(pdf 的总和)并在两者之间进行 mean_squared 误差。
Here is the code:这是代码:
def tuner_loss(y_true, y_pred):
y_actual=K.cumsum(y_true)
y_pred=K.cumsum(y_pred)
return K.mean(K.square(y_actual-y_pred))
I tried model.add_loss(loss_model)
with loss="None"
then tried loss=loss_model
, then commented out the model.add_loss
and loss_model
and directly put the tuner_loss
function into loss.我尝试了model.add_loss(loss_model)
with loss="None"
然后尝试了loss=loss_model
,然后注释掉了model.add_loss
和loss_model
并直接将tuner_loss
函数放入了损失中。
None of the them worked.他们都没有工作。
Here is that code:这是代码:
input_layer= Input(179,)
y_actual=Input(199,)
x=Dense(32, activation='relu')(input_layer)
x=Dense(64, activation='relu')(x)
x=Dense(32, activation='relu')(x)
x=Dropout(0.2)(x)
x=Dense(64)(x)
output_layer = Dense(199, activation='softmax')(x)
model=Model(inputs=[input_layer, y_actual], outputs=output_layer)
#loss_model= tuner_loss(y_actual, output_layer)
#model.add_loss(loss_model)
model.compile(optimizer='adam', loss= tuner_loss(y_actual,output_layer), metrics=['accuracy'])
Any help would be greatly appreciated, I figure it has to be something with how I'm manipulating the tf.tensors in the tuner_loss
.任何帮助将不胜感激,我认为这必须与我如何操纵 tuner_loss 中的tuner_loss
。
Here is error message I got running last code:这是我运行最后一个代码时收到的错误消息:
"OperatorNotAllowedInGraphError using a
tf.Tensor
as a Pythonbool
is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function. "OperatorNotAllowedInGraphError 使用tf.Tensor
作为 Pythonbool
在图形执行中是不允许的。使用 Eager 执行或使用 @tf.function 装饰此函数。
I tried the decorator on the tuner, that also threw an error, so that didn't work.我在调谐器上尝试了装饰器,它也抛出了一个错误,所以没有用。
Neither did enabling eager execution...也没有启用急切执行......
Why not use the standard?为什么不使用标准?
model = Model(input_layer, output_layer)
model.compile(loss = tuner_loss, ...)
model.fit(input_data, output_data, ...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.