TensorFlow實現高階的卷積神經網路

2021-10-04 14:33:21 字數 3121 閱讀 6020

本人使用的資料集是cifar-10。這是乙個經典的資料集,許多**也都是在這個資料集上進行訓練。

使用的卷積神經網路是根據alex描述的cuda-convnet模型修改得來。在這個神經網路中,我使用了一些新的技巧:

(1)對weights進行了l2的正則化

(2)將影象進行翻轉、隨機剪下等資料增強,製造了更多的樣本

(3)在每個卷積-最大化池層後面使用了lrn層,增強了模型的泛化能力

git clone 

cd models/tutorials/image/cifar10

1、準備工作:定義初始化權重weight

2、用cifar10_input產生資料集

3、資料輸入、第一卷積層、第二卷積層、第一全連線層、第二全連線層、輸出層等

4、計算loss

5、訓練(用adam優化器減少loss)

6、評價模型在測試集上的準確度

7、最後計算評價結果並列印

最終,在cifar-10資料集上可以達到大致73%的準確率,增加max_step可以增加準確率。l2正則和lrn層的使用對模型準確率有提公升作用。

資料增強在我們的訓練中作用很大,他可以給單幅圖增加多個副本,提高的利用率,防止對某一張過擬合。使用資料增強後準確率大概上公升11%。

卷積層後一般跟乙個池化層,卷積池化的組合目前已經是影象識別的標準元件。卷積網路後面的幾個全連線層的作用是輸出分類結果,前面的卷積層主要是做特徵提取,直到最後的全連線層才開始對特徵進行組合匹配,並進行分類。

#建立輸入資料

image_holder = tf.placeholder(tf.float32,

[batch_size,24,

24,3]

)label_holder = tf.placeholder(tf.int32,

[batch_size]

)#建立第乙個卷積層

weight1 = variable_with_weight_loss(shape=[5

,5,3

,64], stddev=5e-

2, w1 =

0.0)

kernel1 = tf.nn.conv2d(image_holder,weight1,[1

,1,1

,1],padding=

'same'

)bias1 = tf.variable(tf.constant(

0.0, shape=[64

]))conv1 = tf.nn.bias_add(kernel1, bias1)

pool1 = tf.nn.max_pool(conv1, ksize=[1

,3,3

,1],strides=[1

,2,2

,1],padding=

'same'

)norm1 = tf.nn.lrn(pool1,

4, bias=

1.0, alpha=

0.001

/9.0

, beta=

0.75

)#建立第二個卷積層

weight2 = variable_with_weight_loss(shape=[5

,5,64

,64], stddev=5e-

2, w1 =

0.0)

kernel2 = tf.nn.conv2d(norm1, weight2,[1

,1,1

,1],padding=

'same'

)bias2 = tf.variable(tf.constant(

0.1, shape=[64

]))conv2 = tf.nn.relu(tf.nn.bias_add(kernel2, bias2)

)norm2 = tf.nn.lrn(conv2,

4, bias=

1.0, alpha=

0.001

/9.0

, beta=

0.75

)pool2 = tf.nn.max_pool(norm2, ksize=[1

,3,3

,1], strides=[1

,2,2

,1], padding=

'same'

)#全連線層

reshape = tf.reshape(pool2,

[batch_size,-1

])dim = reshape.get_shape()[

1].value

weight3 = variable_with_weight_loss(shape=

[dim,

384]

, stddev=

0.04

, w1=

0.004

)bias3 = tf.variable(tf.constant(

0.1, shape=

[384])

)local3 = tf.nn.relu(tf.matmul(reshape, weight3)

+ bias3)

#全連線層,跟前一層很像,但是隱含節點數少一半

weight4 = variable_with_weight_loss(shape=

[384

,192

], stddev=

0.04

, w1=

0.004

)bias4 = tf.variable(tf.constant(

0.1, shape=

[192])

)local4 = tf.nn.relu(tf.matmul(local3, weight4)

+ bias4)

#最後一層,先建立weight

weight5 = variable_with_weight_loss(shape=

[192,10

], stddev=1/

192.0

, w1=

0.0)

bias5 = tf.variable(tf.constant(

0.0, shape=[10

]))logits = tf.add(tf.matmul(local4, weight5)

, bias5)

tensorflow高階總結

tf.concat a,b axis n 拼接 不會會產生新的維度 e.g.a.shape 4,32,8 b.shape 6,32,8 axis 0 concat.shape 10,32,8 約束 非拼接維度之間必須保持一致,否則拼接不合法。tf.stack a,b axis n 堆疊 產生新的維度...

tensorflow 異或門的實現

一段小程式 待理解 import tensorflow as tf import numpy as np 輸入訓練資料,這裡是python的list,也可以定義為numpy的ndarray x data 1.0.0.1.0.0.1.1.定義佔位符,佔位符在執行圖的時候必須feed資料 x tf.pl...

tensorflow 高階API的區別與聯絡

tf.keras.layers和tf.layers有什麼區別?問問題投票2投票 最喜歡1 tf.keras.layers和tf.layers有什麼區別?例如,他們都有conv2d,他們提供不同的輸出嗎?如果你把它們混合起來有什麼好處 比如tf.keras.layers.conv2d在乙個隱藏層和下乙...