TensorFlow从入门到理解(三):你的第一个卷积神经网络(CNN)
运行代码:
- from __future__ import print_function
- import tensorflow as tf
- from tensorflow.examples.tutorials.mnist import input_data
- # number 1 to 10 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: 1})
- correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))
- accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
- result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 1})
- return result
- 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):
- # stride [1, x_movement, y_movement, 1]
- # Must have strides[0] = strides[3] = 1
- return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
- def max_pool_2x2(x):
- # stride [1, x_movement, y_movement, 1]
- return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
- # define placeholder for inputs to network
- xs = tf.placeholder(tf.float32, [None, 784])/255. # 28x28
- ys = tf.placeholder(tf.float32, [None, 10])
- keep_prob = tf.placeholder(tf.float32)
- x_image = tf.reshape(xs, [-1, 28, 28, 1])
- # print(x_image.shape) # [n_samples, 28,28,1]
- ## conv1 layer ##
- W_conv1 = weight_variable([5,5, 1,32]) # patch 5x5, in size 1, out size 32
- b_conv1 = bias_variable([32])
- h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # output size 28x28x32
- h_pool1 = max_pool_2x2(h_conv1) # output size 14x14x32
- ## conv2 layer ##
- W_conv2 = weight_variable([5,5, 32, 64]) # patch 5x5, in size 32, out size 64
- b_conv2 = bias_variable([64])
- h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) # output size 14x14x64
- h_pool2 = max_pool_2x2(h_conv2) # output size 7x7x64
- ## fc1 layer ##
- W_fc1 = weight_variable([7*7*64, 1024])
- b_fc1 = bias_variable([1024])
- # [n_samples, 7, 7, 64] ->> [n_samples, 7*7*64]
- 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)
- h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
- ## fc2 layer ##
- W_fc2 = weight_variable([1024, 10])
- b_fc2 = bias_variable([10])
- prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
- # the error between prediction and real data
- cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),reduction_indices=[1]))
- train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
- sess = tf.Session()
- # important step
- init = tf.global_variables_initializer()
- sess.run(init)
- for i in range(1000):
- batch_xs, batch_ys = mnist.train.next_batch(100)
- sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: 0.5})
- if i % 50 == 0:
- print(compute_accuracy(
- mnist.test.images[:1000], mnist.test.labels[:1000]))
运行结果(本人电脑太差,准确度出现了下滑的问题):
TensorFlow从入门到理解(三):你的第一个卷积神经网络(CNN)的更多相关文章
- TensorFlow从入门到理解
一.<莫烦Python>学习笔记: TensorFlow从入门到理解(一):搭建开发环境[基于Ubuntu18.04] TensorFlow从入门到理解(二):你的第一个神经网络 Tens ...
- 卷积神经网络(CNN)学习笔记1:基础入门
卷积神经网络(CNN)学习笔记1:基础入门 Posted on 2016-03-01 | In Machine Learning | 9 Comments | 14935 Vie ...
- 深度学习之卷积神经网络CNN及tensorflow代码实例
深度学习之卷积神经网络CNN及tensorflow代码实例 什么是卷积? 卷积的定义 从数学上讲,卷积就是一种运算,是我们学习高等数学之后,新接触的一种运算,因为涉及到积分.级数,所以看起来觉得很复杂 ...
- 深度学习之卷积神经网络CNN及tensorflow代码实现示例
深度学习之卷积神经网络CNN及tensorflow代码实现示例 2017年05月01日 13:28:21 cxmscb 阅读数 151413更多 分类专栏: 机器学习 深度学习 机器学习 版权声明 ...
- 卷积神经网络CNN原理以及TensorFlow实现
在知乎上看到一段介绍卷积神经网络的文章,感觉讲的特别直观明了,我整理了一下.首先介绍原理部分. [透析] 卷积神经网络CNN究竟是怎样一步一步工作的? 通过一个图像分类问题介绍卷积神经网络是如何工作的 ...
- TensorFlow 2.0 深度学习实战 —— 浅谈卷积神经网络 CNN
前言 上一章为大家介绍过深度学习的基础和多层感知机 MLP 的应用,本章开始将深入讲解卷积神经网络的实用场景.卷积神经网络 CNN(Convolutional Neural Networks,Conv ...
- 【原创 深度学习与TensorFlow 动手实践系列 - 4】第四课:卷积神经网络 - 高级篇
[原创 深度学习与TensorFlow 动手实践系列 - 4]第四课:卷积神经网络 - 高级篇 提纲: 1. AlexNet:现代神经网络起源 2. VGG:AlexNet增强版 3. GoogleN ...
- TensorFlow从1到2(三)数据预处理和卷积神经网络
数据集及预处理 从这个例子开始,相当比例的代码都来自于官方新版文档的示例.开始的几个还好,但随后的程序都将需要大量的算力支持.Google Colab是一个非常棒的云端实验室,提供含有TPU/GPU支 ...
- 深度学习:Keras入门(二)之卷积神经网络(CNN)
说明:这篇文章需要有一些相关的基础知识,否则看起来可能比较吃力. 1.卷积与神经元 1.1 什么是卷积? 简单来说,卷积(或内积)就是一种先把对应位置相乘然后再把结果相加的运算.(具体含义或者数学公式 ...
- 深度学习:Keras入门(二)之卷积神经网络(CNN)【转】
本文转载自:https://www.cnblogs.com/lc1217/p/7324935.html 说明:这篇文章需要有一些相关的基础知识,否则看起来可能比较吃力. 1.卷积与神经元 1.1 什么 ...
随机推荐
- js 判断 是否在当前页面 当前页面是否在前端
1.使用visibilitychange 浏览器标签页被隐藏或显示的时候会触发visibilitychange事件. document.addEventListener("visibilit ...
- 解决VS2012 服务器资源管理器中的表拖不到Linq to sql中
找到C:\Program Files (x86)\Common Files\microsoft shared\Visual Database Tools\dsref80.dll 这个dll文件: 在其 ...
- pycharm 出现 "PEP:8 expected 2 blank lines ,found 0"
https://blog.csdn.net/modangtian/article/details/79687623 这句话的意思是“有两个空白行,但是没有发现.” 在声明函数的那一行的上方必须有两行的 ...
- 完美解决windows+ngnix+phpcgi自动退出的问题
[摘要]在windows下搭建nginx+php环境时,php-cgi.exe会经常性的自动关闭退出,本文介绍通过使用xxfpm进程管理器管理php-cgi.exe. php-cgi.exe在wind ...
- Zookeeper 集群安装配置
今天,栈长分享下 Zookeeper 的集群安装及配置. 下载 下载地址:http://zookeeper.apache.org/ 下载过程就不说了,我们下载了最新的zookeeper-3.4.11. ...
- 将 数据库中的结果集转换为json格式(三)
从数据库中得到结果集 public String list() throws Exception { Connection con = null; PageBean pageBean = new Pa ...
- SpringMvc的基本流程
1.流程图 2.特别说明 1)SpringMvc有6大组件(MVC各一个,再加3个运用策略模式的组件) 2)MVC对应的组件分别是(Handler.View.DisapatchServelet) 3) ...
- Spring Boot笔记一 输出hello
开始学习Spring Boot了,本篇文章你可以学到 1.Spring Boot的基本配置,输出一句hello 许嵩 2.Spring boot打包成jar包 一.Spring boot的基本配置 这 ...
- FastDFS与springboot整合例子
余庆先生提供了一个Java客户端,但是作为一个C程序员,写的java代码可想而知.而且已经很久不维护了. 这里推荐一个开源的FastDFS客户端,支持最新的SpringBoot2.0. 配置使用极为简 ...
- vue基础篇---路由的实现《2》
现在我们来实现这样一个功能: 一个页面,包含登录和注册,点击不同按钮,实现登录和注册页切换: 编写父组件 index.html <div id="app"> <s ...