欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關于tensorflow softmax函數(shù)用法解析

 更新時間:2020年06月30日 11:27:06   作者:ASR_THU  
這篇文章主要介紹了關于tensorflow softmax函數(shù)用法解析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

如下所示:

def softmax(logits, axis=None, name=None, dim=None):
 """Computes softmax activations.
 This function performs the equivalent of
  softmax = tf.exp(logits) / tf.reduce_sum(tf.exp(logits), axis)
 Args:
 logits: A non-empty `Tensor`. Must be one of the following types: `half`,
  `float32`, `float64`.
 axis: The dimension softmax would be performed on. The default is -1 which
  indicates the last dimension.
 name: A name for the operation (optional).
 dim: Deprecated alias for `axis`.
 Returns:
 A `Tensor`. Has the same type and shape as `logits`.
 Raises:
 InvalidArgumentError: if `logits` is empty or `axis` is beyond the last
  dimension of `logits`.
 """
 axis = deprecation.deprecated_argument_lookup("axis", axis, "dim", dim)
 if axis is None:
 axis = -1
 return _softmax(logits, gen_nn_ops.softmax, axis, name)

softmax函數(shù)的返回結果和輸入的tensor有相同的shape,既然沒有改變tensor的形狀,那么softmax究竟對tensor做了什么?

答案就是softmax會以某一個軸的下標為索引,對這一軸上其他維度的值進行 激活 + 歸一化處理。

一般來說,這個索引軸都是表示類別的那個維度(tf.nn.softmax中默認為axis=-1,也就是最后一個維度)

舉例:

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
c = np.random.randn(2,3)
print(c)
# 假設第0維是類別,一共有里兩種類別
cc = softmax(c,axis=0)
# 假設最后一維是類別,一共有3種類別
ccc = softmax(c,axis=-1)
print(cc)
print(ccc)

結果:

c:
[[-1.30022268 0.59127472 1.21384177]
 [ 0.1981082 -0.83686108 -1.54785864]]
cc:
[[0.1826746 0.80661068 0.94057075]
 [0.8173254 0.19338932 0.05942925]]
ccc:
[[0.0500392 0.33172426 0.61823654]
 [0.65371718 0.23222472 0.1140581 ]]

可以看到,對axis=0的軸做softmax時,輸出結果在axis=0軸上和為1(eg: 0.1826746+0.8173254),同理在axis=1軸上做的話結果的axis=1軸和也為1(eg: 0.0500392+0.33172426+0.61823654)。

這些值是怎么得到的呢?

以cc為例(沿著axis=0做softmax):

以ccc為例(沿著axis=1做softmax):

知道了計算方法,現(xiàn)在我們再來討論一下這些值的實際意義:

cc[0,0]實際上表示這樣一種概率: P( label = 0 | value = [-1.30022268 0.1981082] = c[*,0] ) = 0.1826746

cc[1,0]實際上表示這樣一種概率: P( label = 1 | value = [-1.30022268 0.1981082] = c[*,0] ) = 0.8173254

ccc[0,0]實際上表示這樣一種概率: P( label = 0 | value = [-1.30022268 0.59127472 1.21384177] = c[0]) = 0.0500392

ccc[0,1]實際上表示這樣一種概率: P( label = 1 | value = [-1.30022268 0.59127472 1.21384177] = c[0]) = 0.33172426

ccc[0,2]實際上表示這樣一種概率: P( label = 2 | value = [-1.30022268 0.59127472 1.21384177] = c[0]) = 0.61823654

將他們擴展到更多維的情況:假設c是一個[batch_size , timesteps, categories]的三維tensor

output = tf.nn.softmax(c,axis=-1)

那么 output[1, 2, 3] 則表示 P(label =3 | value = c[1,2] )

以上這篇關于tensorflow softmax函數(shù)用法解析就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • PyTorch中關于tensor.repeat()的使用

    PyTorch中關于tensor.repeat()的使用

    這篇文章主要介紹了PyTorch中關于tensor.repeat()的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • python繪圖時,坐標軸負號顯示不出來的解決

    python繪圖時,坐標軸負號顯示不出來的解決

    這篇文章主要介紹了python繪圖時,坐標軸負號顯示不出來的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • python web.py開發(fā)httpserver解決跨域問題實例解析

    python web.py開發(fā)httpserver解決跨域問題實例解析

    這篇文章主要介紹了python web.py開發(fā)httpserver解決跨域問題實例解析,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • 詳解python執(zhí)行shell腳本創(chuàng)建用戶及相關操作

    詳解python執(zhí)行shell腳本創(chuàng)建用戶及相關操作

    這篇文章主要介紹了python執(zhí)行shell腳本創(chuàng)建用戶及相關操作,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • Python實現(xiàn)矩陣可視化的示例代碼

    Python實現(xiàn)矩陣可視化的示例代碼

    matplotlib中提供了兩個矩陣可視化函數(shù),分別是imshow和matshow,本文主要為大家詳細介紹了如何使用這兩個函數(shù),感興趣的小伙伴可以跟隨小編一起學習一下
    2023-10-10
  • 如何利用Python 快速找到最大文件

    如何利用Python 快速找到最大文件

    現(xiàn)在的電腦差不多都是固態(tài)硬盤了,速度很快,但容量不會太大,經(jīng)常會出現(xiàn)磁盤空間不足的情況,怎么辦,刪除那些不重要的最大的文件是最有效的辦法,這篇文章我們就來介紹介紹了如何利用Python 快速找到最大文件,需要的朋友可以參考一下
    2021-11-11
  • django之從html頁面表單獲取輸入的數(shù)據(jù)實例

    django之從html頁面表單獲取輸入的數(shù)據(jù)實例

    這篇文章主要介紹了django之從html頁面表單獲取輸入的數(shù)據(jù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • 利用一個簡單的例子窺探CPython內(nèi)核的運行機制

    利用一個簡單的例子窺探CPython內(nèi)核的運行機制

    這篇文章主要介紹了利用一個簡單的例子窺探CPython內(nèi)核的運行機制,作者通過一個簡單的輸出函數(shù)深入、介紹了CPython源碼C代碼中的一些函數(shù),需要的朋友可以參考下
    2015-03-03
  • python高斯分布概率密度函數(shù)的使用詳解

    python高斯分布概率密度函數(shù)的使用詳解

    今天小編就為大家分享一篇python高斯分布概率密度函數(shù)的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python連接PostgreSQL數(shù)據(jù)庫的過程詳解

    python連接PostgreSQL數(shù)據(jù)庫的過程詳解

    這篇文章主要介紹了python連接PostgreSQL數(shù)據(jù)庫的過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09

最新評論