如何精确地将L1正则化添加到张量流误差函数 [英] How to exactly add L1 regularisation to tensorflow error function

查看:129
本文介绍了如何精确地将L1正则化添加到张量流误差函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我是tensorflow的新手,即使经过很多努力也无法添加 L1正则化项到误差项

x = tf.placeholder("float", [None, n_input])
# Weights and biases to hidden layer
ae_Wh1 = tf.Variable(tf.random_uniform((n_input, n_hidden1), -1.0 / math.sqrt(n_input), 1.0 / math.sqrt(n_input)))
ae_bh1 = tf.Variable(tf.zeros([n_hidden1]))
ae_h1 = tf.nn.tanh(tf.matmul(x,ae_Wh1) + ae_bh1)

ae_Wh2 = tf.Variable(tf.random_uniform((n_hidden1, n_hidden2), -1.0 / math.sqrt(n_hidden1), 1.0 / math.sqrt(n_hidden1)))
ae_bh2 = tf.Variable(tf.zeros([n_hidden2]))
ae_h2 = tf.nn.tanh(tf.matmul(ae_h1,ae_Wh2) + ae_bh2)

ae_Wh3 = tf.transpose(ae_Wh2)
ae_bh3 = tf.Variable(tf.zeros([n_hidden1]))
ae_h1_O = tf.nn.tanh(tf.matmul(ae_h2,ae_Wh3) + ae_bh3)

ae_Wh4 = tf.transpose(ae_Wh1)
ae_bh4 = tf.Variable(tf.zeros([n_input]))
ae_y_pred = tf.nn.tanh(tf.matmul(ae_h1_O,ae_Wh4) + ae_bh4)



ae_y_actual = tf.placeholder("float", [None,n_input])
meansq = tf.reduce_mean(tf.square(ae_y_actual - ae_y_pred))
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(meansq)

在此之后,我使用

init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

n_rounds = 100
batch_size = min(500, n_samp)
for i in range(100):
    sample = np.random.randint(n_samp, size=batch_size)
    batch_xs = input_data[sample][:]
    batch_ys = output_data_ae[sample][:]
    sess.run(train_step, feed_dict={x: batch_xs, ae_y_actual:batch_ys})

以上是4层自动编码器的代码, "meansq"是我的平方损失函数.如何在网络中为权重矩阵(张量)添加L1重新说明?

解决方案

您可以使用TensorFlow的 l1_regularizer 方法. 注意:这是针对Tensorflow 1的,而API在Tensorflow 2中进行了更改,请参见下面的编辑.

基于您的问题的示例:

 import tensorflow as tf

total_loss = meansq #or other loss calcuation
l1_regularizer = tf.contrib.layers.l1_regularizer(
   scale=0.005, scope=None
)
weights = tf.trainable_variables() # all vars of your graph
regularization_penalty = tf.contrib.layers.apply_regularization(l1_regularizer, weights)

regularized_loss = total_loss + regularization_penalty # this loss needs to be minimized
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(regularized_loss)
 

注意:weightslist,其中每个条目都是tf.Variable.

已编辑: 正如 Paddy 正确指出的那样,在Tensorflow 2中,他们更改了正则化程序的API.在Tensorflow 2中,此处中描述了L1正则化.. >

Hey I am new to tensorflow and even after a lot of efforts could not add L1 regularisation term to the error term

x = tf.placeholder("float", [None, n_input])
# Weights and biases to hidden layer
ae_Wh1 = tf.Variable(tf.random_uniform((n_input, n_hidden1), -1.0 / math.sqrt(n_input), 1.0 / math.sqrt(n_input)))
ae_bh1 = tf.Variable(tf.zeros([n_hidden1]))
ae_h1 = tf.nn.tanh(tf.matmul(x,ae_Wh1) + ae_bh1)

ae_Wh2 = tf.Variable(tf.random_uniform((n_hidden1, n_hidden2), -1.0 / math.sqrt(n_hidden1), 1.0 / math.sqrt(n_hidden1)))
ae_bh2 = tf.Variable(tf.zeros([n_hidden2]))
ae_h2 = tf.nn.tanh(tf.matmul(ae_h1,ae_Wh2) + ae_bh2)

ae_Wh3 = tf.transpose(ae_Wh2)
ae_bh3 = tf.Variable(tf.zeros([n_hidden1]))
ae_h1_O = tf.nn.tanh(tf.matmul(ae_h2,ae_Wh3) + ae_bh3)

ae_Wh4 = tf.transpose(ae_Wh1)
ae_bh4 = tf.Variable(tf.zeros([n_input]))
ae_y_pred = tf.nn.tanh(tf.matmul(ae_h1_O,ae_Wh4) + ae_bh4)



ae_y_actual = tf.placeholder("float", [None,n_input])
meansq = tf.reduce_mean(tf.square(ae_y_actual - ae_y_pred))
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(meansq)

after this I run the above graph using

init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

n_rounds = 100
batch_size = min(500, n_samp)
for i in range(100):
    sample = np.random.randint(n_samp, size=batch_size)
    batch_xs = input_data[sample][:]
    batch_ys = output_data_ae[sample][:]
    sess.run(train_step, feed_dict={x: batch_xs, ae_y_actual:batch_ys})

Above is the code for a 4 layer autoencoder, "meansq" is my squared loss function. How can I add L1 reguarisation for the weight matrix (tensors) in the network?

解决方案

You can use TensorFlow's apply_regularization and l1_regularizer methods. Note: this is for Tensorflow 1, and the API changed in Tensorflow 2, see edit below.

An example based on your question:

import tensorflow as tf

total_loss = meansq #or other loss calcuation
l1_regularizer = tf.contrib.layers.l1_regularizer(
   scale=0.005, scope=None
)
weights = tf.trainable_variables() # all vars of your graph
regularization_penalty = tf.contrib.layers.apply_regularization(l1_regularizer, weights)

regularized_loss = total_loss + regularization_penalty # this loss needs to be minimized
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(regularized_loss)

Note: weights is a list where each entry is a tf.Variable.

Edited: As Paddy correctly noted, in Tensorflow 2 they changed the API for regularizers. In Tensorflow 2, L1 regularization is described here.

这篇关于如何精确地将L1正则化添加到张量流误差函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆