不推荐使用Keras模型/图层正则化属性

时间:2017-02-21 17:48:20

标签: tensorflow keras

我正在使用Keras来定义模型,然后我尝试使用Distributed Tensorflow进行校准,就像here一样。

我曾经在链接

中处理正规化损失
model = Sequential()
...... #build keras model
loss = tf.reduce_mean(keras.objectives.mean_squared_error(targets, preds))
# apply regularizers if any
if model.regularizers:
    total_loss = loss * 1.  # copy tensor
    for regularizer in model.regularizers:
        total_loss = regularizer(total_loss)
else:
    total_loss = loss

但是现在正规制定者的财产已被删除,并且警告建议使用losses的{​​{1}}属性,所以我尝试了:

model/layer

但这会导致崩溃。请帮忙

1 个答案:

答案 0 :(得分:0)

正确的做法是:

loss = tf.reduce_mean(keras.objectives.mean_squared_error(targets, preds))
total_loss = loss * 1.  # copy tensor
for reg_loss in model.losses:
total_loss = total8loss + reg_loss