6神经网络手写数字分类mnist

#MNIST数据集的标签是介于0-9的数字,我们要把标签转化为“one-hot vectors”。
#一个one-hot向量除了某一位数字是1以外,其余维度数字都是0,比如标签O将表示为([1,0,0,0,0,0,0.0,0.0]),标签3将表示为([O,0,0,1.0,0,0,0,0,0])。
#因此,mnist.train.labels是一个[60000,10]的数字矩阵,60000张图片10个标签。

#softmax函数,通常放在分类的最后一层,就是将分类结果通过softmax转化为概率

#为提升性能可修改之处:
#1.可修改batch_size=100
#2.增加隐藏层,隐藏层之间的激活函数使用sigmod、ReLU等函数
#3.修改权值(w)和偏置值(b)的初始值(当前全为0)
#4.loss函数可考虑使用交叉熵,当前使用二次代价函数
#5.修改学习率,或使用其他优化方式,当前使用梯度下降
#6.修改训练次数epoch

#一个简单示例:
#输入:784个像素点即784个神经元,输出10中分类即10个神经元。(也可加入隐藏层)

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
 
#载入数据集
mnist=input_data.read_data_sets("MNIST_data", one_hot=True)
 
#每个批次的大小,每次放入100张图片,//为整除符号
batch_size=100#1.可修改
#计算一共有多少批次
n_batch=mnist.train.num_examples // batch_size
 
#定义两个placeholder
x=tf.placeholder(tf.float32,[None,784])
y=tf.placeholder(tf.float32,[None,10])
 
#创建一个简单的神经网络,一个输入层,一个输出层
#2.增加隐藏层,隐藏层之间的激活函数使用sigmod、ReLU等函数
W=tf.Variable(tf.zeros([784,10]))#权值#3.修改初始值(当前全为0)
b=tf.Variable(tf.zeros([1,10]))#偏置值
prediction=tf.nn.softmax(tf.matmul(x,W)+b)#预测值使用softmax函数最终转化为概率值
 
#二次代价函数
loss=tf.reduce_mean(tf.square(y-prediction))#4.可考虑使用交叉熵

#使用剃度下降法,学习率0.2,最小化loss
train_step=tf.train.GradientDescentOptimizer(0.2).minimize(loss)#5.修改学习率,或使用其他优化方式
 
#初始化变量
init=tf.global_variables_initializer()
 
#结果存放在一个布尔型列表中
#tf.equal比较(tf.argmax(y,1), tf.argmax(prediction,1))两个参数的大小,相同返回true不同返回false
correct_prediction=tf.equal(tf.argmax(y,1), tf.argmax(prediction,1)) #argmax返回一维张量中最大的值所在的位置
#求准确率
#tf.cast(correct_prediction,tf.float32)将bool类型转化为float32类型(true转化为1.0,false转化为0.0)
#tf.reduce_mean求平均值
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

with tf.Session() as sess:
    sess.run(init)#初始化变量
    #训练,for epoch in range(20)迭代20个周期,把所以图片训练21次
    for epoch in range(20):#6.修改训练次数
        #for batch in range(n_batch)循环n个batch,把所有图片训练一次
        for batch in range(n_batch):
            batch_xs,batch_ys=mnist.train.next_batch(batch_size)#获取一个batch即100张图片,图片保存于batch_xs,图片标签保存于batch_ys
            sess.run(train_step,feed_dict={x:batch_xs, y:batch_ys})#执行训练
 
        acc=sess.run(accuracy,feed_dict={x:mnist.test.images, y:mnist.test.labels})#一个周期后观察准确率,传入测试集图片和标签
        print("Iter"+str(epoch)+",Testing Accuracy "+str(acc))



WARNING:tensorflow:From <ipython-input-1-46c9acb31136>:14: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From C:\Users\CMM\Anaconda3\envs\Multi-Channelpytorch1py36\lib\site-packages\tensorflow_core\contrib\learn\python\learn\datasets\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
WARNING:tensorflow:From C:\Users\CMM\Anaconda3\envs\Multi-Channelpytorch1py36\lib\site-packages\tensorflow_core\contrib\learn\python\learn\datasets\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting MNIST_data\train-images-idx3-ubyte.gz
WARNING:tensorflow:From C:\Users\CMM\Anaconda3\envs\Multi-Channelpytorch1py36\lib\site-packages\tensorflow_core\contrib\learn\python\learn\datasets\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting MNIST_data\train-labels-idx1-ubyte.gz
WARNING:tensorflow:From C:\Users\CMM\Anaconda3\envs\Multi-Channelpytorch1py36\lib\site-packages\tensorflow_core\contrib\learn\python\learn\datasets\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
WARNING:tensorflow:From C:\Users\CMM\Anaconda3\envs\Multi-Channelpytorch1py36\lib\site-packages\tensorflow_core\contrib\learn\python\learn\datasets\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
Iter0,Testing Accuracy 0.8309
Iter1,Testing Accuracy 0.8712
Iter2,Testing Accuracy 0.8814
Iter3,Testing Accuracy 0.8882
Iter4,Testing Accuracy 0.8936
Iter5,Testing Accuracy 0.8976
Iter6,Testing Accuracy 0.9
Iter7,Testing Accuracy 0.9022
Iter8,Testing Accuracy 0.9039
Iter9,Testing Accuracy 0.9054
Iter10,Testing Accuracy 0.9062
Iter11,Testing Accuracy 0.907
Iter12,Testing Accuracy 0.9085
Iter13,Testing Accuracy 0.9095
Iter14,Testing Accuracy 0.9095
Iter15,Testing Accuracy 0.9106
Iter16,Testing Accuracy 0.9117
Iter17,Testing Accuracy 0.9123
Iter18,Testing Accuracy 0.9132
Iter19,Testing Accuracy 0.9134
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值