pytorch-dropout相關

今天在修改代碼時,發現對dropout的inplace操作不太明白,所以有了此文,介紹一下pytorch的dropout函數

Dropout layers

在pytorch的doc中nn.dropout類如下,與functional.dropout函數一致:

CLASS: torch.nn.Dropout(p=0.5, inplace=False)
Input: Any. Input can be of any shape
Output: Same. Output is of the same shape as input

兩個參數:

  • p爲對於input中各個元素zero out的概率,也就是說當p=1時,output爲全0。
  • inplace參數,示例如下,就是是否對tensor本身操作:

inplace爲True時:

在這裏插入圖片描述
inplace爲False時
在這裏插入圖片描述

Dropout2d or 3d

除此之外,pytorch還定義了Dropout2d和Dropout3d兩個變種,其實也相對簡單,Dropout2d要求輸入數據爲4維,它針對feature map進行zero out操作,也就是說:比如一個abmn的input,對它的每個mn的feature map,也就是input[i, j]進行zero out。示例如下:

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章