1. 程式人生 > >tensorFlow入門實踐(三)初識AlexNet實現結構

tensorFlow入門實踐(三)初識AlexNet實現結構

參數 variable alexnet with col 展望 port kernel 兩個

參考黃文堅《TensorFlow實戰》一書,完成AlexNet的整體實現並展望其訓練和預測過程。

import tensorflow as tf

batch_size = 32
num_batches = 100


# 顯示網絡每一層結構,展示每一個卷積層或池化層輸出tensor的尺寸,接受一個tensor作為輸入
def print_activations(t):
    print(t.op.name,  , t.get_shape().as_list())


# 接受images作為輸入,返回最後一層pool5(第五個池化層)
# 及parameters(AlexNet中所有需要訓練的模型參數)‘‘‘
def inference(images): parameters = [] with tf.name_scope(conv1) as scope: # 用截斷的正態分布函數(標準差為0.1)初始化卷積核的參數kernel。卷積核尺寸為11*11,顏色通道為3,卷積核數量為64 kernel = tf.Variable(tf.truncated_normal([11, 11, 3, 64], dtype=tf.float32, stddev=1e-1), name=weights)
# 使用tf.nn.conv2d對輸入images完成卷積操作 conv = tf.nn.conv2d(images, kernel, [1, 4, 4, 1], padding=SAME) biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32), trainable=True, name=biases) bias = tf.nn.bias_add(conv, biases) conv1 = tf.nn.relu(bias, name=scope) print_activations(conv1) parameters
+= [kernel, biases] # LRN處理和最大池化處理 lrn1 = tf.nn.lrn(conv1, 4, bias=1.0, alpha=0.001/9, beta=0.75, name=lrn1) pool1 = tf.nn.max_pool(lrn1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding=VALID, name=pool1) print_activations(pool1) # 打印輸出結果pool1的結構 # 設計第二個卷積層 卷積核尺寸5*5 輸入通道數64 卷積核數量192 with tf.name_scope(conv2) as scope: kernel = tf.Variable(tf.truncated_normal([5, 5, 64, 192], dthpe=tf.float32, stddev=1e-1), name=weights) # 卷積步長全部設為1,即掃描全圖像素 conv = tf.nn.conv2d(pool1, kernel, [1, 1, 1, 1], padding=SAME) biases = tf.Variable(tf.constant(0.0, shape=[192], dtype=tf.float32), trainable=True, name=biases) bias = tf.nn.bias_add(conv, biases) conv2 = tf.nn.relu(bias, name=scope) parameters += [kernel, biases] print_activations(conv2) lrn2 = tf.nn.lrn(conv2, 4, bias=1.0, alpha=0.001/9, beta=0.75, name=lrn2) pool2 = tf.nn.max_pool(lrn2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding=VALID, name=pool2) print_activations(pool2) # 創建第三個卷積層 卷積核尺寸3*3 輸入通道數192 卷積核數量384 步長全為1 with tf.name_scope(conv3) as scope: kernel = tf.Variable(tf.truncated_normal([3, 3, 192, 384], dthpe=tf.float32, stddev=1e-1), name=weights) # 卷積步長全部設為1,即掃描全圖像素 conv = tf.nn.conv2d(pool2, kernel, [1, 1, 1, 1], padding=SAME) biases = tf.Variable(tf.constant(0.0, shape=[384], dtype=tf.float32), trainable=True, name=biases) bias = tf.nn.bias_add(conv, biases) conv3 = tf.nn.relu(bias, name=scope) parameters += [kernel, biases] print_activations(conv3) # 創建第四個卷積層 卷積核尺寸3*3 輸入通道數384 卷積核數量降為256 with tf.name_scope(conv4) as scope: kernel = tf.Variable(tf.truncated_normal([3, 3, 384, 256], dthpe=tf.float32, stddev=1e-1), name=weights) # 卷積步長全部設為1,即掃描全圖像素 conv = tf.nn.conv2d(conv3, kernel, [1, 1, 1, 1], padding=SAME) biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), trainable=True, name=biases) bias = tf.nn.bias_add(conv, biases) conv4 = tf.nn.relu(bias, name=scope) parameters += [kernel, biases] print_activations(conv4) # 最後的第五個卷積層 卷積核尺寸3*3 輸入通道數256 卷積核數量為256 with tf.name_scope(conv5) as scope: kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256], dthpe=tf.float32, stddev=1e-1), name=weights) # 卷積步長全部設為1,即掃描全圖像素 conv = tf.nn.conv2d(conv4, kernel, [1, 1, 1, 1], padding=SAME) biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), trainable=True, name=biases) bias = tf.nn.bias_add(conv, biases) conv5 = tf.nn.relu(bias, name=scope) parameters += [kernel, biases] print_activations(conv5) # 在5個卷積層之後,還有一個最大池化層,這個池化層和前兩個卷積層後的池化層一致 pool5 = tf.nn.max_pool(conv5, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding=VALID, name=pool5) print_activations(pool5) return pool5, parameters # 在正式使用AlexNet來訓練或預測時,還需要添加3個全連接層,隱含節點數分別為4096、4096、1000

後續形成實現卷積神經網絡構建、訓練、測試的代碼架構,會將Alexnet實現結構重新組織完整和優化。

tensorFlow入門實踐(三)初識AlexNet實現結構