【发布时间】:2019-12-26 21:51:58
【问题描述】:
我正在创建自己的损失函数(我想在 Keras 的急切执行中使用它)。 我想在其中添加一个类似于 l1 损失函数的术语。
这就是我现在使用的损失函数
def loss(model, x, y, x_dev, y_dev, variables):
y_ = model(x)
y_dev_ = model(x_dev)
y_temp = 1.5
return loss_mae(y_true=y, y_pred=y_)+y_temp*
K.mean(tf.convert_to_tensor(variables))
与
import keras.backend as K
def loss_mae(y_true, y_pred):
return K.mean(K.abs(y_pred-y_true))
我的想法是在我的损失函数中添加一个常数 (y_temp),然后我想将它与可训练变量相乘(以实现类似于 l1 正则化项的效果)。
我尝试传递给loss() 函数model.trainable_variables 但这不起作用,我得到了一个
TypeError: can't multiply sequence by non-int of type 'numpy.float32'
有人有什么建议吗?
【问题讨论】:
标签: python tensorflow keras neural-network loss-function