step 1:获取数据集
#!/usr/bin/env python
# encoding: utf-8
'''
@author: lele Ye
@contact: 1750112338@qq.com
@software: pycharm 2018.2
@file: 501download_mnist.py
@time: 2018/9/7 19:57
@desc:
'''
# 运行这两句会从官网上下载mnist数据集并解压到相应目录(要翻墙)
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
print("训练数据:",mnist.train.images)
print("训练数据的shape:",mnist.train.images.shape) #55000行,784列
# 找出取最后一张图片图片显示出来
import pylab
# im = mnist.train.images[54999]
# 等价于上面一句
im = mnist.train.images[mnist.train.images.shape[0]-1]
im = im.reshape(-1,28)
pylab.imshow(im)
pylab.show()
print("验证数据的shape:",mnist.validation.images.shape) #5000行,784列
print("测试数据的shape:",mnist.test.images.shape) #10000行,784列
该部分运行完成以后,会在相应的目录创建MNIST_data文件夹,并包含相应的文件,记住你的电脑能翻墙,因为read_data_sets方法调用了一个网络链接下载的地址SOURCE_URL = 'https://storage.googleapis.com/cvdf-datasets/mnist/'。
step2:定义网络锁需要的变量
#!/usr/bin/env python
# encoding: utf-8
'''
@author: lele Ye
@contact: 1750112338@qq.com
@software: pycharm 2018.2
@file: 501download_mnist.py
@time: 2018/9/7 19:57
@desc:定义loss regression网络结构
'''
# 运行这两句会从官网上下载mnist数据集并解压到相应目录(要翻墙)
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
import tensorflow as tf
tf.reset_default_graph()
# 定义占位符
x = tf.placeholder(tf.float32,[None,784])
y= tf.placeholder(tf.float32,[None,10])
# 这里将W设定为随机值,b设置为0
W = tf.Variable(tf.random_normal(([784,10])))
b = tf.Variable(tf.zeros(([10])))
pred = tf.nn.softmax(tf.matmul(x,W) + b)
# 损失函数
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),reduction_indices=1))
# 定义参数
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
step3:启动Session开始模型的训练
#!/usr/bin/env python
# encoding: utf-8
'''
@author: lele Ye
@contact: 1750112338@qq.com
@software: pycharm 2018.2
@file: 501download_mnist.py
@time: 2018/9/7 19:57
@desc:定义loss regression网络结构
'''
# 运行这两句会从官网上下载mnist数据集并解压到相应目录(要翻墙)
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
import tensorflow as tf
tf.reset_default_graph()
# 定义占位符
x = tf.placeholder(tf.float32,[None,784])
y= tf.placeholder(tf.float32,[None,10])
# 这里将W设定为随机值,b设置为0
W = tf.Variable(tf.random_normal(([784,10])))
b = tf.Variable(tf.zeros(([10])))
pred = tf.nn.softmax(tf.matmul(x,W) + b)
# 损失函数
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),reduction_indices=1))
# 定义参数
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
training_epochs = 25
batch_size = 50
display_step = 1
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# 启动循环开始训练
for epoch in range(training_epochs):
avg_cost = 0
total_batch = int(mnist.train.num_examples/batch_size)
# 循环所有的数据集
for i in range(total_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
# 运行优化器
_,c =sess.run([optimizer,cost],feed_dict={x:batch_xs,y:batch_ys})
# 计算平均loss值
avg_cost += c/total_batch
# 显示训练中的详细信息
if(epoch+1) % display_step ==0:
print("Epoch:",'%04d' % (epoch+1),"cost=","{:.9f}".format(avg_cost))
print("Finnished!")
step4:我们可以在训练过程中加入验证的准确率
#!/usr/bin/env python
# encoding: utf-8
'''
@author: lele Ye
@contact: 1750112338@qq.com
@software: pycharm 2018.2
@file: 501download_mnist.py
@time: 2018/9/7 19:57
@desc:定义loss regression网络结构训练加验证
'''
# 运行这两句会从官网上下载mnist数据集并解压到相应目录(要翻墙)
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
import tensorflow as tf
tf.reset_default_graph()
# 定义占位符
x = tf.placeholder(tf.float32,[None,784])
y= tf.placeholder(tf.float32,[None,10])
# 这里将W设定为随机值,b设置为0
W = tf.Variable(tf.random_normal(([784,10])))
b = tf.Variable(tf.zeros(([10])))
pred = tf.nn.softmax(tf.matmul(x,W) + b)
# 损失函数
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),reduction_indices=1))
# 定义参数
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
training_epochs = 25
batch_size = 50
display_step = 1
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# 启动循环开始训练
for epoch in range(training_epochs):
avg_cost = 0
total_batch = int(mnist.train.num_examples/batch_size)
# 循环所有的数据集
for i in range(total_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
# 运行优化器
_,c =sess.run([optimizer,cost],feed_dict={x:batch_xs,y:batch_ys})
# 计算平均loss值
avg_cost += c/total_batch
# 显示训练中的详细信息
if(epoch+1) % display_step ==0:
# 测试模型
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
# 计算准确率
accurancy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost),
"Validation accurency:", accurancy.eval({x: mnist.validation.images, y: mnist.validation.labels}))
print("Finnished!")
step5:为了方便下次使用模型,将模型保存下来
#!/usr/bin/env python
# encoding: utf-8
'''
@author: lele Ye
@contact: 1750112338@qq.com
@software: pycharm 2018.2
@file: 501download_mnist.py
@time: 2018/9/7 19:57
@desc:定义loss regression网络结构训练加验证
'''
# 运行这两句会从官网上下载mnist数据集并解压到相应目录(要翻墙)
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
import tensorflow as tf
tf.reset_default_graph()
# 定义占位符
x = tf.placeholder(tf.float32,[None,784])
y= tf.placeholder(tf.float32,[None,10])
# 这里将W设定为随机值,b设置为0
W = tf.Variable(tf.random_normal(([784,10])))
b = tf.Variable(tf.zeros(([10])))
pred = tf.nn.softmax(tf.matmul(x,W) + b)
# 损失函数
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),reduction_indices=1))
# 定义参数
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
training_epochs = 25
batch_size = 50
display_step = 1
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# 启动循环开始训练
for epoch in range(training_epochs):
avg_cost = 0
total_batch = int(mnist.train.num_examples/batch_size)
# 循环所有的数据集
for i in range(total_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
# 运行优化器
_,c =sess.run([optimizer,cost],feed_dict={x:batch_xs,y:batch_ys})
# 计算平均loss值
avg_cost += c/total_batch
# 显示训练中的详细信息
if(epoch+1) % display_step ==0:
# 测试模型
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
# 计算准确率
accurancy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost),
"Validation accurency:", accurancy.eval({x: mnist.validation.images, y: mnist.validation.labels}))
print("Finnished!")
# 保存模型
saver = tf.train.Saver()
model_path = "logs/mnist_softmax_model.ckpt"
save_path = saver.save(sess,model_path)
print("Model saved in file:%s" %save_path)
step6:使用我们得到的模型进行预测
#!/usr/bin/env python
# encoding: utf-8
'''
@author: lele Ye
@contact: 1750112338@qq.com
@software: pycharm 2018.2
@file: 501download_mnist.py
@time: 2018/9/7 19:57
@desc:定义loss regression网络结构训练加验证
'''
# 运行这两句会从官网上下载mnist数据集并解压到相应目录(要翻墙)
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
import tensorflow as tf
tf.reset_default_graph()
# 定义占位符
x = tf.placeholder(tf.float32,[None,784])
y= tf.placeholder(tf.float32,[None,10])
# 这里将W设定为随机值,b设置为0
W = tf.Variable(tf.random_normal(([784,10])))
b = tf.Variable(tf.zeros(([10])))
pred = tf.nn.softmax(tf.matmul(x,W) + b)
# 损失函数
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),reduction_indices=1))
# 定义参数
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
training_epochs = 25
batch_size = 50
display_step = 1
# 启动session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# 保存模型
saver = tf.train.Saver()
model_path = "logs/mnist_softmax_model.ckpt"
# 恢复模型变量
saver.restore(sess,model_path)
# 测试模型
correct_prediction = tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
# 计算准确率
accurancy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
print("Accurency:",accurancy.eval({x:mnist.test.images,y:mnist.test.labels}))
# 分别输出预测结果,每个类别的概率,以及one-hot码
output = tf.argmax(pred,1)
# 3代表一次输入三张图片
batch_xs,batch_ys = mnist.train.next_batch(3)
output_test,predv = sess.run([output,pred],feed_dict={x:batch_xs})
print(output_test,predv,batch_ys)
# 对测试图片可视化
from matplotlib import pylab
im = batch_xs[0]
im = im.reshape(-1,28)
pylab.imshow(im)
pylab.show()
im = batch_xs[1]
im = im.reshape(-1, 28)
pylab.imshow(im)
pylab.show()
im = batch_xs[2]
im = im.reshape(-1, 28)
pylab.imshow(im)
pylab.show()
观察最终结果:
step7 :接下来定义一个多层的卷积神经进行分类任务(确保你有mnsit数据集):
#!/usr/bin/env python
# encoding: utf-8
'''
@author: lele Ye
@contact: 1750112338@qq.com
@software: pycharm 2018.2
@file: 501download_mnist.py
@time: 2018/9/7 19:57
@desc:定义loss regression网络结构训练加验证
'''
# 运行这两句会从官网上下载mnist数据集并解压到相应目录(要翻墙)
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
def weight_variable(shape):
init = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(init)
def bias_variable(shape):
init = tf.constant(0.1, shape=shape)
return tf.Variable(init)
def conv2d(x, filter):
return tf.nn.conv2d(x, filter, strides=[1, 1, 1, 1], padding="SAME")
def max_pool(x):
return tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding="SAME")
# 卷积在每个5x5的patch中算出32个特征。卷积的权重张量形状是[5, 5, 1, 32],
# 前两个维度是patch的大小,接着是输入的通道数目,最后是输出的通道数目。 而对于每一个输出通道都有一个对应的偏置量。
def model(x_image, keep_prob):
# 定义一层卷积
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool(h_conv1)
# 定义第二层卷积
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool(h_conv2)
# 定义全连接层,7 * 7 * 64大小的由来是因为此时特征图的维度为 7*7大小,64个通道
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
# 定义dropout层
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob=keep_prob)
# 定义输出层
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
return y_conv
def loss_func(y_true, y_pred):
cross_entropy_loss = -tf.reduce_mean(y_true * tf.log(y_pred))
return cross_entropy_loss
def train(x, y, keep_prob):
x_image = tf.reshape(x, [-1, 28, 28, 1])
y_pred = model(x_image, keep_prob)
loss = loss_func(y, y_pred)
train_step = tf.train.AdamOptimizer(1e-5).minimize(loss)
correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(5001):
batch = mnist.train.next_batch(50)
train_step.run(feed_dict={x: batch[0], y: batch[1], keep_prob: 0.5})
if i % 100 == 0:
train_accuracy = accuracy.eval(feed_dict={
x: batch[0], y: batch[1], keep_prob: 1.0
})
test_acc = accuracy.eval(feed_dict={
x: mnist.test.images, y: mnist.test.labels, keep_prob: 1.0})
print("step %d, training accuracy %g, test accuracy %g" % (i, train_accuracy,test_acc))
if __name__ == "__main__":
mnist = input_data.read_data_sets("./MNIST_data/", one_hot=True)
tf.reset_default_graph()
# 定义占位符
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32)
train(x, y, keep_prob)