Tensorflow实现多层感知机Multi-layer Preceptron

# coding:utf-8
'''
使用Tensorflow训练神经网络的4个步骤
①定义算法公式 即神经网络的forward时的计算
②定义损失函数和选择优化器来优化loss
③训练步骤
④对模型进行准确率评测

隐含层:解决XOR问题
神经网络的隐藏层越多就可以对原特征进行越抽象的变换 模型的拟合能力越强

Tensorflow实现多层感知机Multi-layer Preceptron
'''

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf 

mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)
# 创建一个Tensorlfow默认的session 后面的操作就无需指定Session
sess = tf.InteractiveSession()

in_units = 784
h1_units = 300
# 将权重初始化为截断的正态分布 标准差为0.1 正态分布给参数加一些噪声 打破完全对称且避免0梯度
w1 = tf.Variable(tf.truncated_normal([in_units, h1_units], stddev=0.1))
b1 = tf.Variable(tf.zeros([h1_units]), dtype=tf.float32)
w2 = tf.Variable(tf.zeros([h1_units,10]), dtype=tf.float32)
b2 = tf.Variable(tf.zeros([10]), dtype=tf.float32)

x = tf.placeholder(tf.float32, [None, in_units])
keep_prob = tf.placeholder(tf.float32) #dropout的比率 通常训练小于1 测试等于1

# 定义模型结构
hidden1 = tf.nn.relu(tf.matmul(x, w1) + b1) #激活函数为relu的隐藏层
hidden1_drop = tf.nn.dropout(hidden1, keep_prob) #dropout防止过拟合
y = tf.nn.softmax(tf.matmul(hidden1_drop, w2) + b2) #softmax输出层

# 定义损失函数和选择优化器来优化loss
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)

# 训练步骤
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})

# 对模型进行准确率评测
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}))


没有隐含层的softmax regression只能直接从图像的像素点推断是哪个数字,没有特征抽象的过程。

多层神经网络依靠隐含层,可以组合高阶特征



猜你喜欢

转载自blog.csdn.net/summermaoz/article/details/78852433