标准的卷积神经网络由输入层、卷积层(convolutional layer)、下采样层(downsampling layer)、全链接层(fully—connected layer)和输出层构成。git
第一个特色和优点就是:局部感知网络
第二个特色和优点就是:参数共享ide
第三个特色和优点就是:多卷积核函数
先了解卷积运算:内卷积和外卷积(具体以下图)学习
内卷积:优化
外卷积:spa
卷积层的做用:code
池化方式:(最大池化、平均池化)orm
最大池化方式(每一个小块中的最大值):blog
平均池化方式(每一个小块中的平均值):
池化层的做用:
def conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None,data_format=None, name=None)
除去name参数用以指定该操做的name,与方法有关的一共五个参数:
第一个参数input:
第二个参数filter:
第三个参数strides:
第四个参数padding:
举例理解:
假若有一个28*28的平面,用2*2而且步长为2的窗口对其进行pooling操做
使用SAME PADDING的方式,获得14*14的平面
使用VALID PADDING的方式,获得14*14的平面
假若有一个2*3的平面,用2*2而且步长为2的窗口对其进行pooling操做
使用SAME PADDING的方式,获得1*2的平面
使用VALID PADDING的方式,获得1*1的平面
第五个参数:use_cudnn_on_gpu:bool类型,是否使用cudnn加速,默认为true
结果返回一个Tensor,这个输出,就是咱们常说的feature map
实现代码:
1 import os 2 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 3 import tensorflow as tf 4 from tensorflow.examples.tutorials.mnist import input_data 5 6 #载入数据集 7 mnist = input_data.read_data_sets('MNIST_data', one_hot=True) 8 9 #每一个批次的大小 10 batch_size = 100 11 12 #计算一个有多少个批次 13 n_batch = mnist.train.num_examples // batch_size 14 15 #初始化权值 16 def weight_variable(shape): 17 initial = tf.truncated_normal(shape, stddev=0.1)#生成一个截断的正态分布 18 return tf.Variable(initial) 19 20 #初始化偏置 21 def bias_variable(shape): 22 initial = tf.constant(0.1, shape=shape) 23 return tf.Variable(initial) 24 25 #卷积层 26 def conv2d(x,W): 27 # x input tensor of shape `[batch, in_height, in_width, in_channels]` 28 # W filter / kernel tensor of shape [filter_height, filter_width, in_channels, out_channels] 29 # `strides[0] = strides[3] = 1`. strides[1]表明x方向的步长,strides[2]表明y方向的步长 30 # padding: A `string` from: `"SAME", "VALID"` 31 return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 32 33 #池化层 34 def max_pool_2x2(x): 35 # ksize [1,x,y,1] 36 return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 37 38 #定义两个placeholder 39 x = tf.placeholder(tf.float32, [None, 784]) #28*28 40 y = tf.placeholder(tf.float32, [None,10]) 41 42 #改变x的格式转为4D的向量[batch,in_height,in_width,in_channels] 43 x_image = tf.reshape(x, [-1, 28, 28, 1]) 44 45 #初始化第一个卷积层的权值和偏置 46 W_conv1 = weight_variable([5, 5, 1, 32]) #5*5的采样窗口,32个卷积核从1个平面抽取特征 47 b_conv1 = bias_variable([32]) #每个卷积核一个偏置值 48 49 #把x_image和权值向量进行卷积,再加上偏置值,而后应用于relu激活函数 50 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 51 h_pool1 = max_pool_2x2(h_conv1) 52 53 #初始第二个卷积层的权值和偏置值 54 W_conv2 = weight_variable([5, 5, 32, 64])#5*5的采样窗口,64个卷积核从32个平面抽取特征 55 b_conv2 = bias_variable([64]) 56 57 #把h_pool1和权值向量进行卷积,再加上偏置值,而后应用于relu激活函数 58 h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) 59 h_pool2 = max_pool_2x2(h_conv2) 60 61 #28*28的图片第一次卷积后仍是28*28,第一次池化后变成14*14 62 #第二次卷积为14*14,第二次池化后变成可7*7 63 #通过上面的操做后获得64张7*7的平面 64 65 #初始化第一个全链接层的权值 66 W_fc1 = weight_variable([7*7*64, 1024])#上一层有7*7*64个神经元,全链接层有1024个神经元 67 b_fc1 = bias_variable([1024])#1024个节点 68 69 #把池化层2的输出扁平化为1维 70 h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) 71 72 #求第一个全链接层的输出 73 h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 74 75 #keep_prob用来表示神经元的输出几率 76 keep_prob = tf.placeholder(tf.float32) 77 h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 78 79 #初始化第二个全链接层 80 W_fc2 = weight_variable([1024, 10]) 81 b_fc2 = bias_variable([10]) 82 83 #计算输出 84 prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 85 86 #交叉熵代价函数 87 cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction)) 88 89 #使用AdmaOptimizer进行优化 90 train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) 91 92 #结果存放在一个布尔列表中 93 correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1)) 94 95 #求准确率 96 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 97 98 #变量初始化 99 init = tf.global_variables_initializer() 100 101 with tf.Session() as sess: 102 sess.run(init) 103 for epoch in range(21): 104 for batch in range(n_batch): 105 batch_xs, batch_ys = mnist.train.next_batch(batch_size) 106 sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 0.7}) 107 acc = sess.run(accuracy, feed_dict={x: mnist.test.images[:5000], y: mnist.test.labels[:5000], keep_prob: 1.0}) 108 print('Iter : ' + str(epoch) + ',Testing Accuracy = ' + str(acc))
很久没有作学习总结
最近一直在给老师处理轴承数据,而后用深度学习作分类
天天就是忙忙忙
而后还要去健身
昨天是本身的生日,吃了一个超级可爱的小蛋糕
感受本身还像个孩子
永远18岁
最近烦心事有点多
不少时候没必要向别人解释本身
懂你的人天然而然就会懂
不懂得人解释也不懂
加油吧!小伙郭
加油,每个为了生活而努力向前的人!