我遇到了要转换为keras的代码:
l2 = lambda_loss_amount * sum(
tf.nn.l2_loss(tf_var) for tf_var in tf.trainable_variables()
) # L2 loss prevents this overkill neural network to overfit the data
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=pred)) + l2 # Softmax loss
这将如何编写为Keras损失函数?
答案 0 :(得分:1)
您可以在keras层上使用激活和kernel_regularizer,如下所示:
Dense(..., activation='softmax', kernel_regularizer=regularizers.l2(0))
答案 1 :(得分:1)
请参阅here,以了解有关喀拉拉邦正则化器的描述。这是一个玩具示例:
from keras import regularizers
model.add(Dense(64, input_dim=64,
kernel_regularizer=regularizers.l2(lambda_loss_amount),
bias_regularizer=regularizers.l2(lambda_loss_amount)))