tensorflow1.x学习之 9-卷积神经网络(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。

导包
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
C:\Users\chengyuanting\.conda\envs\tensorflow19\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
C:\Users\chengyuanting\.conda\envs\tensorflow19\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
C:\Users\chengyuanting\.conda\envs\tensorflow19\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
C:\Users\chengyuanting\.conda\envs\tensorflow19\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
C:\Users\chengyuanting\.conda\envs\tensorflow19\lib\site-packages\tensorflow\python\framework\dtypes.py:521: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
C:\Users\chengyuanting\.conda\envs\tensorflow19\lib\site-packages\tensorflow\python\framework\dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
载入数据
mnist = input_data.read_data_sets("MNIST",one_hot=True)
Extracting MNIST\train-images-idx3-ubyte.gz
Extracting MNIST\train-labels-idx1-ubyte.gz
Extracting MNIST\t10k-images-idx3-ubyte.gz
Extracting MNIST\t10k-labels-idx1-ubyte.gz
batch_size = 100
n_batchs = mnist.train.num_examples //  batch_size
n_batchs
550
设置权重初始化函数

传入shape,返回对应shape的参数,服从高斯分布,0均值,0.1方差

def weight_variable(shape):
    initial = tf.truncated_normal(shape,stddev=0.1)
    return tf.Variable(initial)
设置偏执初始化的函数

传入shape,返回对应的0.1的参数

def biases_vriable(shape):
    initial = tf.constant(0.1,shape = shape)
    return tf.Variable(initial)
定义卷积层函数,用于生成一个对应的卷积操作
def conv2d(x,w):
    return tf.nn.conv2d(x,w,strides=[1,1,1,1],padding = 'SAME')
定义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')
定义两个placeholder
x = tf.placeholder(shape=[None,784], dtype = tf.float32)
y = tf.placeholder(shape=[None,10], dtype = tf.float32)
x,y
(<tf.Tensor 'Placeholder:0' shape=(?, 784) dtype=float32>,
 <tf.Tensor 'Placeholder_1:0' shape=(?, 10) dtype=float32>)
改变x的shape

输入的为1维数据,由于使用2D卷积,所以要将数据转为2D的图像数据

x_images = tf.reshape(x,shape=[-1,28,28,1])
x_images
<tf.Tensor 'Reshape:0' shape=(?, 28, 28, 1) dtype=float32>
搭建卷积层
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
(<tf.Variable 'Variable:0' shape=(5, 5, 1, 32) dtype=float32_ref>,
 <tf.Variable 'Variable_1:0' shape=(32,) dtype=float32_ref>)
h_conv1,h_pool1
(<tf.Tensor 'Relu:0' shape=(?, 28, 28, 32) dtype=float32>,
 <tf.Tensor 'MaxPool:0' shape=(?, 14, 14, 32) dtype=float32>)
w_conv2,b_conv2
(<tf.Variable 'Variable_2:0' shape=(5, 5, 32, 64) dtype=float32_ref>,
 <tf.Variable 'Variable_3:0' shape=(64,) dtype=float32_ref>)
h_conv2,h_pool2
(<tf.Tensor 'Relu_1:0' shape=(?, 14, 14, 64) dtype=float32>,
 <tf.Tensor 'MaxPool_1:0' shape=(?, 7, 7, 64) dtype=float32>)
搭建全连接网络

同时将池化层的输出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)
设置Dropout
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)
设置输出层
w_fc2 = weight_variable([1024,10])
b_fc2 = biases_vriable([10])

prediction = tf.nn.softmax(tf.matmul(h_fc1_drop,w_fc2) + b_fc2)
定义损失函数与优化器
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = prediction,labels = y))
WARNING:tensorflow:From <ipython-input-21-7b3b795589a5>:1: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:

Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.

See tf.nn.softmax_cross_entropy_with_logits_v2.

train_step = tf.train.AdamOptimizer(0.001).minimize(loss)
定义测评结果
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()
训练
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))
Iter: 0 Accuracy: 0.7849 Loss: 1.6734433
Iter: 1 Accuracy: 0.8826 Loss: 1.5777793

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值