Skip to content

Commit 9119f26

Browse files
Merge pull request #377 from apphp/SAM-10-SELU
Sam 10 selu and sigmoid
2 parents be0b9ff + 7b0479a commit 9119f26

File tree

13 files changed

+581
-51
lines changed

13 files changed

+581
-51
lines changed

docs/neural-network/activation-functions/elu.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ $activationFunction = new ELU(2.5);
3232
```
3333

3434
## References
35-
[^1]: D. A. Clevert et al. (2016). Fast and Accurate Deep Network Learning by Exponential Linear Units.
35+
[1]: D. A. Clevert et al. (2016). Fast and Accurate Deep Network Learning by Exponential Linear Units.

docs/neural-network/activation-functions/hard-sigmoid.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ $activationFunction = new HardSigmoid();
2626
```
2727

2828
## References
29-
[^1]: https://en.wikipedia.org/wiki/Hard_sigmoid
29+
[1]: https://en.wikipedia.org/wiki/Hard_sigmoid

docs/neural-network/activation-functions/leaky-relu.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ $activationFunction = new LeakyReLU(0.3);
3232
```
3333

3434
## References
35-
[^1]: A. L. Maas et al. (2013). Rectifier Nonlinearities Improve Neural Network Acoustic Models.
35+
[1]: A. L. Maas et al. (2013). Rectifier Nonlinearities Improve Neural Network Acoustic Models.

docs/neural-network/activation-functions/relu.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ $activationFunction = new ReLU(0.1);
3030
```
3131

3232
## References
33-
[^1]: A. L. Maas et al. (2013). Rectifier Nonlinearities Improve Neural Network Acoustic Models.
34-
[^2]: K. Konda et al. (2015). Zero-bias Autoencoders and the Benefits of Co-adapting Features.
33+
[1]: A. L. Maas et al. (2013). Rectifier Nonlinearities Improve Neural Network Acoustic Models.
34+
[2]: K. Konda et al. (2015). Zero-bias Autoencoders and the Benefits of Co-adapting Features.

docs/neural-network/activation-functions/selu.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ $activationFunction = new SELU();
3434
```
3535

3636
## References
37-
[^1]: G. Klambauer et al. (2017). Self-Normalizing Neural Networks.
37+
[1]: G. Klambauer et al. (2017). Self-Normalizing Neural Networks.

docs/neural-network/activation-functions/silu.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ use Rubix\ML\NeuralNet\ActivationFunctions\SiLU;
2828
$activationFunction = new SiLU();
2929
```
3030

31-
### References
32-
[^1]: S. Elwing et al. (2017). Sigmoid-Weighted Linear Units for Neural Network Function Approximation in Reinforcement Learning.
31+
## References
32+
[1]: S. Elwing et al. (2017). Sigmoid-Weighted Linear Units for Neural Network Function Approximation in Reinforcement Learning.

docs/neural-network/activation-functions/softplus.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ $activationFunction = new Softplus();
2626
```
2727

2828
## References
29-
[^1]: X. Glorot et al. (2011). Deep Sparse Rectifier Neural Networks.
29+
[1]: X. Glorot et al. (2011). Deep Sparse Rectifier Neural Networks.

docs/neural-network/activation-functions/softsign.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ $activationFunction = new Softsign();
2626
```
2727

2828
## References
29-
[^1]: X. Glorot et al. (2010). Understanding the Difficulty of Training Deep Feedforward Neural Networks.
29+
[1]: X. Glorot et al. (2010). Understanding the Difficulty of Training Deep Feedforward Neural Networks.

docs/neural-network/activation-functions/thresholded-relu.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ $activationFunction = new ThresholdedReLU(2.0);
3232
```
3333

3434
## References
35-
[^1]: K. Konda et al. (2015). Zero-bias autoencoders and the benefits of co-adapting features.
35+
[1]: K. Konda et al. (2015). Zero-bias autoencoders and the benefits of co-adapting features.

src/NeuralNet/ActivationFunctions/SELU/SELU.php

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Rubix\ML\NeuralNet\ActivationFunctions\SELU;
46

5-
use Tensor\Matrix;
7+
use NumPower;
8+
use NDArray;
9+
use Rubix\ML\NeuralNet\ActivationFunctions\Base\Contracts\ActivationFunction;
10+
use Rubix\ML\NeuralNet\ActivationFunctions\Base\Contracts\IBufferDerivative;
611

712
/**
813
* SELU
@@ -18,69 +23,94 @@
1823
* @category Machine Learning
1924
* @package Rubix/ML
2025
* @author Andrew DalPino
26+
* @author Samuel Akopyan <leumas.a@gmail.com>
2127
*/
22-
class SELU implements ActivationFunction
28+
class SELU implements ActivationFunction, IBufferDerivative
2329
{
2430
/**
2531
* The value at which leakage starts to saturate.
2632
*
2733
* @var float
2834
*/
29-
public const ALPHA = 1.6732632423543772848170429916717;
35+
public const ALPHA = 1.6732632;
3036

3137
/**
3238
* The scaling coefficient.
3339
*
3440
* @var float
3541
*/
36-
public const SCALE = 1.0507009873554804934193349852946;
42+
public const LAMBDA = 1.0507009;
3743

3844
/**
3945
* The scaling coefficient multiplied by alpha.
4046
*
4147
* @var float
4248
*/
43-
protected const BETA = self::SCALE * self::ALPHA;
49+
protected const BETA = self::LAMBDA * self::ALPHA;
4450

4551
/**
4652
* Compute the activation.
4753
*
48-
* @internal
54+
* f(x) = λ * x if x > 0
55+
* f(x) = λ * α * (e^x - 1) if x ≤ 0
4956
*
50-
* @param Matrix $input
51-
* @return Matrix
57+
* @param NDArray $input The input values
58+
* @return NDArray The activated values
5259
*/
53-
public function activate(Matrix $input) : Matrix
60+
public function activate(NDArray $input) : NDArray
5461
{
55-
$positive = NumPower::maximum($input, 0) * self::SCALE;
56-
$negative = self::BETA * NumPower::expm1($input);
62+
// Calculate positive part: λ * x for x > 0
63+
$positive = NumPower::multiply(
64+
self::LAMBDA,
65+
NumPower::maximum($input, 0)
66+
);
67+
68+
// Calculate negative part: λ * α * (e^x - 1) for x <= 0
69+
$negativeMask = NumPower::minimum($input, 0);
70+
$negative = NumPower::multiply(
71+
self::BETA,
72+
NumPower::expm1($negativeMask)
73+
);
5774

58-
return $negative + $positive;
75+
// Combine both parts
76+
return NumPower::add($positive, $negative);
5977
}
6078

6179
/**
62-
* Calculate the derivative of the activation.
80+
* Calculate the derivative of the SELU activation function.
6381
*
64-
* @internal
82+
* f'(x) = λ if x > 0
83+
* f'(x) = λ * α * e^x if x ≤ 0
6584
*
66-
* @param Matrix $input
67-
* @param Matrix $output
68-
* @return Matrix
85+
* @param NDArray $input Input matrix
86+
* @return NDArray Derivative matrix
6987
*/
70-
public function differentiate(Matrix $input, Matrix $output) : Matrix
88+
public function differentiate(NDArray $input) : NDArray
7189
{
72-
$positive = NumPower::greater($output, 0) * self::SCALE;
73-
$negative = NumPower::lessEqual($output) * ($output + self::ALPHA) * self::SCALE;
90+
// For x > 0: λ
91+
$positiveMask = NumPower::greater($input, 0);
92+
$positivePart = NumPower::multiply($positiveMask, self::LAMBDA);
7493

75-
return $positive + $negative;
94+
// For x <= 0: λ * α * e^x
95+
$negativeMask = NumPower::lessEqual($input, 0);
96+
$negativePart = NumPower::multiply(
97+
NumPower::multiply(
98+
NumPower::exp(
99+
NumPower::multiply($negativeMask, $input)
100+
),
101+
self::BETA
102+
),
103+
$negativeMask
104+
);
105+
106+
// Combine both parts
107+
return NumPower::add($positivePart, $negativePart);
76108
}
77109

78110
/**
79-
* Return the string representation of the object.
80-
*
81-
* @internal
111+
* Return the string representation of the activation function.
82112
*
83-
* @return string
113+
* @return string String representation
84114
*/
85115
public function __toString() : string
86116
{

0 commit comments

Comments
 (0)