亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在python中實現softmax方法

在python中實現softmax方法

元芳怎么了 2021-10-19 14:59:05
我試圖從 lightaime 的 Github 頁面理解這段代碼。它是一種 vetorized softmax 方法。讓我困惑的是“softmax_output[range(num_train), list(y)]”這個表達是什么意思?def softmax_loss_vectorized(W, X, y, reg):    """    Softmax loss function, vectorize implementation    Inputs have dimension D, there are C classes, and we operate on minibatches of N examples.    Inputs:        W: A numpy array of shape (D, C) containing weights.        X: A numpy array of shape (N, D) containing a minibatch of data.        y: A numpy array of shape (N,) containing training labels; y[i] = c means that X[i] has label c, where 0 <= c < C.        reg: (float) regularization strength    Returns a tuple of:        loss as single float        gradient with respect to weights W; an array of same shape as W    """    # Initialize the loss and gradient to zero.    loss = 0.0    dW = np.zeros_like(W)    num_classes = W.shape[1]    num_train = X.shape[0]    scores = X.dot(W)    shift_scores = scores - np.max(scores, axis = 1).reshape(-1,1)    softmax_output = np.exp(shift_scores)/np.sum(np.exp(shift_scores), axis = 1).reshape(-1,1)    loss = -np.sum(np.log(softmax_output[range(num_train), list(y)]))       loss /= num_train     loss +=  0.5* reg * np.sum(W * W)    dS = softmax_output.copy()    dS[range(num_train), list(y)] += -1    dW = (X.T).dot(dS)    dW = dW/num_train + reg* W    return loss, dW
查看完整描述

2 回答

?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

這個表達式的意思是:對一個softmax_output形狀數組進行切片,(N, C)從中只提取與訓練標簽相關的值y。


二維numpy.array可以用包含適當值的兩個列表進行切片(即它們不應導致索引錯誤)


range(num_train)為第一個軸創建一個索引,允許使用第二個索引 - 選擇每行中的特定值list(y)。你可以在numpy 的 indexing 文檔中找到它。


第一個索引 range_num 的長度等于softmax_output(= N)的第一個維度。它指向矩陣的每一行;然后對于每一行,它通過索引的第二部分中的相應值選擇目標值 - list(y)。


例子:


softmax_output = np.array(  # dummy values, not softmax

    [[1, 2, 3], 

     [4, 5, 6],

     [7, 8, 9],

     [10, 11, 12]]

)

num_train = 4  # length of the array

y = [2, 1, 0, 2]  # a labels; values for indexing along the second axis

softmax_output[range(num_train), list(y)]

Out:

[3, 5, 7, 12]

因此,它從第一行中選擇第三個元素,從第二行中選擇第二個元素,等等。這就是它的工作原理。


(ps 我誤解了你,你對“為什么”感興趣,而不是“如何”?)


查看完整回答
反對 回復 2021-10-19
?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

這里的損失由以下等式定義

http://img1.sycdn.imooc.com//616e6cf80001e80303160057.jpg

這里,對于數據點所屬的類,y 為 1,對于所有其他類,y 為 0。因此,我們只對數據點類的 softmax 輸出感興趣。因此上面的方程可以改寫為

http://img1.sycdn.imooc.com//616e6d010001fbab03710054.jpg

因此,下面的代碼表示上述等式。

loss = -np.sum(np.log(softmax_output[range(num_train), list(y)]))

該代碼softmax_output[range(num_train), list(y)]用于為各個類選擇 softmax 輸出。range(num_train)代表所有訓練樣本并list(y)代表各自的類別。

Mikhail 在他的回答中很好地解釋了這種索引。


查看完整回答
反對 回復 2021-10-19
  • 2 回答
  • 0 關注
  • 299 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號