1. 程式人生 > >cnn系列文章六 --deeplearning.ai andrew ng課上網路結構實現

cnn系列文章六 --deeplearning.ai andrew ng課上網路結構實現

慣例, 分享一句歌詞哦:

《天涯》
為君清宵歌一曲
一曲天涯夢魂遠

參考

基本函式封裝




def conv_2d(input_data, filter_shape, bias_shape, strides=None, padding='VALID', activation_function=0):
    '''

    :param input_data: [batch, in_height, in_weight, in_channels]
    :param filter_shape: [height, width, in_channels, out_channels]
    :param bias_shape: bias的長度與filter_shape最後一個數相同,
    :param strides: a list of ints, 預設[1, 1,1,1]
    :param padding:
    :param activation_function: 0 ->relu 1->sigmoid 2->tanh 3->無激勵函式
    :return: [batch, out_height, out_width, out_channels]
    '''
# 1. 執行濾波操作 tf.nn.conv2d # 2. 利用python的broadcast(廣播機制)加上bias # 3. 啟用函式(activation function) 預設不經過啟用函式, # 預設 strides = [1,1,1,1] if strides is None: strides = [1, 1, 1,1] # 根據論文,初始引數的方差與輸入到神經元個數關係: variance = 2 / num in_data_num = filter_shape[0] * filter_shape[1
] * filter_shape[1] fliter_init = tf.random_normal_initializer(stddev=(2.0 / in_data_num) ** 0.5) fliter = tf.get_variable('filter', filter_shape, initializer=fliter_init) bias_init = tf.constant_initializer(value=0) b = tf.get_variable('bias', bias_shape, initializer=bias_init) conv_out = tf.nn.conv2d(input_data,fliter,strides=strides, padding=padding) add_bias = tf.nn.bias_add(conv_out, b) if
activation_function == 2: return tf.nn.tanh(add_bias) elif activation_function == 1: return tf.nn.sigmoid(add_bias) elif activation_function == 0: return tf.nn.relu(add_bias) elif activation_function == 3: return add_bias def max_pool(input_data, k_size=2, k_strides=2, padding='VALID'): ''' :param input_data: [batch, in_height, in_weight, channels] :param k_size: 池化區域的大小 :param k_strides: 在每個維度上的步長, 一般在batch和channels上的步長為 1 :param padding: 'VALID' p=0 ; 'SAME' 輸入輸出維度相同 :return: [batch, out_height, ot_weight, channels] ''' return tf.nn.max_pool(input_data,ksize=[1, k_size, k_size, 1], strides=[1, k_strides, k_strides, 1], padding=padding) def layer(input_data, weight_shape, bias_shape, activation_function=0): ''' :param input_data: [batch, in_neurons] :param weight_shape: [in_neurons, out_neurons] :param bias_shape: [out_neurons] :param activation_function 0 ->relu 1->sigmoid 2->tanh 3->無激勵函式 :return: [batch, out_neurons] ''' weight_stddev = (2.0 / weight_shape[0]) ** 0.5 w_init = tf.random_normal_initializer(stddev=weight_stddev) bias_init = tf.constant_initializer(value=0.0) W = tf.get_variable('W', weight_shape, initializer=w_init) b = tf.get_variable('b', bias_shape, initializer=bias_init) out = tf.matmul(input_data, W) + b if activation_function == 2: return tf.nn.tanh(out) elif activation_function == 1: return tf.nn.sigmoid(out) elif activation_function == 0: return tf.nn.relu(out) elif activation_function == 3: return out

課程網路結構實現:

結構如下圖所示:
這裡寫圖片描述

import tensorflow as tf

def cnn_architecture(x, keep_prob=0.5):
    x = tf.reshape(x, shape=[-1, 32, 32, 3])


    with tf.variable_scope("layer_1"):
        # TODO: conv_1: input:[ batch, 32, 32, 3] -> output [batch, 28, 28, 6]
        conv_1 = conv_2d(x,filter_shape=[5, 5, 3, 6], bias_shape=[6], activation_function=0)
        # TODO: max_pool_1: input: [batch, 28, 28, 6] -> output [batch, 14, 14, 6]
        pool_1 = max_pool(conv_1)

    with tf.variable_scope("layer_2"):
        # TODO: conv_2: input: [batch, 14, 14, 6] ->output[ batch, 10, 10, 16]
        conv_2 = conv_2d(pool_1, filter_shape=[5, 5, 6, 16], bias_shape=[16], activation_function=0)
        #TODO: max_pool_2: input:[batch, 10, 10 ,16] -> output [batch, 5, 5, 16]
        pool_2 = max_pool(conv_2)

    with tf.variable_scope('FC3'):
        # TODO : flatten pool_2  [batch, 5, 5,  16] -> [batch, 400]
        _flatten = tf.contrib.layers.flatten(pool_2)
        # TODO: fully connection  [batch, 400] -> [batch, 120]
        fc_3 = layer(_flatten, [400, 120], [120], activation_function=0)

        # apply dropout to avoid overfitting
        fc_3_drop = tf.nn.dropout(fc_3, keep_prob=keep_prob)

    with tf.variable_scope('FC4'):
        # TODO:fully connection  [batch, 120] -> [batch, 84]
        fc_4 = layer(fc_3_drop, [120, 84], [84], activation_function=0)

        # apply dropout to avoid overfitting
        fc_4_drop = tf.nn.dropout(fc_4, keep_prob=keep_prob)

    with tf.variable_scope('Softmax'):
        # TODO: softmax classfier layer [batch, 84] -> [batch, 10]
        out = layer(fc_4_drop, [84, 10], [10], activation_function=3)
        softmax_out = tf.nn.softmax(out)
    return softmax_out