本文講解一些其它的常用層,包括:softmax_loss層,Inner Product層,accuracy層,reshape層和dropout層及其它們的參數配置。
1、softmax-loss
softmax-loss層和softmax層計算大致是相同的。softmax是一個分類器,計算的是類別的概率(Likelihood),是Logistic Regression 的一種推廣。Logistic Regression 只能用於二分類,而softmax可以用於多分類。
softmax與softmax-loss的區別:
softmax計算公式:
而softmax-loss計算公式:
關於兩者的區別更加具體的介紹,可參考:softmax vs. softmax-loss
用戶可能最終目的就是得到各個類別的概率似然值,這個時候就只需要一個 Softmax層,而不一定要進行softmax-Loss 操作;或者是用戶有通過其他什么方式已經得到了某種概率似然值,然后要做最大似然估計,此時則只需要后面的 softmax-Loss 而不需要前面的 Softmax 操作。因此提供兩個不同的 Layer 結構比只提供一個合在一起的 Softmax-Loss Layer 要靈活許多。
不管是softmax layer還是softmax-loss layer,都是沒有參數的,只是層類型不同而也
softmax-loss layer:輸出loss值
layer { name: "loss" type: "SoftmaxWithLoss" bottom: "ip1" bottom: "label" top: "loss" }
softmax layer: 輸出似然值
layers { bottom: "cls3_fc" top: "prob" name: "prob" type: “Softmax" }
2、Inner Product
全連接層,把輸入當作成一個向量,輸出也是一個簡單向量(把輸入數據blobs的width和height全變為1)。
輸入: n*c0*h*w
輸出: n*c1*1*1
全連接層實際上也是一種卷積層,只是它的卷積核大小和原數據大小一致。因此它的參數基本和卷積層的參數一樣。
層類型:InnerProduct
lr_mult: 學習率的系數,最終的學習率是這個數乘以solver.prototxt配置文件中的base_lr。如果有兩個lr_mult, 則第一個表示權值的學習率,第二個表示偏置項的學習率。一般偏置項的學習率是權值學習率的兩倍。
必須設置的參數:
num_output: 過濾器(filfter)的個數
其它參數:
layer { name: "ip1" type: "InnerProduct" bottom: "pool2" top: "ip1" param { lr_mult: 1 } param { lr_mult: 2 } inner_product_param { num_output: 500 weight_filler { type: "xavier" } bias_filler { type: "constant" } } }
3、accuracy
輸出分類(預測)精確度,只有test階段才有,因此需要加入include參數。
層類型:Accuracy
layer { name: "accuracy" type: "Accuracy" bottom: "ip2" bottom: "label" top: "accuracy" include { phase: TEST } }
4、reshape
在不改變數據的情況下,改變輸入的維度。
層類型:Reshape
先來看例子
layer { name: "reshape" type: "Reshape" bottom: "input" top: "output" reshape_param { shape { dim: 0 # copy the dimension from below dim: 2 dim: 3 dim: -1 # infer it from the other dimensions } } }
有一個可選的參數組shape, 用於指定blob數據的各維的值(blob是一個四維的數據:n*c*w*h)。
dim:0 表示維度不變,即輸入和輸出是相同的維度。
dim:2 或 dim:3 將原來的維度變成2或3
dim:-1 表示由系統自動計算維度。數據的總量不變,系統會根據blob數據的其它三維來自動計算當前維的維度值 。
假設原數據為:64*3*28*28, 表示64張3通道的28*28的彩色圖片
經過reshape變換:
reshape_param { shape { dim: 0 dim: 0 dim: 14 dim: -1 } }
輸出數據為:64*3*14*56
5、Dropout
Dropout是一個防止過擬合的trick。可以隨機讓網絡某些隱含層節點的權重不工作。
先看例子:
layer { name: "drop7" type: "Dropout" bottom: "fc7-conv" top: "fc7-conv" dropout_param { dropout_ratio: 0.5 } }
只需要設置一個dropout_ratio就可以了。
還有其它更多的層,但用的地方不多,就不一一介紹了。
隨着深度學習的深入,各種各樣的新模型會不斷的出現,因此對應的各種新類型的層也在不斷的出現。這些新出現的層,我們只有在等caffe更新到新版本后,再去慢慢地摸索了。