import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) def compute_accuracy(v_xs,v_ys):
global prediction
y_pre=sess.run(prediction,feed_dict={xs:v_xs,keep_prob:})
correct_prediction=tf.equal(tf.argmax(y_pre,),tf.argmax(v_ys,))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
result=sess.run(accuracy,feed_dict={xs:v_xs,ys:v_ys})
return result def weight_varirable(shape):
inital=tf.truncated_normal(shape,stddev=0.1)
return tf.Variable(inital) 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=[,,,],padding='SAME') def max_poo_(x):
return tf.nn.max_pool(x, ksize=[,,,], strides=[,,,], padding='SAME') xs=tf.placeholder(tf.float32,[None,])
ys=tf.placeholder(tf.float32,[None,])
keep_prob=tf.placeholder(tf.float32) x_image=tf.reshape(xs,[-,,,]) W_conv1=weight_varirable([,,,])
b_conv1=bias_variable([])
h_conv1=tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
h_pool1=max_poo_(h_conv1) W_conv2=weight_varirable([,,,])
b_conv2=bias_variable([])
h_conv2=tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)
h_pool2=max_poo_(h_conv2) W_fc1=weight_varirable([**,])
b_fc1=bias_variable([]) h_pool2_flat=tf.reshape(h_pool2,[-,**])
h_fcl=tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)
h_fc1_drop=tf.nn.dropout(h_fcl,keep_prob) W_fc2=weight_varirable([,])
b_fc2=bias_variable([]) prediction=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2) cross_entropy=tf.reduce_mean(
-tf.reduce_sum(ys*tf.log(prediction),
reduction_indices=[])) train_step=tf.train.AdamOptimizer(1e-).minimize(cross_entropy) sess=tf.Session() sess.run(tf.global_variables_initializer()) for i in range():
batch_xs, batch_ys = mnist.train.next_batch()
sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys,keep_prob:0.5})
if i % == :
print(compute_accuracy(
mnist.test.images, mnist.test.labels))

如果有同学没有MINST数据,请到http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_download.html下载,或者QQ问我

Tensorflow通过CNN实现MINST数据分类的更多相关文章

  1. FaceRank-人脸打分基于 TensorFlow 的 CNN 模型

    FaceRank-人脸打分基于 TensorFlow 的 CNN 模型 隐私 因为隐私问题,训练图片集并不提供,稍微可能会放一些卡通图片. 数据集 130张 128*128 张网络图片,图片名: 1- ...

  2. Tensorflow简单CNN实现

    觉得有用的话,欢迎一起讨论相互学习~Follow Me 少说废话多写代码~ """转换图像数据格式时需要将它们的颜色空间变为灰度空间,将图像尺寸修改为同一尺寸,并将标签依 ...

  3. Tensorflow的CNN教程解析

    之前的博客我们已经对RNN模型有了个粗略的了解.作为一个时序性模型,RNN的强大不需要我在这里重复了.今天,让我们来看看除了RNN外另一个特殊的,同时也是广为人知的强大的神经网络模型,即CNN模型.今 ...

  4. [DL学习笔记]从人工神经网络到卷积神经网络_3_使用tensorflow搭建CNN来分类not_MNIST数据(有一些问题)

    3:用tensorflow搭个神经网络出来 为什么用tensorflow呢,应为谷歌是亲爹啊,虽然有些人说caffe更适合图像啊mxnet效率更高等等,但爸爸就是爸爸,Android都能那么火,一个道 ...

  5. 第三节,TensorFlow 使用CNN实现手写数字识别(卷积函数tf.nn.convd介绍)

    上一节,我们已经讲解了使用全连接网络实现手写数字识别,其正确率大概能达到98%,这一节我们使用卷积神经网络来实现手写数字识别, 其准确率可以超过99%,程序主要包括以下几块内容 [1]: 导入数据,即 ...

  6. [Tensorflow] Cookbook - CNN

    Convolutional Neural Networks (CNNs) are responsible for the major breakthroughs in image recognitio ...

  7. 6 TensorFlow实现cnn识别手写数字

    ------------------------------------ 写在开头:此文参照莫烦python教程(墙裂推荐!!!) ---------------------------------- ...

  8. TensorFlow实现CNN

    TensorFlow是目前深度学习最流行的框架,很有学习的必要,下面我们就来实际动手,使用TensorFlow搭建一个简单的CNN,来对经典的mnist数据集进行数字识别. 如果对CNN还不是很熟悉的 ...

  9. tensorflow构建CNN模型时的常用接口函数

    (1)tf.nn.max_pool()函数 解释: tf.nn.max_pool(value, ksize, strides, padding, data_format='NHWC', name=No ...

随机推荐

  1. 创建文件夹、新建txt文件

    1.创建文件夹 QString myMkdir(QString path, QString floderName) //参数 path,创建的文件夹所在路径:  参数floderName,所创建的文件 ...

  2. vue-router的访问权限管理

    路由守卫(路由钩子.拦截器) vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航.有多种机会植入路由导航过程中:全局的, 单个路由独享的, 或者组件级的. 可以不登录直接进入系统 ...

  3. P1006 传纸条 /// DP+滚动数组

    题目大意: https://www.luogu.org/problemnew/show/P1006 题解 不难想到 求从起点到终点的两条不同的路 因为只能向右或向下走 所以纸条1和2不可能同时位于同一 ...

  4. C# EF去除重复列DistinctBy

    在网上看了LinQ有DistinctBy方法,实际在用的时候并没有找到,后来参照了该网站才发现写的是拓展方法 https://blog.csdn.net/c1113072394/article/det ...

  5. SpringCloud学习笔记《---03 Ribbon---》基础篇

  6. Linux开机、重启和用户登录注销(2)

    1.关机&重启命令 1.1基本介绍 shutdown shutdown -h now :表示立即关机 shutdown -h 1:     表示1分钟后关机 shutdown -r now : ...

  7. 编写Reduce处理逻辑

  8. 亲历者说:Kubernetes API 与 Operator,不为人知的开发者战争

    如果我问你,如何把一个 etcd 集群部署在 Google Cloud 或者阿里云上,你一定会不假思索的给出答案:当然是用 etcd Operator! 实际上,几乎在一夜之间,Kubernetes ...

  9. vagrant网站中box下载方法

    假设需要下载Laravel/homestead这个包. 首先定位到地址:https://app.vagrantup.com/laravel/boxes/homestead/versions/8.0.0 ...

  10. 【breathandlife】气势磅礴、比较好听的旋律有哪些?

    [breathandlife]气势磅礴.比较好听的旋律有哪些? 分享:yunbest作者:来源:2015-10-26 专题:breathandlife [breathandlife]气势磅礴.比较好听 ...