#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2018/4/28 11:22
@Author : Junya Lu
@Site : 多层感知机识别数字
"""
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
sess = tf.InteractiveSession()
# 1. 定义算法公式: 神经网络forward时的计算
# 给隐含层的参数设置Variable 并进行初始化
in_units = 784 # 输入节点数
h1_units = 300 # 隐层节点数
w1 = tf.Variable(tf.truncated_normal([in_units, h1_units], stddev=0.1)) #初始化为截断的正态分布,标准差为0.1
b1 = tf.Variable(tf.zeros([h1_units]))
w2 = tf.Variable(tf.zeros([h1_units,10]))
b2 = tf.Variable(tf.zeros([10]))
x = tf.placeholder(tf.float32, [None,in_units])
keep_prob = tf.placeholder(tf.float32)
#实现一个激活函数为ReLU的隐含层 y = relu(wx+b)
hidden1 = tf.nn.relu(tf.matmul(x, w1) + b1)
#实现Dropout功能 即随机将一部分结点置为0
hidden1_dropput = tf.nn.dropout(hidden1, keep_prob) # keep_prob为保留数据不置为0的比例,训练时候应该小于1 防止过拟合,预测时候等于1
#输出层
y = tf.nn.softmax(tf.matmul(hidden1_dropput, w2) + b2)
# 2. 定义损失函数和自适应优化器Adagrad 学习率设置为0.3
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)
# 3. 训练步骤
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})
# 4.对模型的准确率进行评估
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # tf.cast 将数据由bool转为float32形式
print (accuracy.eval({x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
实现Softmax Regression识别手写数字
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2018/4/28 14:40
@Author : Junya Lu
@Site :
"""
from tensorflow.examples.tutorials.mnist import input_data # 数字识别
import tensorflow as tf
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)
print (mnist.train.images.shape, mnist.train.labels.shape)
print (mnist.test.images.shape, mnist.test.labels.shape)
print (mnist.validation.images.shape, mnist.validation.labels.shape)
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, [None,784])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W) + b) #定义算法公式# 定义loss , 并选定优化器:随机梯度下降SGD
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.GradientDescentOptimizer(0.5).minimize(cross_entropy)
tf.global_variables_initializer().run() #TensorFlow全局参数初始化器
for i in range(1000): # 迭代执行训练操作train_step
batch_xs, batch_ys = mnist.train.next_batch(100)
train_step.run({x: batch_xs, y_:batch_ys})# 对模型的准确率进行验证 tf.argmax(y, 1) 从tensor中寻找最大值的序号。判断预测类别和真实类别是否一致
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}))
eval() 其实就是tf.Tensor的Session.run() 的另外一种写法。只能用于tf.Tensor类对象,也就是有输出的Operation。对于没有输出的Operation, 可以用.run()或者Session.run()。Session.run()没有这个限制。
tf.argmax(y, 1)从tensor中寻找最大值的序号。判断预测类别和真实类别是否一致
tf.cast(x, dtype, name=None) 将x的数据格式转化成dtype.例如,原来x的数据格式是bool, 那么将其转化成float以后,就能够将其转化成0和1的序列。反之也可以。