LoginSignup
1
2

More than 5 years have passed since last update.

Lua版 ゼロから作るDeep Learning その4[ソフトマックス関数の実装]

Last updated at Posted at 2017-06-10

過去記事

Lua版 ゼロから作るDeep Learning その1[パーセプトロンの実装]
Lua版 ゼロから作るDeep Learning その2[活性化関数]
Lua版 ゼロから作るDeep Learning その3[3層ニューラルネットワークの実装]

ソフトマックス関数の実装

 今回はソフトマックス関数の実装です。

 スクリプトは以下の通りです。

softmax.lua
---ソフトマックス関数.
-- 入力値を確率に変換する
-- @param x 入力 (Type:torch.DoubleTensor)
-- @return 0-1 (Type:number)
function softmax(a)
    local c = torch.max(a)
    local exp_a = torch.exp(a - c)
    local sum_exp_a = torch.sum(exp_a)
    local y = exp_a / sum_exp_a

    return y
end

local a = torch.Tensor({0.3, 2.9, 4.0})
local y = softmax(a)
print(y)
print(torch.sum(y))
実行例
$ th softmax.lua
 0.0182
 0.2452
 0.7366
[torch.DoubleTensor of size 3]

1

 これはpython の場合とほとんど変わりません。

おわりに

 今回は以上です。

 次回はいよいよMNISTの手書き文字認識をします。
 
 ありがとうございました。

1
2
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
1
2