TensorFlow实战2——TensorFlow实现多层感知机
1 #coding = utf-8 2 from tensorflow.examples.tutorials.mnist import input_data 3 import tensorflow as tf 4 5 mnist = input_data.read_data_sets("MNIST_data/", one_hot = True) 6 #创建一个IntercativeSession,这样后面的操作就无需指定Session 7 sess = tf.InteractiveSession() 8 9 #隐含层输出节点设置为300,(在此模型中隐含节点数设在200~1000结果区别不大) 10 in_units = 784 11 h1_units = 300 12 #利用tf.truncated_normal实现截断的正态分布,其标准差为0.1 [-1, 784]x[784, 300] 13 w1 = tf.Variable(tf.truncated_normal([in_units, h1_units], stddev=0.1)) 14 b1 = tf.Variable(tf.zeros([h1_units])) 15 #[784, 300]x[300, 10] 16 w2 = tf.Variable(tf.zeros([h1_units, 10])) 17 b2 = tf.Variable(tf.zeros([10])) 18 19 #定义输入x,Dropout的比率keep_prob(通常在训练时小于1,而预测时等于1) 20 x = tf.placeholder(tf.float32, [None, in_units]) 21 y_ = tf.placeholder(tf.float32, [None, 10]) 22 keep_prob = tf.placeholder(tf.float32) 23 24 #hidden1:隐含层 y = relu(W1*x+b1) 25 hidden1 = tf.nn.relu(tf.matmul(x, w1) + b1) 26 '''调用tf.nn.dropout实现Dropout,keep_prob在训练时小于1,用于制造随机性,防止过拟合; 27 在预测时等于1,即使用全部特征来预测样本类别''' 28 hidden1_drop = tf.nn.dropout(hidden1, keep_prob) 29 30 #prediction 31 y = tf.nn.softmax(tf.matmul(hidden1_drop, w2)+b2) 32 33 cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y), reduction_indices=[1])) 34 trian_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy) 35 36 tf.global_variables_initializer().run() 37 38 for i in range(3000): 39 batch_xs, batch_ys = mnist.train.next_batch(100) 40 trian_step.run({x: batch_xs, y_: batch_ys, keep_prob: 0.75}) 41 #out correct prediction 42 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) 43 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 44 if i % 500 == 0: 45 print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
1 0.2318 2 0.9584 3 0.9709 4 0.9761 5 0.9778 6 0.9782