Python tensorflow实现mnist手写数字识别示例【非卷积与卷积实现】

 更新时间:2019年12月19日 08:40:16   作者:nudt_qxx  
这篇文章主要介绍了Python tensorflow实现mnist手写数字识别,结合实例形式分析了基于tensorflow模块使用非卷积与卷积算法实现手写数字识别的具体操作技巧,需要的朋友可以参考下

本文实例讲述了Python tensorflow实现mnist手写数字识别。分享给大家供大家参考,具体如下:

非卷积实现

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
data_path = 'F:\CNN\data\mnist'
mnist_data = input_data.read_data_sets(data_path,one_hot=True) #offline dataset
x_data = tf.placeholder("float32", [None, 784]) # None means we can import any number of images
weight = tf.Variable(tf.ones([784,10]))
bias = tf.Variable(tf.ones([10]))
Y_model = tf.nn.softmax(tf.matmul(x_data ,weight) + bias)
#Y_model = tf.nn.sigmoid(tf.matmul(x_data ,weight) + bias)
'''
weight1 = tf.Variable(tf.ones([784,256]))
bias1 = tf.Variable(tf.ones([256]))
Y_model1 = tf.nn.softmax(tf.matmul(x_data ,weight1) + bias1)
weight1 = tf.Variable(tf.ones([256,10]))
bias1 = tf.Variable(tf.ones([10]))
Y_model = tf.nn.softmax(tf.matmul(Y_model1 ,weight1) + bias1)
'''
y_data = tf.placeholder("float32", [None, 10])
loss = tf.reduce_sum(tf.pow((y_data - Y_model), 2 ))#92%-93%
#loss = tf.reduce_sum(tf.square(y_data - Y_model)) #90%-91%
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(100000):
  batch_xs, batch_ys = mnist_data.train.next_batch(50)
  sess.run(train, feed_dict = {x_data: batch_xs, y_data: batch_ys})
  if i%50==0:
    correct_predict = tf.equal(tf.arg_max(Y_model,1),tf.argmax(y_data,1))
    accurate = tf.reduce_mean(tf.cast(correct_predict,"float"))
    print(sess.run(accurate,feed_dict={x_data:mnist_data.test.images,y_data:mnist_data.test.labels}))

卷积实现

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
data_path = 'F:\CNN\data\mnist'
mnist_data = input_data.read_data_sets(data_path,one_hot=True) #offline dataset
x_data = tf.placeholder("float32", [None, 784]) # None means we can import any number of images
x_image = tf.reshape(x_data, [-1,28,28,1])
w_conv = tf.Variable(tf.ones([5,5,1,32])) #weight
b_conv = tf.Variable(tf.ones([32]))    #bias
h_conv = tf.nn.relu(tf.nn.conv2d(x_image , w_conv,strides=[1,1,1,1],padding='SAME')+ b_conv)
h_pool = tf.nn.max_pool(h_conv,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
w_fc = tf.Variable(tf.ones([14*14*32,1024]))
b_fc = tf.Variable(tf.ones([1024]))
h_pool_flat = tf.reshape(h_pool,[-1,14*14*32])
h_fc = tf.nn.relu(tf.matmul(h_pool_flat,w_fc) +b_fc)
W_fc = w_fc = tf.Variable(tf.ones([1024,10]))
B_fc = tf.Variable(tf.ones([10]))
Y_model = tf.nn.softmax(tf.matmul(h_fc,W_fc) +B_fc)
y_data = tf.placeholder("float32",[None,10])
loss = -tf.reduce_sum(y_data * tf.log(Y_model))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(1000):
  batch_xs,batch_ys =mnist_data.train.next_batch(5)
  sess.run(train_step,feed_dict={x_data:batch_xs,y_data:batch_ys})
  if i%50==0:
    correct_prediction = tf.equal(tf.argmax(Y_model,1),tf.argmax(y_data,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
    print(sess.run(accuracy,feed_dict={x_data:mnist_data.test.images,y_data:mnist_data.test.labels}))

更多关于Python相关内容可查看本站专题:《Python数学运算技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

  • numpy.insert用法及内插插0的方法

    numpy.insert用法及内插插0的方法

    本文主要介绍了numpy.insert用法及内插插0的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • Python判断List中是否包含某个元素

    Python判断List中是否包含某个元素

    在Python中,判断一个列表(List)是否包含某个特定元素是常见的任务之一,本文就来介绍一下多种判断List成员包含性的方法,并提供丰富的示例代码,以帮助大家更好地理解和运用这些技术
    2023-12-12
  • 浅析Python 引号、注释、字符串

    浅析Python 引号、注释、字符串

    这篇文章主要介绍了Python 引号、注释、字符串的相关知识,文中给大家提到了python中一对单引号,一对双引号,三个单双引号的区别和用法,需要的朋友可以参考下
    2019-07-07
  • Python利用matplotlib画出漂亮的分析图表

    Python利用matplotlib画出漂亮的分析图表

    这篇文章主要介绍了Python利用matplotlib画出漂亮的分析图表,文章首先引入数据集展开详情,需要的朋友可以参考一下
    2022-07-07
  • Python 文件数据读写的具体实现

    Python 文件数据读写的具体实现

    这篇文章主要介绍了Python 文件数据读写的具体实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • python跳过第一行快速读取文件内容的实例

    python跳过第一行快速读取文件内容的实例

    今天小编就为大家分享一篇python跳过第一行快速读取文件内容的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Flask  使用Gunicorn部署服务介绍

    Flask  使用Gunicorn部署服务介绍

    这篇文章主要分享了 Flask  使用Gunicorn部署服务介绍,Flask 虽然自带 Web 服务器,但是该服务器性能较低,是单进程单线程模型,原本是供开发测试使用。所以我们在生产环境中需要使用 Gunicorn 这样高性能服务器部署Flask服务,想了解更多内容,请参考下面详细内容
    2021-11-11
  • 解析python高级异常和运算符重载

    解析python高级异常和运算符重载

    异常是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行。 运算符重载,意味着在某个类的方法中拦截内置的操作,当类的实例出现在内置操作中,Python会调用你的方法,并且方法的返回值会作为相应操作的结果。本文主要介绍了python的高级异常和运算符重载
    2021-06-06
  • Python基于xlutils修改表格内容过程解析

    Python基于xlutils修改表格内容过程解析

    这篇文章主要介绍了Python基于xlutils修改表格内容过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • 利用Python将图片中扭曲矩形的复原

    利用Python将图片中扭曲矩形的复原

    这篇文章主要给大家介绍了关于如何利用Python将图片中扭曲矩形复原的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09

最新评论