簡體   English   中英

Python:定義softmax函數

[英]Python: Define the softmax function

import numpy as np

def softmax(x):
    row_num = x.shape[0]
    col_num = x.shape[1]
    for m in row_num:
        for n in col_num:
            new_x[m,n] = np.exp(x[m,n])/sum(x[:,n])

    return new_x

logits = [1.0, 2.0, 3.0]
logits2 = np.array([
    [1, 2, 3, 6],
    [2, 4, 5, 6],
    [3, 8, 7, 6]])

print(softmax(logits1))
print(softmax(logits2))

以上是softmax的函數(用於將logits轉為概率)

我想獲得如下所示的解決方案:

[ 0.09003057  0.24472847  0.66524096]

[
    [ 0.09003057  0.00242826  0.01587624  0.33333333]
    [ 0.24472847  0.01794253  0.11731043  0.33333333]
    [ 0.66524096  0.97962921  0.86681333  0.33333333]
]

但是,發現錯誤“'int' 對象不可迭代”。 此外,我希望看到一個更高效、更復雜的函數代碼。

這將完成這項工作:

logits = np.array([1.0, 2.0, 3.0])
logits2 = np.array([
    [1, 2, 3, 6],
    [2, 4, 5, 6],
    [3, 8, 7, 6]])
def softmax(x):
    r=np.exp(x - np.max(x))
    return r/r.sum(axis=0)

你得到錯誤

“'int' 對象不可迭代”

因為row_num (和類似col_num )是一個數字,所以你不能迭代它。 您需要添加range (即for m in range(row_num) )。

還有其他問題。 例如, x.shape[1]不一定定義(它不是為logits定義的)所以它也會拋出錯誤。 new_x也沒有定義。

最有效的代碼應該是:

import numpy as np
def softmax(x):
    return np.exp(x) / np.sum(np.exp(x), axis=0)

編輯 從 1.2.0 版本開始,scipy 包含 softmax 作為一個特殊功能:

https://scipy.github.io/devdocs/generated/scipy.special.softmax.html

一般來說,最好使用矢量化實現而不是依賴 for 循環。 您可以利用 numpy 的 廣播來做到這一點。 還有許多其他問題擁有這樣一個函數的正確實現(例如: herehere )。

為了將答案與問題相關聯,我將粘貼在任意軸上運行的通用 softmax 函數,包括一個棘手的最大減法位。 我還寫了一篇關於它的更詳細的博客文章

def softmax(X, theta = 1.0, axis = None):
    """
    Compute the softmax of each element along an axis of X.

    Parameters
    ----------
    X: ND-Array. Probably should be floats. 
    theta (optional): float parameter, used as a multiplier
        prior to exponentiation. Default = 1.0
    axis (optional): axis to compute values along. Default is the 
        first non-singleton axis.

    Returns an array the same size as X. The result will sum to 1
    along the specified axis.
    """

    # make X at least 2d
    y = np.atleast_2d(X)

    # find axis
    if axis is None:
        axis = next(j[0] for j in enumerate(y.shape) if j[1] > 1)

    # multiply y against the theta parameter, 
    y = y * float(theta)

    # subtract the max for numerical stability
    y = y - np.expand_dims(np.max(y, axis = axis), axis)

    # exponentiate y
    y = np.exp(y)

    # take the sum along the specified axis
    ax_sum = np.expand_dims(np.sum(y, axis = axis), axis)

    # finally: divide elementwise
    p = y / ax_sum

    # flatten if X was 1D
    if len(X.shape) == 1: p = p.flatten()

    return p

我在 numpy 模塊中對 softmax 函數的實現是這樣的:

import numpy as np
def softmax(self,x,axis=0):    
    ex = np.exp(x - np.max(x,axis=axis,keepdims=True))
    return ex / np.sum(ex,axis=axis,keepdims=True) 
np.softmax = softmax.__get__(np)

那么就可以使用 softmax 函數作為典型的 numpy 內置函數。 喜歡 :

output = model(x)  # output.shape : (50000,10)
softmaxed_value = np.softmax(output,axis=1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM