TensorFlow1.x入门(9)——卷积神经网络(CNN)

系列文章

本教程有同步的github地址

0. 统领篇

1. 计算图的创建与启动

2. 变量的定义及其操作

3. Feed与Fetch

4. 线性回归

5. 构建非线性回归模型

6. 简单分类问题

7. Dropout与优化器

8. 手动调整学习率与TensorBoard

9. 卷积神经网络(CNN)

10. 循环神经网络(RNN)

11. 模型的保存与恢复

卷积神经网络(CNN)

引言

卷积神经网络(Convolutional Neural Networks, CNN)是深度学习领域内的一个重要的组成构建。CNN现在大量的用于图像识别领域,但是在自然语言处理和语音识别领域作为特征抽取器有广泛的应用。

一般情况下,提到卷积神经网络,它的网络结构包含卷积层和池化层。卷积层主用用于提取特征,而池化层有最大池化和平均池化,用于过滤特征。

知识点

基于示例中的代码讲解

tf.truncated_normal()是高斯截断初始化,随机生成的高斯分布的值,但生成的值不在2个标准差内的会进行丢弃。传入的参数shape是生成数据的大小,而stddev则是标准差。

tf.nn.conv2d()是TensorFlow中的二维卷积函数,x为输入的数据,w为卷积核大大小和通道数。strides的第一位和最后一位为1,剩下的代表卷积核的步长。padding只有“SAME”和“VLIAD”两种。

tf.nn.max_pool()是TensorFlow中的最大池化函数,x为输入的数据,kszie为池化层的核大小,strides为步长这,padding是模式的选择,与tf.nn.conv2d()含义相同。

tf.reshape()是对数据的重构(形状方面),可以改变数据的shape。

示例

#%% md
# 9-卷积神经网络(CNN)
#%% md
卷积神经网络是一类良好的特征提取器,多用于图像识别领域,现在在自然语言处理领域和语音识别领域也有较为广泛的运用。
#%% md
导包
#%%
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#%% md
载入数据
#%%
mnist = input_data.read_data_sets("MNIST", one_hot=True)
#%% md
设置batch_size的大小和批次
#%%
batch_size = 100
n_batchs = mnist.train.num_examples // batch_size
#%%
n_batchs
#%% md
设置权重初始化函数

传入shape,返回对应shape的参数,服从高斯分布,0均值,0.1方差
#%%
def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)
#%% md
设置偏执初始化的函数

传入shape,返回对应的0.1的参数
#%%
def biases_vriable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)
#%% md
定义卷积层函数,用于生成一个对应的卷积操作
#%%
def  conv2d(x, w):
    return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')
#%% md
定义$2\times 2$的池化层
#%%
def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#%% md
定义两个placeholder
#%%
x = tf.placeholder(shape=[None, 784], dtype=tf.float32)
y = tf.placeholder(shape=[None, 10], dtype=tf.float32)
#%%
x,y
#%% md
改变x的shape

输入的为1维数据,由于使用2D卷积,所以要将数据转为2D的图像数据
#%%
x_images = tf.reshape(x, shape=[-1, 28, 28, 1])
#%%
x_images
#%% md
搭建卷积层
#%%
w_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = biases_vriable([32])

h_conv1 = tf.nn.relu(conv2d(x_images, w_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

w_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = biases_vriable([64])

h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
#%%
w_conv1, b_conv1
#%%
h_conv1,h_pool1
#%%
w_conv2, b_conv2
#%%
h_conv2,h_pool2
#%% md
搭建全连接网络

同时将池化层的输出reshape为全联接的输入,并输入到网络中
#%%
w_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = biases_vriable([1024])

h_pool2_flat = tf.reshape(h_pool2, [-1, w_fc1.shape[0]])
h_fc1 = tf.nn.tanh(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)
#%% md
设置Dropout
#%%
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
#%% md
设置输出层
#%%
w_fc2 = weight_variable([1024, 10])
b_fc2 = biases_vriable([10])

prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, w_fc2) + b_fc2)
#%% md
定义损失函数与优化器
#%%
 loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
#%%
train_step = tf.train.AdamOptimizer(0.001).minimize(loss)
#%% md
定义测评结果
#%%
correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#%%
init = tf.global_variables_initializer()
#%% md
训练
#%%
with tf.Session() as sess:
    sess.run(init)
    for epoch in range(100):
        for batch in range(n_batchs):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step, {x: batch_xs, y:batch_ys, keep_prob:0.7})
        acc, l = sess.run([accuracy, loss], {x:mnist.test.images, y:mnist.test.labels, keep_prob: 1.0})
        print("Iter: " + str(epoch) + " Accuracy: " + str(acc) + " Loss: "+ str(l) )
#%%


猜你喜欢

转载自blog.csdn.net/qq_19672707/article/details/105613732