LoginSignup
8
7

More than 5 years have passed since last update.

ソフトマックス関数実装したヅラ

Posted at

ソフトマックス関数とは

分類問題等でよく使われる活性化関数
なぜなら、正解ラベルへの推論を確率で割り振ってくれるため。
例)
mnistで手書きの8に対してのソフトマックス
[0.05, 0.01, 0.04, 0.1, 0.02, 0.05, 0.2, 0.03, 0.4, 0.1]

左の要素から数字の0,1,2,....9の予測確率に対応(4割の確率で8だと予測している)
すべての要素を足すと1になる。

実装

softmax.py
# coding: UTF-8
import numpy as np

# ソフトマックス関数
def softmax(a):
    # 一番大きい値を取得
    c = np.max(a)
    # 各要素から一番大きな値を引く(オーバーフロー対策)
    exp_a = np.exp(a - c)
    sum_exp_a = np.sum(exp_a)
    # 要素の値/全体の要素の合計
    y = exp_a / sum_exp_a

    return y 


a = [23.0, 0.94, 5.46]
print (softmax(a))
# [  9.99999976e-01   2.62702205e-10   2.41254141e-08]

参考文献
ゼロから作るDeep Learning

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7