2016-04-19 36 views
3

嘿,我是新来tensorflow,甚至经过大量的努力可以在此之后 L1则项不会增加误差项如何准确地添加L1正规化tensorflow误差函数

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 reguarisation?

+0

L1可以用sum和abs运算符来实现,这两个运算符都存在于tensorflow(包括它们的梯度) –

+7

'0.001 * tf.reduce_sum(tf.abs(parameters))'给出了参数向量的L1范数在技​​术上可能是一个更高的排名张量),所以惩罚你的学习 –

+0

非常感谢你+雅罗斯拉夫。因此,对于我的情况,它应该像(?) meansq = tf.reduce_mean(tf.square(ae_y_actual-ae_y_pred))+ 0.001 * tf.reduce_sum(tf.abs(ae_Wh1))+ 0.001 * tf。 reduce_sum(tf.abs(ae_Wh1)) 我是否正确? – Abhishek

回答

2

您可以使用TensorFlow的apply_regularizationl1_regularizer方法。

根据你的问题的一个例子:

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

+2

是tf.trainable_variables()还包括偏差它应该是 – Shaowu

+0

。 tf.trainable_variables()返回一个变量列表,所以你可以遍历它们来查看变量是否真的在那里。 (请参阅https://www.tensorflow.org/programmers_guide/variables) – bruThaler

+0

我问的原因是,通常情况下,人们不会正规化,正如您在许多论文中看到的那样,简单地说,权重就是正则化的。 – Shaowu

0

您还可以使用slim losses中的tf.slim.l1_regularizer()。

+0

如果你包含一个小代码示例,你的答案可能会更有帮助 –

相关问题