最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關于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ù)用法解析就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • python導出chrome書簽到markdown文件的實例代碼

    python導出chrome書簽到markdown文件的實例代碼

    python導出chrome書簽到markdown文件,主要就是解析chrome的bookmarks文件,然后拼接成markdown格式的字符串,最后輸出到文件即可。下面給大家分享實例代碼,需要的朋友參考下
    2017-12-12
  • python使用append合并兩個數(shù)組的方法

    python使用append合并兩個數(shù)組的方法

    這篇文章主要介紹了python使用append合并兩個數(shù)組的方法,涉及Python中append方法的使用技巧,需要的朋友可以參考下
    2015-04-04
  • Python音頻處理庫pydub的使用示例詳解

    Python音頻處理庫pydub的使用示例詳解

    pydub是一個輕量級的音頻處理庫,安裝方便,使用簡單,這篇文章主要為大家詳細介紹了pydub的具體使用,文中的示例代碼講解詳細,需要的小伙伴可以參考下
    2023-11-11
  • Python通過OpenPyXL處理Excel的完整教程

    Python通過OpenPyXL處理Excel的完整教程

    OpenPyXL是一個強大的Python庫,用于處理Excel文件,允許讀取、編輯和創(chuàng)建Excel工作簿和工作表,本文將詳細介紹OpenPyXL的各種功能,希望對大家有所幫助
    2023-11-11
  • python 高效去重復 支持GB級別大文件的示例代碼

    python 高效去重復 支持GB級別大文件的示例代碼

    今天小編就為大家分享一篇python 高效去重復 支持GB級別大文件的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python os模塊在系統(tǒng)管理中的應用

    python os模塊在系統(tǒng)管理中的應用

    這篇文章主要介紹了python os模塊在系統(tǒng)管理中的應用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • Python使用國內鏡像加速pip安裝的方法講解

    Python使用國內鏡像加速pip安裝的方法講解

    在Python開發(fā)中,pip是一個非常重要的工具,用于安裝和管理Python的第三方庫,然而,在國內使用pip安裝依賴時,往往會因為網(wǎng)絡問題而導致速度緩慢甚至超時,為了解決這個問題,本文將詳細介紹如何使用-i參數(shù)配置國內鏡像源,加速pip的安裝過程,需要的朋友可以參考下
    2025-02-02
  • Python繪制3d螺旋曲線圖實例代碼

    Python繪制3d螺旋曲線圖實例代碼

    這篇文章主要介紹了Python繪制3d螺旋曲線圖實例代碼,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • python 實現(xiàn)矩陣上下/左右翻轉,轉置的示例

    python 實現(xiàn)矩陣上下/左右翻轉,轉置的示例

    今天小編就為大家分享一篇python 實現(xiàn)矩陣上下/左右翻轉,轉置的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • 給Python中的MySQLdb模塊添加超時功能的教程

    給Python中的MySQLdb模塊添加超時功能的教程

    這篇文章主要介紹了給Python中的MySQLdb模塊添加超時功能的教程,timeout功能在服務器的運維當中非常有用,需要的朋友可以參考下
    2015-05-05

最新評論

兰坪| 沐川县| 延庆县| 涞源县| 建水县| 都江堰市| 盘山县| 九江市| 都兰县| 石狮市| 钦州市| 博白县| 本溪| 托克逊县| 沙坪坝区| 海门市| 全州县| 咸阳市| 三明市| 肥城市| 韶山市| 高州市| 丹巴县| 宜君县| 昭觉县| 资溪县| 胶南市| 禹州市| 瑞昌市| 社会| 桂平市| 徐水县| 渝中区| 景泰县| 琼海市| 顺平县| 靖西县| 喀喇沁旗| 库车县| 鹤壁市| 如皋市|