| 1 | package Peta::NN::Layer::Activation; | ||||||||||
| 2 | # ABSTRACT: relu, tanh and sigmoid | ||||||||||
| 3 | |||||||||||
| 4 | # Element-wise non-linearities. Each derivative is taken in terms of the | ||||||||||
| 5 | # OUTPUT, so the layer keeps only what it returned. | ||||||||||
| 6 | |||||||||||
| 7 | 27 | ▶ | use v5.36; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 27 | our $VERSION = '0.2610090'; | |||||||||
| 10 | |||||||||||
| 11 | 27 | my %KIND = map { $_ => 1 } qw(relu tanh sigmoid); | |||||||||
| 12 | |||||||||||
| 13 | 27 | ▶ | sub kinds { return sort keys %KIND } | ||||||||
|
Subroutine
kinds: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 14 | |||||||||||
| 15 | 152 | ▶ | sub new ($class, %arg) { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 16 | 152 | ▶ | die "unknown activation '$arg{kind}'\n" if !$KIND{ $arg{kind} }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 152 | return bless { kind => $arg{kind} }, $class; | |||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | 1584 | ▶ | sub type ($self) { return $self->{kind} } | ||||||||
|
Subroutine
type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 21 | |||||||||||
| 22 | 152 | ▶ | sub init ($self, $n_in, $rng, $backend) { | ||||||||
|
Subroutine
init: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 23 | 152 | ▶ | die "$self->{kind} cannot follow a token input directly\n" if ref $n_in; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 152 | $self->{backend} = $backend; | |||||||||
| 25 | 152 | return $n_in; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 17806 | ▶ | sub param_names ($self) { return } | ||||||||
|
Subroutine
param_names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 29 | |||||||||||
| 30 | 95909 | ▶ | sub forward ($self, $X) { | ||||||||
|
Subroutine
forward: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 31 | 95909 | return $self->{Y} = $self->{backend}->activate($self->{kind}, $X); | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 93142 | ▶ | sub backward ($self, $dY) { | ||||||||
|
Subroutine
backward: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 35 | 93142 | return $self->{backend}->activate_grad($self->{kind}, $self->{Y}, $dY); | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 33 | ▶ | sub spec ($self) { return $self->{kind} } | ||||||||
|
Subroutine
spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 39 | |||||||||||
| 40 | 27 | 1; | |||||||||
| 41 | |||||||||||
| 42 | __END__ | ||||||||||