nai*_*bah 3 python keras tensorflow
在Keras中,对于密集层,我们可以使用参数activity_regularizer。在Tensorflow中,没有类似的参数。
凯拉斯:
from keras import regularizers
encoding_dim = 32
input_img = Input(shape=(784,))
# add a Dense layer with a L1 activity regularizer
encoded = Dense(encoding_dim, activation='relu', activity_regularizer=regularizers.l1(10e-5))(input_img)
decoded = Dense(784, activation='sigmoid')(encoded)
autoencoder = Model(input_img, decoded)
Run Code Online (Sandbox Code Playgroud)
如何在tensorflow中制作一个activity_regularizer?
Keras文档不是太精确,但是据我所知,活动正则化只是将特定层的输出添加到模型的相应损失函数中的一个L1或L2术语。
假设您有一些损失,例如某些标签的MSE:
loss = tf.metrics.mean_squared_error(labels, model_output)
Run Code Online (Sandbox Code Playgroud)
要将L1活动正则化添加到某个层,您只需将具有该层输出强度的L1正则化项添加到具有一定正则化强度的损失中(我会10e-5像在您的问题中给出的那样):
loss += 10e-5*tf.nn.l1_loss(layer_output)
Run Code Online (Sandbox Code Playgroud)
layer_output您要调节的图层的输出在哪里。
如果对层的权重而不是其输出进行相同的操作,则将获得Keras 文档所说的内核正则化。如果对该层的偏差向量执行相同操作,则将获得Keras的偏差正则化。
| 归档时间: |
|
| 查看次数: |
1722 次 |
| 最近记录: |