【发布时间】:2021-02-08 22:08:05
【问题描述】:
我想使用 y_true 和 y_pred 为 tensorflow 模型创建自定义损失函数,但出现以下错误: ValueError:无法从形状推断 num(无,1) 这是我的自定义指标:
def custom_metric(y_true,y_pred):
y_true = float(y_true)
y_pred = float(y_pred)
y_true = tf.unstack(y_true)
y_pred = tf.unstack(y_pred)
sqr_pred_error = K.square(y_true - y_pred)
sqr_y_true = K.square(y_true)
r = []
for i in y_true:
if sqr_pred_error[i] < sqr_y_true[i] or sqr_pred_error[i] == sqr_y_true[i]:
result = 1
print("result: 1")
else:
result = 0
print("result: 0")
r.append(result)
r = tf.stack(r)
return K.sum(r)/K.shape(r)
【问题讨论】:
标签: tensorflow model loss-function