需要安装 python,numpy,tensorflow,运行代码即可。
tensorflow很好装,用pip安装即可。
可以参照http://wiki.jikexueyuan.com/project/tensorflow-zh/get_started/os_setup.htm安装。
数据集:mnist的,其实就是对数字进行分类。
      input:就是图像数据集
      labels:数字从0到9. 输出格式就是one-hot,一个10维度的向量,比如1就是[0,1,0,0,0,0,0,0,0,0,0].相应位置为1,其它位置为0.

1.CNN是一种带有卷积结构的深度神经网络,包括:

  • 卷积层
  • pooling layer
  • fullConnection layer

大概过程就是:

图像->卷积convention变换->pooling池化变换->卷积convention变换->pooling池化变换->fullConnection->output

2.卷积

卷积就是用一个卷积函数去过滤输入的图像,这个过程会把和卷积函数相似的那些数据提取出来,变成相应的特征,我们可以用多个卷积函数去卷积,那么就可以提取出多个图像,比如我代码里面就从原始的1张图片,变成32,再变成64...图像特征出来。相当于把原始的一张图片,变高,从1个张变成32张,每一张都是有卷积卷积出来的。 生成的32张可能分别包括了图像的直线,角,纹理等一些什么特征。深度学习自己提取特征,不用我们手工来构造这些特征,像这种图像,我们也不一定构造的特征就有用。

3.池化过程,就是下采样。

每邻域四个像素求和变为一个像素,然后通过标量Wx+1加权,再增加偏置bx+1,然后通过一个激活函数,产生一个大概缩小n倍的特征映射图,降维,

  • 图像有种“静态性”的属性
  • 降低维度

4.权值共享

   CNN为了减少训练参数,就是有个共享weight的概念,看代码其实就知道,再卷积的那个过程,其实卷积函数和不同位置的图像的w是一样的,比如窗口是5*5,有6个卷积核,就是有6*(5*5+1)个训练参数。

5.最后面的步骤就是和我们传统介绍的ANN一样,神经元都是全连接的。

[code]#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2016-07-24 12:52:36
# @Link : ${link}
# @Version : $Id$ import tensorflow as tf # 引入tensorflow库
from tensorflow.examples.tutorials.mnist import input_data # 引入tensorflow中的自带的一个读取mnist文件 mnist = input_data.read_data_sets('MNIST_data', one_hot=True) #计算每次迭代训练模型的准确率,在训练时,
#增了额外的参数keep_prob在feed_dict中,以控制dropout的几率;
#最后一步用到了dropout函数将模型数值随机地置零。如果keep_prob=1则忽略这步操作
#tf.argvmax:Returns the index with the largest value across dimensions of a tensor. 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 #返回指定shape的weight变量,这边truncated_normal表示正态分布 def weight_variable(shape):
initial = tf.truncated_normal(shape,stddev=0.1)
return tf.Variable(initial) #返回指定shape的bias,这边默认为0.1,constant 表示长量 def bias_variable(shape):
initial = tf.constant(0.1,shape=shape)
return tf.Variable(initial) #使用tensorflow的库,卷积操作,x是输入数据,W是权重
#Given an input tensor of shape [batch, in_height, in_width, in_channels] and a filter / kernel tensor of shape [filter_height, filter_width, in_channels, out_channels],
#strides 表示那个卷积核移动的步数,这边是1,并且采用smae,最后卷积的大小和输入的图像大小是一致的。 def conv2d(x,W):
return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME') #经过conv2d后,我们对过滤的图像进行一个pooling操作
#输入的x : [batch, height, width, channels]
#ksize就是pool的大小 这边是2*2
#strides 表示pool移动的步数,这边是2,所以每次会缩小2倍 def max_pool_2x2(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME') #输入的784维度的数据,lable是10维度。None这边表示训练数据的大小,可以先填none。
#placeholder 表示这个变量没有值,需要传入 feed_dic xs = tf.placeholder(tf.float32,[None,784])
ys = tf.placeholder(tf.float32,[None,10])
keep_prob = tf.placeholder(tf.float32) #把输入的数据转化为28*28*1的格式,-1表示数据的大小,可以填写为-1,最后一个1是rgb通道数目,这边默认为1
#这样x_image就变成[sample,28,28,1] x_image = tf.reshape(xs,[-1,28,28,1]) ## conv1 layer ##
#第一层卷积,5*5的卷积核,输入为1个图像,卷积成32个28*28图像 W_conv1 = weight_variable([5,5, 1,32]) # patch 5x5, in size 1, out size 32
b_conv1 = bias_variable([32])
#tf.nn.relu是激励函数
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # output size 28x28x32 #pooling缩小2倍,变成32个14*14的图像
h_pool1 = max_pool_2x2(h_conv1) # output size 14x14x32 ## conv2 layer ##
#第二层卷积,5*5的卷积核,输入为32个图像,卷积成64个28*28图像 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 #pooling缩小2倍,变成64个7*7的图像 h_pool2 = max_pool_2x2(h_conv2) #接下来就是普通的神经网络过程
## func1 layer ## W_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024]) # [n_samples, 7, 7, 64] ->> [n_samples, 7*7*64]
#把多维数据变为1个维度的数据,就是数组了 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) ## func2 layer ##
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
#使用softmax预测(0~9)
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=[1])) # loss
#优化函数
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) sess = tf.Session()
# 初始化变量,一定要有
sess.run(tf.initialize_all_variables()) 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, mnist.test.labels))

  

tensorflow-cnn的更多相关文章

  1. Android+TensorFlow+CNN+MNIST 手写数字识别实现

    Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...

  2. Tensorflow&CNN:验证集预测与模型评价

    版权声明:本文为博主原创文章,转载 请注明出处:https://blog.csdn.net/sc2079/article/details/90480140 - 写在前面 本科毕业设计终于告一段落了.特 ...

  3. Tensorflow&CNN:裂纹分类

    版权声明:本文为博主原创文章,转载 请注明出处:https://blog.csdn.net/sc2079/article/details/90478551 - 写在前面 本科毕业设计终于告一段落了.特 ...

  4. 强智教务系统验证码识别 Tensorflow CNN

    强智教务系统验证码识别 Tensorflow CNN 一直都是使用API取得数据,但是API提供的数据较少,且为了防止API关闭,先把验证码问题解决 使用Tensorflow训练模型,强智教务系统的验 ...

  5. tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图

    tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图 因为很多 demo 都比较复杂,专门抽出这两个函数,写的 demo. 更多教程:http://www.tensorflown ...

  6. Python Tensorflow CNN 识别验证码

    Python+Tensorflow的CNN技术快速识别验证码 文章来源于: https://www.jianshu.com/p/26ff7b9075a1 验证码处理的流程是:验证码分析和处理—— te ...

  7. python,tensorflow,CNN实现mnist数据集的训练与验证正确率

    1.工程目录 2.导入data和input_data.py 链接:https://pan.baidu.com/s/1EBNyNurBXWeJVyhNeVnmnA 提取码:4nnl 3.CNN.py i ...

  8. TensorFlow CNN 測试CIFAR-10数据集

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50738311 1 CIFAR-10 数 ...

  9. TensorFlow CNN 测试CIFAR-10数据集

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50738311 1 CIFAR-10 数 ...

  10. TensorFlow——CNN卷积神经网络处理Mnist数据集

    CNN卷积神经网络处理Mnist数据集 CNN模型结构: 输入层:Mnist数据集(28*28) 第一层卷积:感受视野5*5,步长为1,卷积核:32个 第一层池化:池化视野2*2,步长为2 第二层卷积 ...

随机推荐

  1. 使用Retrofit时出现 java.lang.IllegalArgumentException: URL query string "t={type}&p={page}&size={count}" must not have replace block. For dynamic query parameters use @Query.异常原因

    /** * Created by leo on 16/4/30. */ public interface GanchaiService { @GET("digest?t={type}& ...

  2. tcp通信:多进程共享listen socket方式

    原文链接:http://blog.csdn.net/largetalk/article/details/7939080 看tornado源码多进程(process.py)那段,发现他的多进程模型和一般 ...

  3. cocos2dx 的基本框架

    AppDelegate.h #ifndef _APP_DELEGATE_H_ #define _APP_DELEGATE_H_ #include "cocos2d.h" USING ...

  4. 车牌识别LPR(五)-- 一种车牌定位法

    该方法是某个文章中看到的,有点忘了是那一篇了,看的太多也太久了. Step1.把采集到的RGB图像转换为HSI图像. HSI模型能反映人对色彩的感知和鉴别能力,非常适合基于色彩的图像的相似比较,故采用 ...

  5. Android用户界面布局(layouts)

    Android用户界面布局(layouts) 备注:view理解为视图 一个布局定义了用户界面的可视结构,比如activity的UI或是APP widget的UI,我们可以用下面两种方式来声明布局: ...

  6. 面向对象设计Object Oriented Design

    http://www.codeproject.com/Articles/93369/How-I-explained-OOD-to-my-wife http://www.cnblogs.com/niyw ...

  7. oracle tns in linux

    [oracle@redhat4 admin]$ cd $ORACLE_HOME/network/admin[oracle@redhat4 admin]$ cat tnsnames.ora# tnsna ...

  8. git跨平台换行符不兼容

    https://help.github.com/articles/dealing-with-line-endings/#platform-all

  9. JAVA操作数据库插入中文表中显示乱码的解决方法

    String dbUrl = "jdbc:mysql://localhost:3306/BookDB?useUnicode=true&characterEncoding=GB2312 ...

  10. Qt之QTableView添加复选框(QAbstractItemDelegate)

    简述 上节分享了使用自定义模型QAbstractTableModel来实现复选框.下面我们来介绍另外一种方式: 自定义委托-QAbstractItemDelegate. 简述 效果 QAbstract ...