1、tensorflow卷积层
tensorflow提供了2个函数创建卷积层:tf.nn.conv2d()和tf.nn.bias_add()
k_output = 64
image_width = 10
image_height = 10
color_channels = 3
filter_size_width = 5
filter_size_height = 5
input = tf.placeholder(tf.float32, shape=[None, image_height, image_width, color_channels])
weight = tf.Variable(tf.truncated_normal([filter_size_height, filter_size_width, color_channels, k_output]))
bias = tf.Variable(tf.zeros(k_output))
conv_layer = tf.nn.conv2d(input, weight, strides=[1,2,2,1], padding='SAME')
conv_layer = tf.nn.bias_add(conv_layer, bias)
conv_layer = tf.nn.relu(conv_layer)
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None)
input是[batch, image_height, image_width, color_channels],None是samples个数。
weight是滤波器,[filter_size_height, filter_size_width, color_channels, k_output]
Tensorflow中的stride对input的每一维都有一个补偿,所以是strides=[1,2,2,1]。通常batch和color_channels的stride都是设为1。
2、常用的CNN模型结构
3个概念:池化,1*1卷积,inception
最常用的模型:卷积+池化,再加几个全连接层。
最开始使用这个模型的是:LeNet-5,1998年,字母识别
现在的CNN:AlexNet, 2012年赢得ImageNet物体识别大赛
3、tensorflow最大池化
Tensorflow中提供tf.nn.max_pool()函数用于对卷积层进行最大池化。
conv_layer = tf.nn.conv2d(input, weight, strides=[1,2,2,1], padding='SAME')
conv_layer = tf.nn.bias_add(conv_layer, bias)
conv_layer = tf.nn.relu(conv_layer)
conv_layer = tf.nn.max_pool(conv_layer, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
池化层总的来说是用来:减少输出大小,降低过拟合。
但池化也有自己的缺点:现在的数据量很大,更怕欠拟合问题。丢失了信息,因此更青睐dropout。
分类:平均池化、最大池化
4、1*1卷积
加入一个1*1卷积,将神经网络转换成非线性
5、Inception
将多个卷积、池化堆叠在一起。增加网络深度,但是又减少参数。