轉自https://blog.csdn.net/shuzfan/article/details/51460895。
Loss Function
softmax_loss的計算包含2步:
(1)計算softmax歸一化概率
(2)計算損失
這里以batchsize=1的2分類為例:
設最后一層的輸出為[1.2 0.8],減去最大值后為[0 -0.4],
然后計算歸一化概率得到[0.5987 0.4013],
假如該圖片的label為1,則Loss=-log0.4013=0.9130
可選參數
(1) ignore_label
int型變量,默認為空。
如果指定值,則label等於ignore_label的樣本將不參與Loss計算,並且反向傳播時梯度直接置0.
(2) normalize
bool型變量,即Loss會除以參與計算的樣本總數;否則Loss等於直接求和
(3) normalization
enum型變量,默認為VALID,具體代表情況如下面的代碼。
enum NormalizationMode { // Divide by the number of examples in the batch times spatial dimensions. // Outputs that receive the ignore label will NOT be ignored in computing the normalization factor. FULL = 0; // Divide by the total number of output locations that do not take the // ignore_label. If ignore_label is not set, this behaves like FULL. VALID = 1; // Divide by the batch size. BATCH_SIZE = 2; // NONE = 3; }
歸一化case的判斷:
(1) 如果未設置normalization,但是設置了normalize。
則有normalize==1 -> 歸一化方式為VALID
normalize==0 -> 歸一化方式為BATCH_SIZE
(2) 一旦設置normalization,歸一化方式則由normalization決定,不再考慮normalize。
使用方法
layer { name: "loss" type: "SoftmaxWithLoss" bottom: "fc1" bottom: "label" top: "loss" top: "prob" loss_param{ ignore_label:0 normalize: 1 normalization: FULL } }
擴展使用
(1) 如上面的使用方法中所示,softmax_loss可以有2個輸出,第二個輸出為歸一化后的softmax概率
(2) 最常見的情況是,一個樣本對應一個標量label,但softmax_loss支持更高維度的label。
當bottom[0]的輸入維度為N*C*H*W時,
其中N為一個batch中的樣本數量,C為channel通常等於分類數,H*W為feature_map的大小通常它們等於1.
此時我們的一個樣本對應的label不再是一個標量了,而應該是一個長度為H*W的矢量,里面的數值范圍為0——C-1之間的整數。
至於之后的Loss計算,則采用相同的處理。