How to add regularizations in TensorFlow?

后端 未结 10 1332
慢半拍i
慢半拍i 2020-12-07 06:55

I found in many available neural network code implemented using TensorFlow that regularization terms are often implemented by manually adding an additional term to loss valu

10条回答
  •  -上瘾入骨i
    2020-12-07 07:30

    If anyone's still looking, I'd just like to add on that in tf.keras you may add weight regularization by passing them as arguments in your layers. An example of adding L2 regularization taken wholesale from the Tensorflow Keras Tutorials site:

    model = keras.models.Sequential([
        keras.layers.Dense(16, kernel_regularizer=keras.regularizers.l2(0.001),
                           activation=tf.nn.relu, input_shape=(NUM_WORDS,)),
        keras.layers.Dense(16, kernel_regularizer=keras.regularizers.l2(0.001),
                           activation=tf.nn.relu),
        keras.layers.Dense(1, activation=tf.nn.sigmoid)
    ])
    

    There's no need to manually add in the regularization losses with this method as far as I know.

    Reference: https://www.tensorflow.org/tutorials/keras/overfit_and_underfit#add_weight_regularization

提交回复
热议问题