import sys,os
sys.path.append(os.pardir)
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
from PIL import Image
import tensorflow as tf def predict():
meta_path = 'ckpt/mnist.ckpt.meta'
model_path = 'ckpt/mnist.ckpt'
sess = tf.InteractiveSession ()
saver = tf.train.import_meta_graph (meta_path)
saver.restore (sess, model_path)
graph = tf.get_default_graph ()
W = graph.get_tensor_by_name ("w:0")
b = graph.get_tensor_by_name ("b:0")
x = tf.placeholder (tf.float32, [None, 784])
y = tf.nn.softmax (tf.matmul (x, W) + b)
keep_prob = tf.placeholder (tf.float32)
batch_xs, batch_ys=mnist.train.next_batch (100)
one_img = batch_xs[0].reshape ((1, 784))
one_num = batch_ys[0].reshape ((1, 10))
temp = sess.run (y, feed_dict={x: one_img, keep_prob: 1.0})
b = sess.run (tf.argmax (temp, 1))
a = sess.run (tf.arg_max (one_num, 1))
print(temp)
print(one_num)
if b == a:
print ("success! the num is :", (b[0]))
showImgTest(one_img)
else:
print ("mistakes predict.") def trainNet():
x = tf.placeholder (tf.float32, [None, 784])
W = tf.Variable (tf.zeros ([784, 10]),name="w")
b = tf.Variable (tf.zeros ([10]),name="b")
y = tf.nn.softmax (tf.matmul (x, W) + b)
y_ = tf.placeholder (tf.float32, [None, 10])
keep_prob = tf.placeholder (tf.float32)
# 定义测试的准确率
correct_prediction = tf.equal (tf.argmax (y, 1), tf.argmax (y_, 1))
accuracy = tf.reduce_mean (tf.cast (correct_prediction, tf.float32))
#
saver = tf.train.Saver (max_to_keep=1)
max_acc = 0
train_accuracy = 0
#交叉熵
cross_entropy = tf.reduce_mean (-tf.reduce_sum (y_ * tf.log (y)))
# cross_error=cross_entropy_error_batch(y,y_)
train_step = tf.train.GradientDescentOptimizer (0.01).minimize (cross_entropy)
sess = tf.InteractiveSession ()
tf.global_variables_initializer ().run ()
for i in range (1000):
batch_xs, batch_ys = mnist.train.next_batch (100)
sess.run (train_step, feed_dict={x: batch_xs, y_: batch_ys, keep_prob: 1.0})
if i % 100 == 0:
train_accuracy = accuracy.eval (feed_dict={x: batch_xs, y_: batch_ys, keep_prob: 1.0})
print ("step %d, training accuracy %g" % (i, train_accuracy))
if train_accuracy > max_acc:
max_acc = train_accuracy
saver.save (sess, 'ckpt/mnist.ckpt') if __name__ == '__main__':
mnist = input_data.read_data_sets ("MNIST_data/", one_hot=True)
choice=0
while choice == 0:
print ("------------------------tensorflow--------------------------")
print ("\t\t\t1\ttrain model..")
print("\t\t\t2\tpredict model")
print("\t\t\t3\tshow the first image")
print ("\t\t\t0\texit")
choice = input ("please input your choice!")
print(choice)
if choice == "1":
print("start train...")
trainNet()
if choice=="2":
predict()
if choice=="3":
showImg()

注:正在学习CNN,选项4还没有来的及做。后面补上

使用tensorflow进行mnist数字识别【模型训练+预测+模型保存+模型恢复】的更多相关文章

  1. TensorFlow学习笔记(三)MNIST数字识别问题

    一.MNSIT数据处理 MNSIT是一个非常有名的手写体数字识别数据集.包含60000张训练图片,10000张测试图片.每张图片是28X28的数字. TonserFlow提供了一个类来处理 MNSIT ...

  2. 一个简单的TensorFlow可视化MNIST数据集识别程序

    下面是TensorFlow可视化MNIST数据集识别程序,可视化内容是,TensorFlow计算图,表(loss, 直方图, 标准差(stddev)) # -*- coding: utf-8 -*- ...

  3. MNIST数字识别问题

    摘自<Tensorflow:实战Google深度学习框架> import tensorflow as tf from tensorflow.examples.tutorials.mnist ...

  4. Pytorch CNN网络MNIST数字识别 [超详细记录] 学习笔记(三)

    目录 1. 准备数据集 1.1 MNIST数据集获取: 1.2 程序部分 2. 设计网络结构 2.1 网络设计 2.2 程序部分 3. 迭代训练 4. 测试集预测部分 5. 全部代码 1. 准备数据集 ...

  5. Tensorflow手写数字识别训练(梯度下降法)

    # coding: utf-8 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data #p ...

  6. TensorFlow深度学习实战---MNIST数字识别问题

    1.滑动平均模型: 用途:用于控制变量的更新幅度,使得模型在训练初期参数更新较快,在接近最优值处参数更新较慢,幅度较小 方式:主要通过不断更新衰减率来控制变量的更新幅度. 衰减率计算公式 : deca ...

  7. 吴裕雄 python 神经网络——TensorFlow实现回归模型训练预测MNIST手写数据集

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...

  8. Tensorflow手写数字识别---MNIST

    MNIST数据集:包含数字0-9的灰度图, 图片size为28x28.训练样本:55000,测试样本:10000,验证集:5000

  9. Tensorflow手写数字识别(交叉熵)练习

    # coding: utf-8import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data #pr ...

随机推荐

  1. 【译文】InnoDB 的不同的SQL如何加锁

    http://dev.mysql.com/doc/refman/5.6/en/innodb-locks-set.html 前置:检索如果用不到索引,会扫描全表,并根据策略加锁.所以,这就是我们合理建立 ...

  2. Django之Model (ORM)

    传统操作数据库 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层 ...

  3. KVOController原理解析

    1.使用类似动态代理的模式和消息派发中枢模式实现整个架构: 2.使用NSMapTable和NSHashTable进行切面信息的增删查维护:主要用于去重和查看是否存在. 实现方式 消息流 KVOCont ...

  4. using指令都用了这么多年了,其实还真没懂!

    在C语言中,我们经常使用#include<stdio.h>指令来导入标准输入输出库,这确实很好理解,相当于把代码复制到当前的程序中. 但在C#语言中,当我们写Console程序时,经常在第 ...

  5. 打造自己的移动绿色版 Python 环境

    本文只适用 Windows 系统,Linux,Mac 或其他系统可以直接自己写个脚本安装就好了,甚至大部分系统自带. 相信某些人写好了Python程序结果给朋友运行又得装个Python环境,难免失去了 ...

  6. 【转】使用URL SCHEME启动天猫客户端并跳转到某个商品页面的方法

    在项目中遇到了这样一个需求:让用户在手机应用中,点击一个天猫的商品链接(知道商品在PC浏览器里的地址),直接启动天猫的客户端并显示这个商品.以前曾经实现过类似的功能,不过那次是淘宝的商品,天猫和淘宝的 ...

  7. javascript:apply方法 以及和call的区别 (转载)

    javascript:apply方法   1.        apply和call的区别在哪里 2.        什么情况下用apply,什么情况下用call 3.        apply的其他巧 ...

  8. Ubuntu下查看自己的GPU型号

    1.在命令行中输入:lspci 即可看到当前显卡型号. 2.Ubuntu 14.04 安装 Nvidia 私有驱动 sudo apt-get install nvidia-331 3.进行双显卡切换n ...

  9. Spring4自动装配(default-autowire)

    §1 什么是自动装配? Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系.因此,如果可能的话,可以自动让Spring通过检查BeanFactory中的内容,来替我 ...

  10. MySQL数据库启停

    坑1:net start mysql 提示:服务名无效 1.win+R打开运行窗口,输入services.msc 2.在其中查看mysql的服务名,我的是MySQL55 3.以管理员身份打开cmd,输 ...