在张量流顺序回归模型中实现l2损失

时间:2019-10-25 10:33:17

标签: python-3.x tensorflow keras

我创建了一个kerastensorflow模型,受 this guide 看起来像

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import time 
import numpy as np
import sys
from keras import losses


model = keras.Sequential()
model.add(layers.Dense(nodes,activation = tf.keras.activations.relu, input_shape=[len(data_initial.keys())]))
model.add(layers.Dense(64,activation = tf.keras.activations.relu))
model.add(layers.Dropout(0.1, noise_shape=None))
model.add(layers.Dense(1))

model.compile(loss='mse',    # <-------- Here we define the loss function 
              optimizer=tf.keras.optimizers.Adam(lr= 0.01,
                                                beta_1 = 0.01,
                                                beta_2 = 0.001,
                                                epsilon= 0.03),
                                                metrics=['mae', 'mse'])
model.fit(train_data,train_labels,epochs = 200)

这是一个回归模型,我想使用 loss ='mse' tf keras mse loss L2正则化术语。问题是

  • 如何在 model.compile 语句中添加预定义的正则化函数(我认为是this one)。

  • 如何编写完全自定义的损失函数并将其添加到 model.compile

1 个答案:

答案 0 :(得分:1)

您可以将正则化添加为图层参数或图层。

将其用作图层参数如下所示

model.add(layers.Dense(8, 
          kernel_regularizer=regularizers.l2(0.01),
          activity_regularizer=regularizers.l1(0.01)))

具有第一个密集层正则化和自定义损失函数的样本代码

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import time 
import numpy as np
import sys
from keras import losses
from keras import regularizers
import keras.backend as K


model = keras.Sequential()
model.add(layers.Dense(8,activation = tf.keras.activations.relu, input_shape=(8,), 
                       kernel_regularizer=regularizers.l2(0.01), 
                       activity_regularizer=regularizers.l1(0.01)))

model.add(layers.Dense(4,activation = tf.keras.activations.relu))
model.add(layers.Dropout(0.1, noise_shape=None))
model.add(layers.Dense(1))


def custom_loss(y_true, y_pred):
    return K.mean(y_true - y_pred)**2

model.compile(loss=custom_loss,
              optimizer=tf.keras.optimizers.Adam(lr= 0.01,
                                                beta_1 = 0.01,
                                                beta_2 = 0.001,
                                                epsilon= 0.03),
                                                metrics=['mae', 'mse'])

model.fit(np.random.randn(10,8),np.random.randn(10,1),epochs = 1)