检查失败:1 == NumElements()(1比2)必须具有一个元素张量(神经网络度量)

问题描述 投票:0回答:1

我有我的神经网络int TF2,为此,我想制定自己的指标。在我的函数中,我反复抛出每个张量值,并将新值添加到output_list中。我将堆叠为新的y_pred并将其放入mean_absolute_error中。编译是可以的,但是在第一次迭代中,标题出现错误。我在做什么错?

@tf.function 
def custom_metric_mae( y_true , y_pred ):
    output_list=tf.TensorArray(dtype=tf.float32, size=tf.shape(y_pred))
    for i in range(223):
        dphi = abs(y_true[i][0]-y_pred[i][0])
        if(dphi > 0.5):
            output_list.write(i,1 - dphi)
        else:
            output_list.write(i,dphi)     
    y_PredChanged = output_list.stack()
    return tf.metrics.mean_absolute_error(y_true , y_PredChanged)

我的模特:

    model = keras.Sequential([
    keras.layers.Flatten(input_shape=(32,32)),
    keras.layers.Dense(64,activation="relu"),
    keras.layers.Dense(32,activation="relu"),
    keras.layers.Dense(16,activation="relu"),
    keras.layers.Dense(1, activation='linear')
    ])
model.compile(optimizer="adam",loss = "mean_absolute_error",metrics=[custom_metric_mae])
python tensorflow keras metrics
1个回答
0
投票

摘自Keras' custom metrics的文档:

该函数需要将(y_true,y_pred)作为参数并返回单个张量值。

tf.metrics.mean_absolute_error返回two值:实际的MAE和tf.metrics.mean_absolute_error,一旦评估,该值将更新运行值并返回MAE值。我不完全确定这是否与Keras兼容,我建议将其替换为update_op

© www.soinside.com 2019 - 2024. All rights reserved.