轉載請註明做者:夢裏風林
Github工程地址:https://github.com/ahangchen/GDLnotes
歡迎star,有問題能夠到Issue區討論
官方教程地址
視頻/字幕下載html
輔助閱讀:TensorFlow中文社區教程 - 英文官方教程node
代碼見:full_connect.pypython
def reformat(dataset, labels): dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32) # Map 0 to [1.0, 0.0, 0.0 ...], 1 to [0.0, 1.0, 0.0 ...] labels = (np.arange(num_labels) == labels[:, None]).astype(np.float32) return dataset, labels
optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
上面這些變量都是一種Tensor的概念,它們是一個個的計算單元,咱們在Graph中設置了這些計算單元,規定了它們的組合方式,就好像把一個個門電路串起來那樣git
Session用來執行Graph裏規定的計算,就好像給一個個門電路通上電,咱們在Session裏,給計算單元衝上數據,That’s Flow.github
with tf.Session(graph=graph) as session: tf.initialize_all_variables().run() for step in range(num_steps): _, l, predictions = session.run([optimizer, loss, train_prediction])
valid_prediction.eval()
這樣訓練的準確度爲83.2%算法
offset = (step * batch_size) % (train_labels.shape[0] - batch_size) batch_data = train_dataset[offset:(offset + batch_size), :] batch_labels = train_labels[offset:(offset + batch_size), :]
tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
準確率提升到86.5%,並且準確率隨訓練次數增長而提升的速度變快了api
Y = W2 * RELU(W1*X + b1) + b2
[n * 10] = RELU([n * 784] · [784 * N] + [n * N]) · [N * 10] + [n * 10]
weights1 = tf.Variable( tf.truncated_normal([image_size * image_size, hidden_node_count])) biases1 = tf.Variable(tf.zeros([hidden_node_count])) weights2 = tf.Variable( tf.truncated_normal([hidden_node_count, num_labels])) biases2 = tf.Variable(tf.zeros([num_labels]))
ys = tf.matmul(tf_train_dataset, weights1) + biases1 hidden = tf.nn.relu(ys) logits = tf.matmul(hidden, weights2) + biases2
代碼見nn_overfit.py網絡
在前面實現的RELU鏈接的兩層神經網絡中,加Regularization進行約束,採用加l2 norm的方法,進行調節:session
代碼實現上,只須要對tf_sgd_relu_nn中train_loss作修改便可:app
l2_loss = tf.nn.l2_loss(weights1) + tf.nn.l2_loss(weights2)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, 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)
以爲個人文章對您有幫助的話,給個star可好?
土豪能夠打賞支持,一分也是愛: