學習筆記TF014:卷積層、激活函數、池化層、歸一化層、高級層


CNN神經網絡架構至少包含一個卷積層 (tf.nn.conv2d)。單層CNN檢測邊緣。圖像識別分類,使用不同層類型支持卷積層,減少過擬合,加速訓練過程,降低內存占用率。

TensorFlow加速所有不同類弄卷積層卷積運算。tf.nn.depthwise_conv2d,一個卷積層輸出邊接到另一個卷積層輸入,創建遵循Inception架構網絡 Rethinking the Inception Architecture for Computer Vision
https://arxiv.org/abs/1512.00567 。tf.nn.separable_conv2d,規模較大模型不犧牲准確率加速訓練,規模小模型快速收斂但准確率低。tf.nn.conv2d_transpos,卷積核用於新特征圖,每部分填充卷積核相同值,卷積核遍歷新圖像,重疊部分相加。斯坦福大學課程CS231n Winter 2016:Lecture 13。

激活函數與其他層輸出生成特征圖,對某些運算結果平滑(微分),為神經網絡引入非線性(輸入輸出曲線關系),刻畫輸入復雜變化,訓練復雜模型。激活函數主要因素,單調,輸出隨輸入增長,可用梯度下降法找局部極值點;可微分,定義域內任意一點有導數,輸出可用梯度下降法。
tf.nn.relu,修正線性單元,斜坡函數。分段線性,輸入非負輸出相同,輸入為負輸出為0。不受“梯度消失”影響,取值范圍[0, +∞]。較大學習速率時,易受飽和神經元影響。損失信息但性能突出。輸入秩1張量(向量),小於0置0,其余分量不變。
tf.sigmoid,只接收浮點數,返回區間[0.0, 1.0]內的值。輸入值較大返回接近1.0,輸入值較小返回接近0.0。適用於真實輸出位於[0.0, 1.0]。輸入接近飽和或變化劇烈,輸出范圍縮減成為問題。輸入0,輸出0.5,sigmoid函數值域中間點。
tf.tanh,雙曲正切函數,值域[-1.0, 1.0],有輸出負值能力。值域中間點為0.0。網絡下層期待輸入為負值或0.0,會有問題。
tf.nn.dropout,依據可配置概率輸出設0.0。適合少量隨機性有助於訓練。keep_prob參數指定輸出保持概率。每次執行,不同輸出。丟棄輸出設為0.0。

池化層減少過擬合,減小輸入尺寸,提高性能。輸入降采樣,為后續層保留重要信息。池化層減小尺寸效率比tf.nn.conv2d高。
tf.nn.max_pool,跳躍遍歷張量,卷積核覆蓋元素最大數值作卷積結果。適合輸入數據灰度與圖像重要性相關。輸入為前一層輸出,非直接圖像。跨度strides使用image_height、image_width遍歷輸入。只保留輸入張量最大元素。最大池化(max-pooling),利用接受域(卷積核)完成。2X2接受域,單個通路最小數量降采樣。1X1接受域,輸出輸入相同。
tf.nn.avg_pool,跳躍遍歷張量,卷積核覆蓋各深度值取平均。適合卷積核重要,實現值縮減。如輸入張量寬度高度大,深度小。

tf.nn.relu是無界函數,歸一化識別高頻特征。tf.nn.local_response_normalization(tf.nn.lrn),局部響應歸一化,給定向量,每個分量被depth_radius覆蓋輸入加權和除。輸入保持在可接受范圍。考慮每個值重要性。歸一化輸出調整到區間[-1.0, 1.0]。

高級層減少代碼冗余,遵循最佳實踐。
tf.contrib.layers.convolution2d。權值初始化、偏置初始化、可訓練變量輸出、偏置相加、添加激活函數。卷積核,可訓練變量。權值初始化用於卷積核首次運行值填充(tf.truncated_normal)。簡單元組形式表示卷積核高度和寬度。輸入圖像,tf.image.convert_image_dtype,調整各分量表示顏色值。TensorFlow要求浮點型描述圖像顏色,分量在[0, 1]。
tf.contrib.layers.fully_connected。全連接層,每個輸入輸出存在連接。CNN最后一層常是全連接層。TensorFlow全連接層格式,tf.matmul(features,weight)+bias。輸入張量與輸出層每個神經元連接。

原始輸入需要傳遞給輸入層。目標識別與分類輸入層tf.nn.conv2d。

 

    import tensorflow as tf
    features = tf.range(-2, 3)
    print features
    sess = tf.Session()
    print sess.run([features, tf.nn.relu(features)])
    features2 = tf.to_float(tf.range(-1, 3))
    print features2
    print sess.run([features2, tf.sigmoid(features2)])
    print sess.run([features2, tf.tanh(features2)])
    features3 = tf.constant([-0.1, 0.0, 0.1, 0.2])
    print features3
    print sess.run([features3, tf.nn.dropout(features3, keep_prob=0.5)])
    batch_size = 1
    input_height = 3
    input_width = 3
    input_channels = 1
    layer_input = tf.constant([
            [
                [[1.0], [0.2], [1.5]],
                [[0.1], [1.2], [1.4]],
                [[1.1], [0.4], [0.4]]
            ]
        ])
    print layer_input
    kernel = [batch_size, input_height, input_width, input_channels]
    print kernel
    max_pool = tf.nn.max_pool(layer_input, kernel, [1, 1, 1, 1], "VALID")
    print max_pool
    print sess.run(max_pool)
    layer_input2 = tf.constant([
            [
                [[1.0], [1.0], [1.0]],
                [[1.0], [0.5], [0.0]],
                [[0.0], [0.0], [0.0]]
            ]
        ])
    print layer_input2
    avg_pool = tf.nn.avg_pool(layer_input2, kernel, [1, 1, 1, 1], "VALID")
    print avg_pool
    print sess.run(avg_pool)
    layer_input3 = tf.constant([
            [
                [[1.], [2.], [3.]]
            ]
        ])
    print layer_input3
    lrn = tf.nn.local_response_normalization(layer_input3)
    print lrn
    print sess.run([layer_input3, lrn])
    image_input = tf.constant([
            [
                [[0., 0., 0.], [255., 255., 255.], [254., 0., 0.]],
                [[0., 191., 0.], [3., 108., 233.], [0., 191., 0.]],
                [[254., 0., 0.], [255., 255., 255.], [0., 0., 0.]]
            ]
        ])
    print image_input
    conv2d = tf.contrib.layers.convolution2d(
        image_input,
        num_outputs=4,
        kernel_size=(1,1),
        activation_fn=tf.nn.relu,
        stride=(1,1),
        trainable=True)
    print conv2d
    sess.run(tf.global_variables_initializer())
    print sess.run(conv2d)
    features4 = tf.constant([
            [[1.2], [3.4]]
        ])
    print features4
    fc = tf.contrib.layers.fully_connected(features4, num_outputs=2)
    print fc
    sess.run(tf.global_variables_initializer())
    print sess.run(fc)

 


參考資料:
《面向機器智能的TensorFlow實踐》

歡迎加我微信交流:qingxingfengzi
我的微信公眾號:qingxingfengzigz
我老婆張幸清的微信公眾號:qingqingfeifangz


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM