TensorFlow官方文档中文版-笔记(三)
2017-11-02 18:14
453 查看
增加隐含层实现MNIST任务
定义模型参数
in_units = 784# in_units是输入节点数 h1_units = 300# h1_units是隐含层的输出节点数 #初始化为截断的正态分布,其标准差为0.1 (因为模型使用的激活函数是Relu,所以需要使用正态分布给参数加点噪声,来打破完全对称并且避免0梯度) W1 = tf.Variable(tf.truncated_normal([in_units, h1_units], stddev=0.1))# 隐含层的权重 b1 = tf.Variable(tf.zeros([h1_units]))# 隐含层的偏置 W2 = tf.Variable(tf.zeros([h1_units, 10]))# 输出层的权重 b2 = tf.Variable(tf.zeros([10]))# 输出层的偏置 # 把Dropout的比率作为计算图的输入,并定义成一个placeholder,Dropout的比率keep_prob(保留节点的概率)是不一样的,通常在训练时小于1,预测时等于1,所以将比率作为计算图的输入 keep_prob = tf.placeholder(tf.float32)
定义模型
# 定义模型结构 # 定义隐含层,实现一个激活函数为ReLU的隐含层,y=relu(w1x+b1) hidden1 = tf.nn.relu(tf.matmul(x, W1) + b1) # 实现Dropout功能,即随机将一部分节点置为0,keep_prob参数为保留数据而不置为0的比例,训练时小于1,用于制造随机性防止过拟合,预测时等于1,使用全部特征来预测样本的类别 hidden1_drop = tf.nn.dropout(hidden1, keep_prob) # 定义输出层 y = tf.nn.softmax(tf.matmul(hidden1_drop, W2) + b2)
准确率为98%!
完整代码如下:
# TensorFlow进阶3—增加隐含层的神经网络 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # Import data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) sess = tf.InteractiveSession() # Step1、2 定义输入和定义算法公式 in_units = 784# in_units是输入节点数 h1_units = 300# h1_units是隐含层的输出节点数 W1 = tf.Variable(tf.truncated_normal([in_units, h1_units], stddev=0.1))# 隐含层的权重 b1 = tf.Variable(tf.zeros([h1_units]))# 隐含层的偏置 W2 = tf.Variable(tf.zeros([h1_units, 10]))# 输出层的权重 b2 = tf.Variable(tf.zeros([10]))# 输出层的偏置 keep_prob = tf.placeholder(tf.float32) x = tf.placeholder(tf.float32, [None, in_units]) hidden1 = tf.nn.relu(tf.matmul(x, W1) + b1) hidden1_drop = tf.nn.dropout(hidden1, keep_prob) y = tf.nn.softmax(tf.matmul(hidden1_drop, W2) + b2) # Step3 定义损失函数cross-entropy y_ = tf.placeholder(tf.float32, [None, 10]) cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) # Step4 定义优化算法-SGD # 选择优化器(此处选择自适应优化器Adagrad)来优化loss train_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy) # Step5 迭代执行训练操作 tf.global_variables_initializer().run() for i in range(3000): batch_xs, batch_ys = mnist.train.next_batch(100) train_step.run({x: batch_xs, y_: batch_ys, keep_prob: 0.75}) # Step6 验证模型准确率 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
相关文章推荐
- TensorFlow官方文档中文版-笔记(五)
- TensorFlow官方文档中文版-笔记(一)
- TensorFlow官方文档中文版-笔记(二)
- TensorFlow官方文档中文版-笔记(八)
- TensorFlow官方文档中文版-笔记(六)
- TensorFlow官方文档中文版-笔记(四)
- TensorFlow官方文档中文版-笔记(七)
- tensorflow学习笔记十七:tensorflow官方文档学习 Convolutional Neural Networks
- TensorFlow 官方文档中文版解读之2——tf.sparse_to_dense的用法
- 人工智能系统Google开源的TensorFlow官方文档中文版
- 深度学习框架TensorFlow 官方文档中文版
- TensorFlow 官方文档中文版解读之1 ——tf.concat的用法的用法
- tensorflow学习笔记十六:tensorflow官方文档学习 如何训练Inception v3模型最后一层
- TensorFlow 官方文档中文版
- TensorFlow 运行官方文档中文版 简单例子出现:IndentationError:unexpected indent
- TensorFlow 官方文档中文版
- 人工智能 深度学习 TensorFlow 官方文档中文版
- TensorFlow 官方文档中文版
- 在Tensorflow官方文档中文版中关于深入MNIST中卷积神经网络代码中常数浅析
- TensorFlow官方文档中文版