训练代码:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('data_dir', '/tmp/data/', 'Directory for storing data') print(FLAGS.data_dir)
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True) input=tf.placeholder(tf.float32,[None,784],name='input')
label=tf.placeholder(tf.float32,[None,10],name='label')
keep_prob=tf.placeholder(tf.float32,name='keep_prob') image=tf.reshape(input,[-1,28,28,1]) conv1_W=tf.Variable(tf.truncated_normal([5,5,1,32],stddev=0.1))
conv1_b=tf.Variable(tf.constant(0.1,shape=[32]))
layer1=tf.nn.elu(tf.nn.conv2d(image,conv1_W,strides=[1,1,1,1],padding='SAME')+conv1_b)
layer2=tf.nn.max_pool(layer1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME') conv2_W=tf.Variable(tf.truncated_normal([5,5,32,64],stddev=0.1))
conv2_b=tf.Variable(tf.constant(0.1,shape=[64]))
layer3=tf.nn.elu(tf.nn.conv2d(layer2,conv2_W,strides=[1,1,1,1],padding='SAME')+conv2_b)
layer4=tf.nn.max_pool(layer3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME') layer5=tf.reshape(layer4,[-1,7*7*64]) fc1_W=tf.Variable(tf.truncated_normal([7*7*64,1024],stddev=0.1))
fc1_b=tf.Variable(tf.constant(0.1,shape=[1024]))
layer5=tf.reshape(layer4,[-1,7*7*64])
layer6=tf.nn.elu(tf.matmul(layer5,fc1_W)+fc1_b) layer7=tf.nn.dropout(layer6,keep_prob) fc2_W=tf.Variable(tf.truncated_normal([1024,10],stddev=0.1))
fc2_b=tf.Variable(tf.constant(0.1,shape=[10]))
output=tf.nn.softmax(tf.matmul(layer7,fc2_W)+fc2_b,name='output') cross_entropy=tf.reduce_mean(-tf.reduce_sum(label*tf.log(output),reduction_indices=[1])) train_step=tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_predition=tf.equal(tf.argmax(output,1),tf.arg_max(label,1))
accuracy=tf.reduce_mean(tf.cast(correct_predition,tf.float32),name='accuracy') sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
batch = mnist.train.next_batch(50)
for i in range(20000):
batch = mnist.train.next_batch(50)
train_step.run(feed_dict={input: batch[0], label: batch[1], keep_prob: 0.5})
if i%100==0:
train_accuracy = accuracy.eval(feed_dict={input:batch[0], label:batch[1], keep_prob: 1.0})
print("%d:training accuracy %g"%(i,train_accuracy)) saver = tf.train.Saver()
save_path = saver.save(sess,"E:/dnn/model")

测试代码:

from __future__ import division
import numpy as np
import tensorflow as tf
from PIL import Image img = Image.open('E:/dnn/test.bmp').convert('L')
if img.size[0] != 28 or img.size[1] != 28:
img = img.resize((28, 28))
arr = []
for i in range(28):
for j in range(28):
pixel = 1.0 - float(img.getpixel((j, i)))/255.0
arr.append(pixel)
image = np.array(arr).reshape((1, 28, 28, 1)) saver = tf.train.import_meta_graph('E:/dnn/model.meta')
graph = tf.get_default_graph()
input=graph.get_tensor_by_name('input:0')
label=graph.get_tensor_by_name('label:0')
output=graph.get_tensor_by_name('output:0')
keep_prob=graph.get_tensor_by_name('keep_prob:0')
accuracy=graph.get_tensor_by_name('accuracy:0') with tf.Session() as sess:
saver.restore(sess, tf.train.latest_checkpoint('E:/dnn'))
test = sess.run(output, feed_dict={input: image.reshape(-1,784), label: np.full(10,1e-10).reshape(-1,10), keep_prob: 1.0})
print(test)
ans=0
for i in range(10):
if (test[0][i]>test[0][ans]):
ans=i
print(ans)

测试结果:

TensorFlow-mnist的更多相关文章

  1. TensorFlow MNIST(手写识别 softmax)实例运行

    TensorFlow MNIST(手写识别 softmax)实例运行 首先要有编译环境,并且已经正确的编译安装,关于环境配置参考:http://www.cnblogs.com/dyufei/p/802 ...

  2. 学习笔记TF056:TensorFlow MNIST,数据集、分类、可视化

    MNIST(Mixed National Institute of Standards and Technology)http://yann.lecun.com/exdb/mnist/ ,入门级计算机 ...

  3. TensorFlow MNIST 问题解决

    TensorFlow MNIST 问题解决 一.数据集下载错误 错误:IOError: [Errno socket error] [Errno 101] Network is unreachable ...

  4. Mac tensorflow mnist实例

    Mac tensorflow mnist实例 前期主要需要安装好tensorflow的环境,Mac 如果只涉及到CPU的版本,推荐使用pip3,傻瓜式安装,一行命令!代码使用python3. 在此附上 ...

  5. tensorflow MNIST Convolutional Neural Network

    tensorflow MNIST Convolutional Neural Network MNIST CNN 包含的几个部分: Weight Initialization Convolution a ...

  6. tensorflow MNIST新手教程

    官方教程代码如下: import gzip import os import tempfile import numpy from six.moves import urllib from six.m ...

  7. TensorFlow MNIST初级学习

    MNIST MNIST 是一个入门级计算机视觉数据集,包含了很多手写数字图片,如图所示: 数据集中包含了图片和对应的标注,在 TensorFlow 中提供了这个数据集,我们可以用如下方法进行导入: f ...

  8. 学习笔记TF057:TensorFlow MNIST,卷积神经网络、循环神经网络、无监督学习

    MNIST 卷积神经网络.https://github.com/nlintz/TensorFlow-Tutorials/blob/master/05_convolutional_net.py .Ten ...

  9. AI tensorflow MNIST

    MNIST 数据 train-images-idx3-ubyte.gz:训练集图片 train-labels-idx1-ubyte.gz:训练集图片类别 t10k-images-idx3-ubyte. ...

  10. tensorflow——MNIST机器学习入门

    将这里的代码在项目中执行下载并安装数据集. 执行下面代码,训练.并评估模型: # _*_coding:utf-8_*_ import inputdata mnist = inputdata.read_ ...

随机推荐

  1. " \t\r\n\f"是什么意思

    空格字符 \t 制表符 \r 回车符 \n 换行符 \f 换页符

  2. 【转载】Java IO 转换流 字节转字符流

    字节流输入字节流:---------| InputStream 所有输入字节流的基类. 抽象类.------------| FileInputStream 读取文件的输入字节流.----------- ...

  3. PAT_A1140#Look-and-say Sequence

    Source: PAT A1140 Look-and-say Sequence (20 分) Description: Look-and-say sequence is a sequence of i ...

  4. eas之kdtable分组

    如何指定是否要进行数据分组以及对哪些列进行分组 // 指定KDTable要进行数据分组 table.getGroupManager().setGroup(true); // 指明要对0.1.2三列进行 ...

  5. [置顶] 我的 Java 后端书架 (2016 年暖冬版)

    转自:  http://calvin1978.blogcn.com/articles/bookshelf16.html 我的 Java 后端书架 (2016 年暖冬版) 本书架主要针对 Java 后端 ...

  6. SpringBoot快速创建HelloWorld项目

    废话不多提,拿起键盘,打开 IDEA 就是一通骚操作. 打开 IDEA 后,首页选择 Create New Project,再接着按下图所示,快速搭建SpringBoot项目. 接下来将 Group ...

  7. QT5.4.1在ARM开发板上不能显示汉字

    在linux下正常的程序,移植到ARM上,中文不能显示.网上好多介绍,一头雾水.查看其中话题是关于中文显示的(http://www.qtcn.org/bbs/simple/?t55852.html). ...

  8. Summary of Memory Management Methods

    Summary of Memory Management Methods Table 18-1 summarizes the various memory management methods. If ...

  9. 第一个关于selenium项目

    1.创建一个简单的Python工程 在主菜单中,选择File | New Project ,并指定Python解释器版本 2.创建python类,快捷键alt+insert 3.编写打开浏览器的代码, ...

  10. SecureCRT图形界面(通过设置调用Xmanager - Passive程序)

    首先,在server进行设置 假设server是图形化界面启动的,xhost +命令能够不用运行 [root@test ~]# xhost + xhost:  unable to open displ ...