(6)caffe總結之其它常用層及引數

時光碎了天發表於2020-04-04

本文講解一些其它的常用層,包括: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)的個數

其它引數:

      weight_filler: 權值初始化。 預設為“constant",值全為0,很多時候我們用"xavier"演算法來進行初始化,也可以設定為”gaussian"
      bias_filler: 偏置項的初始化。一般設定為"constant",值全為0。
      bias_term: 是否開啟偏置項,預設為true, 開啟
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就可以了。

相關文章