Skip to content

Commit

Permalink
more activations
Browse files Browse the repository at this point in the history
  • Loading branch information
julio guillermo mayo vidal committed Aug 15, 2023
1 parent 10e5ab2 commit 3e92258
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
25 changes: 25 additions & 0 deletions activation/elu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package activation

import (
"math"

"github.com/julioguillermo/godeep/types"
)

type ELU[T types.Number] struct {
Alpha T
}

func (p ELU[T]) Activate(n T) T {
if n >= 0 {
return n
}
return p.Alpha * (T(math.Exp(float64(n)) - 1))
}

func (p ELU[T]) Derive(n T) T {
if n >= 0 {
return n
}
return p.Activate(n) + p.Alpha
}
23 changes: 23 additions & 0 deletions activation/gelu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package activation

import (
"math"

"github.com/julioguillermo/godeep/types"
)

type GELU[T types.Number] struct{}

func (GELU[T]) Activate(n T) T {
x := float64(n)
return T(0.5 * x * (1 + math.Tanh(math.Sqrt(2/math.Pi)*(x+0.044715*math.Pow(x, 3)))))
}

func (p GELU[T]) Derive(x T) T {
h := 0.000001
H := T(h)
if H == 0 {
H++
}
return (p.Activate(x+H) - p.Activate(x-H)) / (2 * H)
}
6 changes: 3 additions & 3 deletions activation/relu.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import (
)

type Relu[T types.Number] struct {
M T
Alpha T
}

func (p Relu[T]) Activate(t T) T {
if t < 0 {
return t * p.M
return t * p.Alpha
}
return t
}

func (p Relu[T]) Derive(t T) T {
if t < 0 {
return p.M
return p.Alpha
}
return 1
}
18 changes: 18 additions & 0 deletions activation/selu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package activation

import (
"github.com/julioguillermo/godeep/types"
)

type SELU[T types.Number] struct {
ELU[T]
Lambda T
}

func (p SELU[T]) Activate(n T) T {
return p.Lambda * p.ELU.Activate(n)
}

func (p SELU[T]) Derive(n T) T {
return p.Lambda * p.ELU.Derive(n)
}
17 changes: 17 additions & 0 deletions activation/swish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package activation

import (
"github.com/julioguillermo/godeep/types"
)

type Swish[T types.Number] struct {
Sigmoid[T]
}

func (p Swish[T]) Activate(t T) T {
return t * p.Sigmoid.Activate(t)
}

func (p Swish[T]) Derive(t T) T {
return p.Activate(t) + p.Sigmoid.Activate(t)*(1-p.Activate(t))
}

0 comments on commit 3e92258

Please sign in to comment.