欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python人工智能tensorflow構(gòu)建卷積神經(jīng)網(wǎng)絡(luò)CNN

 更新時間:2022年05月05日 10:04:26   作者:Bubbliiiing  
學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)已經(jīng)有一段時間,從普通的BP神經(jīng)網(wǎng)絡(luò)到LSTM長短期記憶網(wǎng)絡(luò)都有一定的了解,但是從未系統(tǒng)的把整個神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)記錄下來,我相信這些小記錄可以幫助我更加深刻的理解神經(jīng)網(wǎng)絡(luò)

學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)已經(jīng)有一段時間,從普通的BP神經(jīng)網(wǎng)絡(luò)到LSTM長短期記憶網(wǎng)絡(luò)都有一定的了解,但是從未系統(tǒng)的把整個神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)記錄下來,我相信這些小記錄可以幫助我更加深刻的理解神經(jīng)網(wǎng)絡(luò)。

簡介

卷積神經(jīng)網(wǎng)絡(luò)(Convolutional Neural Networks, CNN)是一類包含卷積計算且具有深度結(jié)構(gòu)的前饋神經(jīng)網(wǎng)絡(luò)(Feedforward Neural Networks),是深度學(xué)習(xí)(deep learning)的代表算法之一。

其主要結(jié)構(gòu)分為輸入層、隱含層、輸出層。

在tensorboard中,其結(jié)構(gòu)如圖所示:

對于卷積神經(jīng)網(wǎng)絡(luò)而言,其輸入層、輸出層與平常的卷積神經(jīng)網(wǎng)絡(luò)無異。

但其隱含層可以分為三個部分,分別是卷積層(對輸入數(shù)據(jù)進行特征提?。?、池化層(特征選擇和信息過濾)、全連接層(等價于傳統(tǒng)前饋神經(jīng)網(wǎng)絡(luò)中的隱含層)。

隱含層介紹

1、卷積層

卷積將輸入圖像放進一組卷積濾波器,每個濾波器激活圖像中的某些特征。

假設(shè)一副黑白圖像為5*5的大小,像這樣:

利用如下卷積器進行卷積:

卷積結(jié)果為:

卷積過程可以提取特征,卷積神經(jīng)網(wǎng)絡(luò)是根據(jù)特征來完成分類的。

在tensorflow中,卷積層的重要函數(shù)是:

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)

其中:

1、input是輸入量,shape是[batch, height, width, channels]。;

2、filter是使用的卷積核;

3、strides是步長,其格式[1,step,step,1],step指的是在圖像卷積的每一維的步長;

4、padding:string類型的量,只能是"SAME","VALID"其中之一,SAME表示卷積前后圖像面積不變。

2、池化層

池化層用于在卷積層進行特征提取后,輸出的特征圖會被傳遞至池化層進行特征選擇和信息過濾。

常見的池化是最大池化,最大池化指的是取出這些被卷積后的數(shù)據(jù)的最大值,就是取出其最大特征。

假設(shè)其池化窗口為2X2,步長為2。

原圖像為:

池化后為:

在tensorflow中,池化層的重要函數(shù)是:

tf.nn.max_pool(value, ksize, strides, padding, data_format, name)

1、value:池化層的輸入,一般池化層接在卷積層后面,shape是[batch, height, width, channels]。

2、ksize:池化窗口的大小,取一個四維向量,一般是[1, in_height, in_width, 1]。

3、strides:和卷積類似,窗口在每一個維度上滑動的步長,也是[1, stride,stride, 1]。

4、padding:和卷積類似,可以取’VALID’ 或者’SAME’。

這是tensorboard中卷積層和池化層的連接結(jié)構(gòu):

3、全連接層

全連接層與普通神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)相同,如圖所示:

具體實現(xiàn)代碼

卷積層、池化層與全連接層實現(xiàn)代碼

def conv2d(x,W,step,pad): #用于進行卷積,x為輸入值,w為卷積核
    return tf.nn.conv2d(x,W,strides = [1,step,step,1],padding = pad)
def max_pool_2X2(x,step,pad):	#用于池化,x為輸入值,step為步數(shù)
    return tf.nn.max_pool(x,ksize = [1,2,2,1],strides= [1,step,step,1],padding = pad)
def weight_variable(shape):		#用于獲得W
    initial = tf.truncated_normal(shape,stddev = 0.1) #從截斷的正態(tài)分布中輸出隨機值
    return tf.Variable(initial)
def bias_variable(shape):		#獲得bias
    initial = tf.constant(0.1,shape=shape)  #生成普通值
    return tf.Variable(initial)
def add_layer(inputs,in_size,out_size,n_layer,activation_function = None,keep_prob = 1):	
#用于添加全連接層
    layer_name = 'layer_%s'%n_layer
    with tf.name_scope(layer_name):
        with tf.name_scope("Weights"):
            Weights = tf.Variable(tf.truncated_normal([in_size,out_size],stddev = 0.1),name = "Weights")
            tf.summary.histogram(layer_name+"/weights",Weights)
        with tf.name_scope("biases"):
            biases = tf.Variable(tf.zeros([1,out_size]) + 0.1,name = "biases")
            tf.summary.histogram(layer_name+"/biases",biases)
        with tf.name_scope("Wx_plus_b"):
            Wx_plus_b = tf.matmul(inputs,Weights) + biases
            tf.summary.histogram(layer_name+"/Wx_plus_b",Wx_plus_b)
        if activation_function == None :
            outputs = Wx_plus_b 
        else:
            outputs = activation_function(Wx_plus_b)
        print(activation_function)
        outputs = tf.nn.dropout(outputs,keep_prob)
        tf.summary.histogram(layer_name+"/outputs",outputs)
        return outputs
def add_cnn_layer(inputs, in_z_dim, out_z_dim, n_layer, conv_step = 1, pool_step = 2, padding = "SAME"):
#用于生成卷積層和池化層
    layer_name = 'layer_%s'%n_layer
    with tf.name_scope(layer_name):
        with tf.name_scope("Weights"):
            W_conv = weight_variable([5,5,in_z_dim,out_z_dim])
        with tf.name_scope("biases"):
            b_conv = bias_variable([out_z_dim])
        with tf.name_scope("conv"):
        #卷積層
            h_conv = tf.nn.relu(conv2d(inputs, W_conv, conv_step, padding)+b_conv) 
        with tf.name_scope("pooling"):
        #池化層
            h_pool = max_pool_2X2(h_conv, pool_step, padding)
    return h_pool

全部代碼

import tensorflow as tf 
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data",one_hot = "true")
def conv2d(x,W,step,pad):
    return tf.nn.conv2d(x,W,strides = [1,step,step,1],padding = pad)
def max_pool_2X2(x,step,pad):
    return tf.nn.max_pool(x,ksize = [1,2,2,1],strides= [1,step,step,1],padding = pad)
def weight_variable(shape):
    initial = tf.truncated_normal(shape,stddev = 0.1) #從截斷的正態(tài)分布中輸出隨機值
    return tf.Variable(initial)
def bias_variable(shape):
    initial = tf.constant(0.1,shape=shape)  #生成普通值
    return tf.Variable(initial)
def add_layer(inputs,in_size,out_size,n_layer,activation_function = None,keep_prob = 1):
    layer_name = 'layer_%s'%n_layer
    with tf.name_scope(layer_name):
        with tf.name_scope("Weights"):
            Weights = tf.Variable(tf.truncated_normal([in_size,out_size],stddev = 0.1),name = "Weights")
            tf.summary.histogram(layer_name+"/weights",Weights)
        with tf.name_scope("biases"):
            biases = tf.Variable(tf.zeros([1,out_size]) + 0.1,name = "biases")
            tf.summary.histogram(layer_name+"/biases",biases)
        with tf.name_scope("Wx_plus_b"):
            Wx_plus_b = tf.matmul(inputs,Weights) + biases
            tf.summary.histogram(layer_name+"/Wx_plus_b",Wx_plus_b)
        if activation_function == None :
            outputs = Wx_plus_b 
        else:
            outputs = activation_function(Wx_plus_b)
        print(activation_function)
        outputs = tf.nn.dropout(outputs,keep_prob)
        tf.summary.histogram(layer_name+"/outputs",outputs)
        return outputs
def add_cnn_layer(inputs, in_z_dim, out_z_dim, n_layer, conv_step = 1, pool_step = 2, padding = "SAME"):
    layer_name = 'layer_%s'%n_layer
    with tf.name_scope(layer_name):
        with tf.name_scope("Weights"):
            W_conv = weight_variable([5,5,in_z_dim,out_z_dim])
        with tf.name_scope("biases"):
            b_conv = bias_variable([out_z_dim])
        with tf.name_scope("conv"):
            h_conv = tf.nn.relu(conv2d(inputs, W_conv, conv_step, padding)+b_conv) 
        with tf.name_scope("pooling"):
            h_pool = max_pool_2X2(h_conv, pool_step, padding)
    return h_pool
def compute_accuracy(x_data,y_data):
    global prediction
    y_pre = sess.run(prediction,feed_dict={xs:x_data,keep_prob:1})
    correct_prediction = tf.equal(tf.arg_max(y_data,1),tf.arg_max(y_pre,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    result = sess.run(accuracy,feed_dict = {xs:batch_xs,ys:batch_ys,keep_prob:1})
    return result
keep_prob = tf.placeholder(tf.float32)
xs = tf.placeholder(tf.float32,[None,784])
ys = tf.placeholder(tf.float32,[None,10])
x_image = tf.reshape(xs,[-1,28,28,1])
h_pool1 = add_cnn_layer(x_image, in_z_dim = 1, out_z_dim = 32, n_layer = "cnn1",)
h_pool2 = add_cnn_layer(h_pool1, in_z_dim = 32, out_z_dim = 64, n_layer = "cnn2",)
h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])
h_fc1_drop = add_layer(h_pool2_flat, 7*7*64, 1024, "layer1", activation_function = tf.nn.relu, keep_prob = keep_prob)
prediction = add_layer(h_fc1_drop, 1024, 10, "layer2", activation_function = tf.nn.softmax, keep_prob = 1)
with tf.name_scope("loss"):
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=ys,logits = prediction),name = 'loss')
    tf.summary.scalar("loss",loss)
train = tf.train.AdamOptimizer(1e-4).minimize(loss)
init = tf.initialize_all_variables()
merged = tf.summary.merge_all()
with tf.Session() as sess:
    sess.run(init)
    write = tf.summary.FileWriter("logs/",sess.graph)
    for i in range(5000):
        batch_xs,batch_ys = mnist.train.next_batch(100)
        sess.run(train,feed_dict = {xs:batch_xs,ys:batch_ys,keep_prob:0.5})
        if i % 100 == 0:
            print(compute_accuracy(mnist.test.images,mnist.test.labels))

以上就是python人工智能tensorflow構(gòu)建卷積神經(jīng)網(wǎng)絡(luò)CNN的詳細內(nèi)容,更多關(guān)于tensorflow構(gòu)建卷積神經(jīng)網(wǎng)絡(luò)CNN的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python 數(shù)據(jù)結(jié)構(gòu)之樹的概念詳解

    Python 數(shù)據(jù)結(jié)構(gòu)之樹的概念詳解

    這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)之樹的概念詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Python的Django框架中URLconf相關(guān)的一些技巧整理

    Python的Django框架中URLconf相關(guān)的一些技巧整理

    這篇文章主要介紹了Python的Django框架中URLconf相關(guān)的一些技巧整理,包括視圖配置和debug的示例等,需要的朋友可以參考下
    2015-07-07
  • python批量替換多文件字符串問題詳解

    python批量替換多文件字符串問題詳解

    批量替換是我們在日常工作中經(jīng)常會遇到的一個問題,下面這篇文章主要給大家介紹了關(guān)于python批量替換多文件字符串問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • python 中dict的元素取值操作

    python 中dict的元素取值操作

    這篇文章主要介紹了python 中dict的元素取值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python調(diào)用shell cmd方法代碼示例解析

    Python調(diào)用shell cmd方法代碼示例解析

    這篇文章主要介紹了Python調(diào)用shell cmd方法代碼示例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • python比較兩個列表是否相等的方法

    python比較兩個列表是否相等的方法

    這篇文章主要介紹了python比較兩個列表是否相等的方法,實例分析了Python中==和is兩種方法的區(qū)別,需要的朋友可以參考下
    2015-07-07
  • Python中xml和json格式相互轉(zhuǎn)換操作示例

    Python中xml和json格式相互轉(zhuǎn)換操作示例

    這篇文章主要介紹了Python中xml和json格式相互轉(zhuǎn)換操作,結(jié)合實例形式分析了xmltodict庫的安裝及xml格式與json格式數(shù)據(jù)相互轉(zhuǎn)換操作技巧,需要的朋友可以參考下
    2018-12-12
  • Numpy中的ravel_multi_index函數(shù)用法說明

    Numpy中的ravel_multi_index函數(shù)用法說明

    這篇文章主要介紹了Numpy中的ravel_multi_index函數(shù)用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • 公認8個效率最高的爬蟲框架

    公認8個效率最高的爬蟲框架

    在本篇文章里小編給大家整理的是關(guān)于2020年8個效率最高的爬蟲框架知識點,需要的朋友們可以學(xué)習(xí)下。
    2020-07-07
  • 如何在pycharm中安裝第三方包

    如何在pycharm中安裝第三方包

    這篇文章主要介紹了如何在pycharm中安裝第三方包,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10

最新評論