须要安装 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.python
1.CNN是一种带有卷积结构的深度神经网络,包括:数组
大概过程就是:网络
图像->卷积convention变换->pooling池化变换->卷积convention变换->pooling池化变换->fullConnection->output
2.卷积:ide
卷积就是用一个卷积函数去过滤输入的图像,这个过程会把和卷积函数类似的那些数据提取出来,变成相应的特征,咱们能够用多个卷积函数去卷积,那么就能够提取出多个图像,好比我代码里面就从原始的1张图片,变成32,再变成64...图像特征出来。至关于把原始的一张图片,变高,从1个张变成32张,每一张都是有卷积卷积出来的。 生成的32张可能分别包括了图像的直线,角,纹理等一些什么特征。深度学习本身提取特征,不用咱们手工来构造这些特征,像这种图像,咱们也不必定构造的特征就有用。函数
3.池化过程,就是下采样。学习
每邻域四个像素求和变为一个像素,而后经过标量Wx+1加权,再增长偏置bx+1,而后经过一个激活函数,产生一个大概缩小n倍的特征映射图,降维,优化
4.权值共享spa
CNN为了减小训练参数,就是有个共享weight的概念,看代码其实就知道,再卷积的那个过程,其实卷积函数和不一样位置的图像的w是同样的,好比窗口是5*5,有6个卷积核,就是有6*(5*5+1)个训练参数。code
5.最后面的步骤就是和咱们传统介绍的ANN同样,神经元都是全链接的。orm
[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))