嘿,我是新來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?
L1可以用sum和abs運算符來實現,這兩個運算符都存在於tensorflow(包括它們的梯度) –
'0.001 * tf.reduce_sum(tf.abs(parameters))'給出了參數向量的L1範數在技術上可能是一個更高的排名張量),所以懲罰你的學習 –
非常感謝你+雅羅斯拉夫。因此,對於我的情況,它應該像(?) 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