在前面实现的RELU连接的两层神经网络中,加Regularization进行约束,采用加l2 norm的方法,进行负反馈:
代码实现上,只需要对tf_sgd_relu_nn中train_loss做修改即可:
l2_loss = tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels)) + 0.001 * l2_loss
在训练数据很少的时候,会出现训练结果准确率高,但测试结果准确率低的情况
offset_range = 1000
offset = (step * batch_size) % offset_range
采取Dropout方式强迫神经网络学习更多知识
参考aymericdamien/TensorFlow-Examples中dropout的使用
keep_prob = tf.placeholder(tf.float32)
if drop_out:
hidden_drop = tf.nn.dropout(hidden, keep_prob)
h_fc = hidden_drop
if drop_out:
hidden_drop = tf.nn.dropout(hidden, 0.5)
h_fc = hidden_drop
随着训练次数增加,自动调整步长
增加神经网络层数,增加训练次数到20000
# middle layer
for i in range(layer_cnt - 2):
y1 = tf.matmul(hidden_drop, weights[i]) + biases[i]
hidden_drop = tf.nn.relu(y1)
if drop_out:
keep_prob += 0.5 * i / (layer_cnt + 1)
hidden_drop = tf.nn.dropout(hidden_drop, keep_prob)
for i in range(layer_cnt - 2):
if hidden_cur_cnt > 2:
hidden_next_cnt = int(hidden_cur_cnt / 2)
else:
hidden_next_cnt = 2
hidden_stddev = np.sqrt(2.0 / hidden_cur_cnt)
weights.append(tf.Variable(tf.truncated_normal([hidden_cur_cnt, hidden_next_cnt], stddev=hidden_stddev)))
biases.append(tf.Variable(tf.zeros([hidden_next_cnt])))
hidden_cur_cnt = hidden_next_cnt
stddev = np.sqrt(2.0 / n)
keep_prob += 0.5 * i / (layer_cnt + 1)