训练mnist数据集

结构组成:

input_image --> convolution1 --> pool1 --> convolution2 --> pool2 --> full_connecion1 --> full_connection2
# 卷积
import tensorflow as tf import input_data # 加载mnist数据集
mnist = input_data.read_data_sets('MNIST_data', one_hot=True) # 构建多层卷积网络
# 权重及偏置初始化, ReLU神经元 用一个较小的正数来初始化偏置项来打破对称性以及避免0梯度
def weight_variable(shape):
"""
:param shape:二维tensor,第一个维度代表层中权重变量所连接(connect from)的单元数目,
第二个维度代表层中权重变量所连接(connect to)到的单元数量
:return: W
"""
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):
"""
卷积
:param x:
:param W:
:return:
"""
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding="SAME") def max_pool_2x2(x):
"""
最大池化
:param x:
:return:
"""
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") def compute_accuracy(v_xs, v_ys):
""" 计算的准确率 """
global prediction # prediction value
y_pre = sess.run(prediction, feed_dict={xs: v_xs})
# 与期望的值比较 bool
correct_pre = tf.equal(tf.argmax(y_pre, 1), tf.argmax(ys, 1))
# 将bools转化为数字
accuracy = tf.reduce_mean(tf.cast(correct_pre, tf.float32))
result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys})
return result # 数据图片
xs = tf.placeholder("float", shape=[None, 784]) # size 28 * 28 =784
# 预期概率
ys = tf.placeholder("float", shape=[None, 10]) # 10: 矩阵维度(分类)
keep_prob = tf.placeholder(tf.float32)
x_image = tf.reshape(xs, [-1, 28, 28, 1]) # -1: 任意数量的图片; 28*28:图片的长宽; 1:灰色图片为1 # layer1
W_conv1 = weight_variable([5, 5, 1, 32]) # 5*5:patch过滤长宽, 1:起始输入一张图片, 32:out_size
b_conv1 = bias_variable([32]) # 32:上层输入的out_size
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # output_size=28*28*32
h_pool1 = max_pool_2x2(h_conv1) # output_size=14*14*32 pool_strdes=2 # layer2
W_conv2 = weight_variable([5, 5, 32, 64]) # 64是训练中不断增加的高度,自定义
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) # output_size=14*14*64
h_pool2 = max_pool_2x2(h_conv2) # output_size=7*7*64 池化的步长为[2,2] # func1 layer
W_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) # 将pool2铺平为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) # 防止过拟合 # func2 layer
W_fc2 = weight_variable([1024, 10]) # 传入的1024, 判断0-9的数字one-hot,10来代表每个数字
b_fc2 = bias_variable([10])
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) # softmax分类 # the loss between prediction and really
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), reduction_indices=[1]))
tf.summary.scalar('loss', cross_entropy) # 字符串类型的标量张量,包含一个Summaryprotobuf 1.1记录标量
# training
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) # AdamOptimizer使用复杂模型 sess = tf.Session()
sess.run(tf.initialize_all_variables()) # start training
for i in range(1000):
batch_x, batch_y = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={xs: batch_x, ys: batch_y, keep_prob: 0.1})
if i % 50 == 0:
print(compute_accuracy(mnist.test.images, mnist.test.labels))
print("Training Finished !!!")

convolution-卷积神经网络的更多相关文章

  1. Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.1

    3.Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.1 http://blog.csdn.net/sunbow0 ...

  2. 深度学习:卷积神经网络(convolution neural network)

    (一)卷积神经网络 卷积神经网络最早是由Lecun在1998年提出的. 卷积神经网络通畅使用的三个基本概念为: 1.局部视觉域: 2.权值共享: 3.池化操作. 在卷积神经网络中,局部接受域表明输入图 ...

  3. 深度学习方法(十一):卷积神经网络结构变化——Google Inception V1-V4,Xception(depthwise convolution)

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.机器学习技术感兴趣的同学加入. 上一篇讲了深度学习方法(十) ...

  4. Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.2

    3.Spark MLlib Deep Learning Convolution Neural Network(深度学习-卷积神经网络)3.2 http://blog.csdn.net/sunbow0 ...

  5. Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.3

    3.Spark MLlib Deep Learning Convolution Neural Network(深度学习-卷积神经网络)3.3 http://blog.csdn.net/sunbow0 ...

  6. Deep Learning模型之:CNN卷积神经网络(一)深度解析CNN

    http://m.blog.csdn.net/blog/wu010555688/24487301 本文整理了网上几位大牛的博客,详细地讲解了CNN的基础结构与核心思想,欢迎交流. [1]Deep le ...

  7. Caffe(卷积神经网络框架)介绍

    Caffe(卷积神经网络框架)Caffe,全称Convolution Architecture For Feature Extraction caffe是一个清晰,可读性高,快速的深度学习框架.作者是 ...

  8. Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现(转)

    Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现 zouxy09@qq.com http://blog.csdn.net/zouxy09          自己平时看了一些论文, ...

  9. 卷积神经网络CNN介绍:结构框架,源码理解【转】

    1. 卷积神经网络结构 卷积神经网络是一个多层的神经网络,每层都是一个变换(映射),常用卷积convention变换和pooling池化变换,每种变换都是对输入数据的一种处理,是输入特征的另一种特征表 ...

  10. 卷积神经网络(CNN)

    1. 概述 卷积神经网络是一种特殊的深层的神经网络模型,它的特殊性体现在两个方面,一方面它的神经元间的连接是非全连接的, 另一方面同一层中某些神经元之间的连接的权重是共享的(即相同的).它的非全连接和 ...

随机推荐

  1. Docker私有镜像仓库

    使用阿里云加速: tee /etc/docker/daemon.json << 'EOF' { "registry-mirrors": [ "https:// ...

  2. hdu2087 剪花布条 暴力/KMP

    在字符串中不可重叠地寻找子串数量,暴力/KMP #include<stdio.h> #include<string.h> int main(){ ],b[]; ]!='#'){ ...

  3. 视觉惯性里程计Visual–Inertial Odometry(VIO)概述

    周围很多朋友开始做vio了,之前在知乎上也和胖爷讨论过这个问题,本文主要来自于知乎的讨论. 来自https://www.zhihu.com/question/53571648/answer/13772 ...

  4. 【MVC】View与Control之间数据传递

    1. Controller向View传递数据 使用ViewData传递数据[弱类型,字典型ViewDataDictionary] ViewData[“Message_ViewData”] = “ He ...

  5. WC游记

    第一次来WC,感觉这种集训真吼啊 day0 火车上快速补习了莫队,和AC自动姬,AC自动姬以前就会写只不过太久没写忘了我会了= = 莫队只是学习了做法,还没有做过题…… 本来想再复习一下后缀数组,然后 ...

  6. Javascript 在严格模式下不允许删除变量或对象

    如下代码,运行后在浏览器中会报错. <script> "use strict"; var x = 3.14; delete x; </script>

  7. mac上安装nginx

    终端执行: brew install nginx nginx 默认安装在 /usr/local/Cellar/nginx/1.12.2 conf 文件默认安装在 /usr/local/etc/ngin ...

  8. vue-router 知识点

    vue-router配置scrollBehavior 第三个参数 savedPosition 当且仅当 popstate 导航 (通过浏览器的 前进/后退 按钮触发) 时才可用. 注意: 这个功能只在 ...

  9. 线性模型的fit,predict

    线性模型的fit其实一个进行学习的过程,根据数据和标签进行学习:predict则是基于fit之后形成的模型,来决定指定的数据对应于标签(y_train_5)的值. 下面的是手写字母判断是否为“5” s ...

  10. Angularjs+ThinkPHP3.2.3集成微信分享JS-SDK实践

    先来看看微信分享效果: 在没有集成微信分享js-sdk前是这样的:没有摘要,缩略图任意抓取正文图片   在集成微信分享js-sdk后是这样的:标题,摘要,缩略图自定义   一.下载微信SDK开发包 下 ...