1
我已經實現了CNN
數字分類模型。我的模型過度配合,爲了克服過度配合,我試圖在我的成本函數中使用L2 Regularization
。我有一個小混亂 我怎麼能選擇<weights>
把成本公式(代碼的最後一行)。如何實現卷積神經網絡的L2正則化成本函數
...
x = tf.placeholder(tf.float32, shape=[None, img_size, img_size, num_channels], name='x') # Input
y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true') # Labels
<Convolution Layer 1>
<Convolution Layer 2>
<Convolution Layer 3>
<Fully Coonected 1>
<Fully Coonected 2> O/P = layer_fc2
# Loss Function
lambda = 0.01
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=layer_fc2, labels=y_true)
# cost = tf.reduce_mean(cross_entropy) # Nornmal Loss
cost = tf.reduce_mean(cross_entropy + lambda * tf.nn.l2_loss(<weights>)) # Regularized Loss
...