-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_Softmax_.php
More file actions
50 lines (46 loc) · 1.06 KB
/
_Softmax_.php
File metadata and controls
50 lines (46 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/**
* _Chain_ ©2015 Julien <youlweb@hotmail.com>
* Refer to the LICENSE file for the full copyright and license information.
* @package chain/math
*/
namespace _Chain_\_Math_;
use _Chain_\_AbsLink_;
use _Chain_\I_O;
use _Chain_\Type;
/**
* Represents the categorical probability distribution of a vector.
*
* @author Julien <youlweb@hotmail.com>
*/
class _Softmax_ extends _AbsLink_
{
/**
* Represents the categorical probability distribution of a vector.
*
* I/O contract
* ------------
* <pre>
* I multi Array of numbers.
* O multi Probability distribution.
* X no
* </pre>
*
* @param I_O $IO
* @return I_O
*/
public function EXE(I_O $IO)
{
$sum = 0;
$values = $IO->I_(Type::MULTI);
foreach ($values as &$value) {
$value = exp($value);
$sum += $value;
}
return $IO->_O(
array_map(function ($value) use ($sum) {
return $value / $sum;
}, $values)
);
}
}