本部分的代碼目前都是基於GitHub大佬非常詳細的TensorFlow的教程上,首先給出鏈接:
https://github.com/aymericdamien/TensorFlow-Examples/
本人對其中部分代碼做了注釋和中文翻譯,會持續更新,目前包括:
1. 傳統多層神經網絡用語MNIST數據集分類(代碼講解,翻譯)
1. 傳統多層神經網絡用語MNIST數據集分類(代碼講解,翻譯)
1 """ Neural Network. 2 3 A 2-Hidden Layers Fully Connected Neural Network (a.k.a Multilayer Perceptron) 4 implementation with TensorFlow. This example is using the MNIST database 5 of handwritten digits (http://yann.lecun.com/exdb/mnist/). 6 7 Links: 8 [MNIST Dataset](http://yann.lecun.com/exdb/mnist/). 9 10 Author: Aymeric Damien 11 Project: https://github.com/aymericdamien/TensorFlow-Examples/ 12 """ 13 14 from __future__ import print_function 15 16 # Import MNIST data 17 # 導入mnist數據集 18 from tensorflow.examples.tutorials.mnist import input_data 19 mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) 20 21 # 導入tf 22 import tensorflow as tf 23 24 # Parameters 25 # 設定各種超參數 26 learning_rate = 0.1 # 學習率 27 num_steps = 500 # 訓練500次 28 batch_size = 128 # 每批次取128個樣本訓練 29 display_step = 100 # 每訓練100步顯示一次 30 31 # Network Parameters 32 # 設定網絡的超參數 33 n_hidden_1 = 256 # 1st layer number of neurons 34 n_hidden_2 = 256 # 2nd layer number of neurons 35 num_input = 784 # MNIST data input (img shape: 28*28) 36 num_classes = 10 # MNIST total classes (0-9 digits) 37 38 # tf Graph input 39 # tf圖的輸入,因為不知道到底輸入大小是多少,因此設定占位符 40 X = tf.placeholder("float", [None, num_input]) 41 Y = tf.placeholder("float", [None, num_classes]) 42 43 # Store layers weight & bias 44 # 初始化w和b 45 weights = { 46 'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])), 47 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), 48 'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes])) 49 } 50 biases = { 51 'b1': tf.Variable(tf.random_normal([n_hidden_1])), 52 'b2': tf.Variable(tf.random_normal([n_hidden_2])), 53 'out': tf.Variable(tf.random_normal([num_classes])) 54 } 55 56 57 # Create model 58 # 創建模型 59 def neural_net(x): 60 # Hidden fully connected layer with 256 neurons 61 # 隱藏層1,全連接了256個神經元 62 layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) 63 # Hidden fully connected layer with 256 neurons 64 # 隱藏層2,全連接了256個神經元 65 layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']) 66 # Output fully connected layer with a neuron for each class 67 # 最后作為輸出的全連接層,對每一分類連接一個神經元 68 out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] 69 return out_layer 70 71 # Construct model 72 # 開啟模型 73 # 輸入數據X,得到得分向量logits 74 logits = neural_net(X) 75 # 用softmax分類器將得分向量轉變成概率向量 76 prediction = tf.nn.softmax(logits) 77 78 # Define loss and optimizer 79 # 定義損失和優化器 80 # 交叉熵損失, 求均值得到---->loss_op 81 loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits( 82 logits=logits, labels=Y)) 83 # 優化器使用的是Adam算法優化器 84 optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) 85 # 最小化損失得到---->可以訓練的train_op 86 train_op = optimizer.minimize(loss_op) 87 88 # Evaluate model 89 # 評估模型 90 # tf.equal() 逐個元素進行判斷,如果相等就是True,不相等,就是False。 91 correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1)) 92 # tf.cast() 數據類型轉換----> tf.reduce_mean() 再求均值 93 accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) 94 95 # Initialize the variables (i.e. assign their default value) 96 # 初始化這些變量(作用比如說,給他們分配隨機默認值) 97 init = tf.global_variables_initializer() 98 99 # Start training 100 # 現在開始訓練啦! 101 with tf.Session() as sess: 102 103 # Run the initializer 104 # 運行初始化器 105 sess.run(init) 106 107 for step in range(1, num_steps+1): 108 # 每批次128個訓練,取出這128個對應的data:x;標簽:y 109 batch_x, batch_y = mnist.train.next_batch(batch_size) 110 # Run optimization op (backprop) 111 # train_op是優化器得到的可以訓練的op,通過反向傳播優化模型 112 sess.run(train_op, feed_dict={X: batch_x, Y: batch_y}) 113 # 每100步打印一次訓練的成果 114 if step % display_step == 0 or step == 1: 115 # Calculate batch loss and accuracy 116 # 計算每批次的是損失和准確度 117 loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x, 118 Y: batch_y}) 119 print("Step " + str(step) + ", Minibatch Loss= " + \ 120 "{:.4f}".format(loss) + ", Training Accuracy= " + \ 121 "{:.3f}".format(acc)) 122 123 print("Optimization Finished!") 124 125 # Calculate accuracy for MNIST test images 126 # 看看在測試集上,我們的模型表現如何 127 print("Testing Accuracy:", \ 128 sess.run(accuracy, feed_dict={X: mnist.test.images, 129 Y: mnist.test.labels}))