使用tensorflow和mnist识别自己手写的数字

WhyWin WhyWin     2022-10-06     328

关键词:

#!/usr/bin/env python3
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(‘MNIST_data‘, one_hot=True)


import tensorflow as tf

sess = tf.InteractiveSession()


x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))


sess.run(tf.global_variables_initializer())

y = tf.matmul(x,W) + b

cross_entropy = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

for _ in range(1000):
  batch = mnist.train.next_batch(100)
  train_step.run(feed_dict={x: batch[0], y_: batch[1]})
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(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

def weight_variable(shape):
  initial = tf.truncated_normal(shape, stddev=0.1)
  return tf.Variable(initial)

def bias_variable(shape):
  initial = tf.constant(0.1, shape=shape)
  return tf.Variable(initial)

def conv2d(x, W):
  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding=‘SAME‘)

def max_pool_2x2(x):
  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1], padding=‘SAME‘)

W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1,28,28,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(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_2x2(h_conv2)

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)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])

y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
cross_entropy = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

saver = tf.train.Saver()  # defaults to saving all variables

sess.run(tf.global_variables_initializer())
for i in range(20000):
  batch = mnist.train.next_batch(50)
  if i%100 == 0:
    train_accuracy = accuracy.eval(feed_dict={
        x:batch[0], y_: batch[1], keep_prob: 1.0})
    print("step %d, training accuracy %g"%(i, train_accuracy))

  train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
saver.save(sess, ‘fanlinglong/model.ckpt‘)

print("test accuracy %g"%accuracy.eval(feed_dict={
    x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

 

基于tensorflow的mnist手写识别

这个例子,是学习tensorflow的人员通常会用到的,也是基本的学习曲线中的一环。我也是! 这个例子很简单,这里,就是简单的说下,不同的tensorflow版本,相关的接口函数,可能会有不一样哟。在TensorFlow的中文介绍文档中的... 查看详情

tensorflow中使用cnn实现mnist手写体识别

  本文参考YannLeCun的LeNet5经典架构,稍加ps得到下面适用于本手写识别的cnn结构,构造一个两层卷积神经网络,神经网络的结构如下图所示:  输入-卷积-pooling-卷积-pooling-全连接层-Dropout-Softmax输出    第一层卷积利用5*... 查看详情

tensorflow笔记之mnist手写识别系列一

tensorflow笔记(四)之MNIST手写识别系列一版权声明:本文为博主原创文章,转载请指明转载地址http://www.cnblogs.com/fydeblog/p/7436310.html前言这篇博客将利用神经网络去训练MNIST数据集,通过学习到的模型去分类手写数字。我会将本篇... 查看详情

tensorflow学习笔记mnist手写数字识别(代码片段)

...模型,以达到稳定的高性能,而是学会如何使用Tensorflow解 查看详情

mnist手写数字识别tensorflow(代码片段)

Mnist手写数字识别Tensorflow任务目标了解mnist数据集搭建和测试模型编辑环境操作系统:Win10python版本:3.6集成开发环境:pycharmtensorflow版本:1.*了解mnist数据集mnist数据集:mnist数据集下载地址??MNIST数据集来自美国国家标准与技术研究所,... 查看详情

tensorflow之手写数字识别mnist

官方文档: MNISTForMLBeginners- https://www.tensorflow.org/get_started/mnist/beginners DeepMNISTforExperts- https://www.tensorflow.org/get_started/mnist/pros 版本: TensorFlow1.2 查看详情

tensorflow基础学习五:mnist手写数字识别

MNIST数据集介绍:fromtensorflow.examples.tutorials.mnistimportinput_data#载入MNIST数据集,如果指定地址下没有已经下载好的数据,tensorflow会自动下载数据mnist=input_data.read_data_sets(‘.‘,one_hot=True)#打印Trainingdatasize:55000。print("Train 查看详情

tensorflow入门实战|第1周:实现mnist手写数字识别(代码片段)

...语言环境:Python3.6.5编译器:jupyternotebook深度学习环境:TensorFlow2文章目录一、前期工作1.设置GPU(如果使用的是CPU可以忽略这步)2.导入数据3.归一化4.可视化图片5.调整图片格式二、构建CNN网络模型三、编译模型四、训练模型五... 查看详情

tensorflow与flask结合打造手写体数字识别(代码片段)

TensorFlow与Flask结合打造手写体数字识别主要步骤:获取mnist数据集分别创建regression和convolution的模型,设置对应的计算方式、参数等信息创建regression、convolution获取数据,调用对应模型进行训练、测试最后保存对应模型创建mnist... 查看详情

tensorflow入门实战|第1周:实现mnist手写数字识别(代码片段)

...语言环境:Python3.6.5编译器:jupyternotebook深度学习环境:TensorFlow2文章目录一、前期工作1.设置GPU(如果使用的是CPU可以忽略这步)2.导入数据3.归一化4.可视化图片5.调整图片格式二、构建CNN网络模型三、编译模型四、训练模型五... 查看详情

android+tensorflow+cnn+mnist手写数字识别实现

SyncHere  查看详情

android+tensorflow+cnn+mnist手写数字识别实现

SyncHere  查看详情

tensorflow1.x代码实战系列:mnist手写数字识别(代码片段)

相关资料https://github.com/aymericdamien/TensorFlow-Examples实战1:MNSIT手写数字识别这里使用的是普通的DNN模型实现MNIST识别,如果想提高精度,可以自行使用CNN模型。'''AlogisticregressionlearningalgorithmexampleusingTensorFlowlibra... 查看详情

tensorflow之mnist手写数字识别:分类问题(代码片段)

整体代码:#数据读取importtensorflowastfimportmatplotlib.pyplotaspltimportnumpyasnpfromtensorflow.examples.tutorials.mnistimportinput_datamnist=input_data.read_data_sets("MNIST_data/",one_hot=True)#定义待输入数据的占位符# 查看详情

ai常用框架和工具丨11.基于tensorflow(keras)+flask部署mnist手写数字识别至本地web

代码实例,基于TensorFlow+Flask部署MNIST手写数字识别至本地web,希望对您有所帮助。文章目录环境说明文件结构模型训练本地web创建实现效果环境说明操作系统:Windows10CUDA版本为:10.0cudnn版本为:7.6.5Python版本为:Python3.6.13tensorflow-gp... 查看详情

keras实现mnist数据集手写数字识别(代码片段)

一.Tensorflow环境的安装这里我们只讲CPU版本,使用Anaconda进行安装a.首先我们要安装Anaconda链接:https://pan.baidu.com/s/1AxdGi93oN9kXCLdyxOMnRA密码:79ig过程如下:第一步:点击next第二步:IAgree第三步:JustME第四步:自己选择一个恰当位置... 查看详情

基于tensorflow2.x实现bp神经网络,实践mnist手写数字识别(代码片段)

...,在很多资料中都会被用作深度学习的入门样例。在Tensorflow2.x中该数据集已被封装在了tf.keras.datasets工具包下,如果没有指定数据集的位置,并先前也没有使用过,会自动联网下载该,使该数据集使用起来更... 查看详情

基于tensorflow2.x实现bp神经网络,实践mnist手写数字识别(代码片段)

...,在很多资料中都会被用作深度学习的入门样例。在Tensorflow2.x中该数据集已被封装在了tf.keras.datasets工具包下,如果没有指定数据集的位置,并先前也没有使用过,会自动联网下载该,使该数据集使用起来更... 查看详情