包含一個隱含層的全連接神經網絡結構如下:
包含一個隱含層的神經網絡結構圖
以MNIST數據集為例,以上結構的神經網絡訓練如下:
#coding=utf-8 from tensorflow.examples.tutorials.mnist import input_data import tensorflow as tf # 加載數據 mnist = input_data.read_data_sets('/home/workspace/python/tf/data/mnist', one_hot=True) """ # 創建模型 x = tf.placeholder(tf.float32, [None, 784]) W = tf.Variable(tf.zeros([784,10])) b = tf.Variable(tf.zeros([10])) y = tf.matmul(x, W) + b """ x = tf.placeholder(tf.float32, [None, 784]) W1 = tf.Variable(tf.truncated_normal([784, 500], stddev=0.1)) b1 = tf.Variable(tf.zeros([500])) W2 = tf.Variable(tf.truncated_normal([500, 10], stddev=0.1)) b2 = tf.Variable(tf.zeros([10])) layer1 = tf.nn.relu(tf.matmul(x, W1) + b1) y = tf.matmul(layer1, W2) + b2 # 正確的樣本標簽 y_ = tf.placeholder(tf.float32, [None, 10]) # 損失函數選擇softmax后的交叉熵,結果作為y的輸出 cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) sess = tf.InteractiveSession() tf.global_variables_initializer().run() # 訓練過程 for _ in range(5000): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) if _%1000 == 0: # 使用測試集評估准確率 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print (sess.run(accuracy, feed_dict = {x: mnist.test.images, y_: mnist.test.labels}))
注意:權重向量初始化時使用tf.truncated_normal,而不要使用tf.zeros
以上代碼大概能得到97.98%的准確率。
軟件版本
TensorFlow 1.0.1 + Python 2.7.12