Tensorflow实现多层感知机
2018-08-03 18:28
155 查看
今天对照Tensorflow的书,实现了一个简单的多层感知机。基于MNIST数据集。
多层感知机与之前的Softmax Regression的区别是多了一层隐藏层,但是,本质上还是FCN(全连接神经网络),与卷积神经网络还是有差距的。
下面上代码:
from tensorflow.examples.tutorials.mnist import input_data import tensorflow as tf #加载数据与创建默认的Session mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) sess = tf.InteractiveSession() #定义隐藏层的参数并进行初始化 in_units = 784 h1_units = 300 #将W1初始化为正态分布,并增加一个0.1的标准差 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])) #定义输入x的placeholder,并定义一个dropout(用于随机丢弃) x = tf.placeholder(tf.float32, [None, in_units]) keep_prob = tf.placeholder(tf.float32) #定义模型结构 #定义激活函数为relu的一个隐藏层 hidden1 = tf.nn.relu(tf.matmul(x, W1) + b1) hidden1_drop = tf.nn.dropout(hidden1, keep_prob) #用softmax作为输出层 y = tf.nn.softmax(tf.matmul(hidden1_drop, W2) + b2) #定义损失函数和自适应打优化器Adagrad y_ = tf.placeholder(tf.float32, [None, 10]) cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) train_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy) #训练,保留75%的节点,其余25%设置为0 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}) #进行准确率评测,将节点全部保留,keep_prob设为1 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实现MLP(多层感知机)
- tensorflow31《TensorFlow实战》笔记-04 TensorFlow实现自编码器及多层感知机 code
- 神经网络_多层感知机MLP_以mnist为例(tensorflow和keras实现)
- TensorFlow实现多层感知机
- TensorFlow实现多层感知机
- Tensorflow-多层感知机学习与实现
- Tensorflow 实现多层感知机
- tensorflow实现多层感知机
- TensorFlow实战(二)Tensorflow实现自编码器及多层感知机
- Tensorflow学习之实现多层感知机
- Tensorflow实现多层感知机Multi-layer Preceptron
- TensorFlow教程:TensorFlow实现多层感知机
- Tensorflow 实战 笔记 (一)实现多层感知机
- TensorFlow上实现MLP多层感知机模型
- TensorFlow实现MLP多层感知机模型
- tensorflow实现多层感知机进行手写字识别
- TensorFlow-4 实现多层感知机
- TensorFlow实现多层感知机及可视化训练过程中的数据记录
- TensorFlow实现多层感知机
- TensorFlow实现多层感知机 (4.4节)