| 1 | # Licensed to the Apache Software Foundation (ASF) under one | ||
| 2 | # or more contributor license agreements. See the NOTICE file | ||
| 3 | # distributed with this work for additional information | ||
| 4 | # regarding copyright ownership. The ASF licenses this file | ||
| 5 | # to you under the Apache License, Version 2.0 (the | ||
| 6 | # "License"); you may not use this file except in compliance | ||
| 7 | # with the License. You may obtain a copy of the License at | ||
| 8 | # | ||
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | # | ||
| 11 | # Unless required by applicable law or agreed to in writing, | ||
| 12 | # software distributed under the License is distributed on an | ||
| 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| 14 | # KIND, either express or implied. See the License for the | ||
| 15 | # specific language governing permissions and limitations | ||
| 16 | # under the License. | ||
| 17 | |||
| 18 | package AI::MXNet::Gluon::NN::Activation; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use AI::MXNet::Function::Parameters; | ||
| 22 | |||
| 23 | =head1 | ||
| 24 | |||
| 25 | AI::MXNet::Gluon::NN::Activation | ||
| 26 | =cut | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | Applies an activation function to input. | ||
| 31 | |||
| 32 | Parameters | ||
| 33 | ---------- | ||
| 34 | activation : str | ||
| 35 | Name of activation function to use. | ||
| 36 | See mxnet.ndarray.Activation for available choices. | ||
| 37 | |||
| 38 | Input shape: | ||
| 39 | Arbitrary. | ||
| 40 | |||
| 41 | Output shape: | ||
| 42 | Same shape as input. | ||
| 43 | =cut | ||
| 44 | |||
| 45 | use AI::MXNet::Gluon::Mouse; | ||
| 46 | extends 'AI::MXNet::Gluon::HybridBlock'; | ||
| 47 | has 'activation' => (is => 'ro', isa => 'Str', required => 1); | ||
| 48 | |||
| 49 | method python_constructor_arguments() | ||
| 50 | { | ||
| 51 | ['activation']; | ||
| 52 | } | ||
| 53 | |||
| 54 | method _alias() | ||
| 55 | { | ||
| 56 | return $self->activation; | ||
| 57 | } | ||
| 58 | |||
| 59 | method hybrid_forward(GluonClass $F, GluonInput $x) | ||
| 60 | { | ||
| 61 | return $F->Activation($x, act_type => $self->activation, name=>'fwd'); | ||
| 62 | } | ||
| 63 | |||
| 64 | use overload '""' => sub { my $self = shift; "${\ $self->_class_name }(${\ $self->activation })"; }; | ||
| 65 | |||
| 66 | __PACKAGE__->register('AI::MXNet::Gluon::NN'); | ||
| 67 | |||
| 68 | package AI::MXNet::Gluon::NN::LeakyReLU; | ||
| 69 | =head1 | ||
| 70 | |||
| 71 | AI::MXNet::Gluon::NN::LeakyReLU - Leaky version of a Rectified Linear Unit. | ||
| 72 | =cut | ||
| 73 | |||
| 74 | =head1 DESCRIPTION | ||
| 75 | |||
| 76 | Leaky version of a Rectified Linear Unit. | ||
| 77 | |||
| 78 | It allows a small gradient when the unit is not active | ||
| 79 | |||
| 80 | Parameters | ||
| 81 | ---------- | ||
| 82 | alpha : float | ||
| 83 | slope coefficient for the negative half axis. Must be >= 0. | ||
| 84 | =cut | ||
| 85 | |||
| 86 | use AI::MXNet::Gluon::Mouse; | ||
| 87 | extends 'AI::MXNet::Gluon::HybridBlock'; | ||
| 88 | has 'alpha' => (is => 'ro', isa => 'Num', required => 1); | ||
| 89 | |||
| 90 | method python_constructor_arguments() | ||
| 91 | { | ||
| 92 | ['alpha']; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub BUILD | ||
| 96 | { | ||
| 97 | confess('Slope coefficient for LeakyReLU must be no less than 0') | ||
| 98 | unless shift->alpha > 0; | ||
| 99 | } | ||
| 100 | |||
| 101 | method hybrid_forward(GluonClass $F, GluonInput $x) | ||
| 102 | { | ||
| 103 | return $F->LeakyReLU($x, act_type => 'leaky', slope => $self->alpha, name=>'fwd'); | ||
| 104 | } | ||
| 105 | |||
| 106 | use overload '""' => sub { my $self = shift; "${\ $self->_class_name }(${\ $self->alpha })"; }; | ||
| 107 | |||
| 108 | __PACKAGE__->register('AI::MXNet::Gluon::NN'); | ||
| 109 | |||
| 110 | package AI::MXNet::Gluon::NN::PReLU; | ||
| 111 | =head1 | ||
| 112 | |||
| 113 | AI::MXNet::Gluon::NN::PReLU - Parametric leaky version of a Rectified Linear Unit. | ||
| 114 | =cut | ||
| 115 | |||
| 116 | =head1 DESCRIPTION | ||
| 117 | |||
| 118 | Parametric leaky version of a Rectified Linear Unit. | ||
| 119 | https://arxiv.org/abs/1502.01852 | ||
| 120 | |||
| 121 | It learns a gradient when the unit is not active | ||
| 122 | |||
| 123 | Parameters | ||
| 124 | ---------- | ||
| 125 | alpha_initializer : Initializer | ||
| 126 | Initializer for the embeddings matrix. | ||
| 127 | =cut | ||
| 128 | |||
| 129 | use AI::MXNet::Gluon::Mouse; | ||
| 130 | extends 'AI::MXNet::Gluon::HybridBlock'; | ||
| 131 | has 'alpha_initializer' => (is => 'ro', isa => 'Initializer', default => sub { AI::MXNet::Constant->new(0.25) }); | ||
| 132 | |||
| 133 | method python_constructor_arguments() | ||
| 134 | { | ||
| 135 | ['alpha_initializer']; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub BUILD | ||
| 139 | { | ||
| 140 | my $self = shift; | ||
| 141 | $self->name_scope(sub { | ||
| 142 | $self->alpha($self->params->get('alpha', shape=>[1], init=>$self->alpha_initializer)); | ||
| 143 | }); | ||
| 144 | } | ||
| 145 | |||
| 146 | method hybrid_forward(GluonClass $F, GluonInput $x, GluonInput :$alpha) | ||
| 147 | { | ||
| 148 | return $F->LeakyReLU($x, gamma => $alpha, act_type => 'prelu', name=>'fwd'); | ||
| 149 | } | ||
| 150 | |||
| 151 | __PACKAGE__->register('AI::MXNet::Gluon::NN'); | ||
| 152 | |||
| 153 | package AI::MXNet::Gluon::NN::ELU; | ||
| 154 | =head1 | ||
| 155 | |||
| 156 | AI::MXNet::Gluon::NN::ELU - Exponential Linear Unit (ELU) | ||
| 157 | =cut | ||
| 158 | |||
| 159 | =head1 DESCRIPTION | ||
| 160 | |||
| 161 | Exponential Linear Unit (ELU) | ||
| 162 | "Fast and Accurate Deep Network Learning by Exponential Linear Units", Clevert et al, 2016 | ||
| 163 | https://arxiv.org/abs/1511.07289 | ||
| 164 | Published as a conference paper at ICLR 2016 | ||
| 165 | |||
| 166 | Parameters | ||
| 167 | ---------- | ||
| 168 | alpha : float | ||
| 169 | The alpha parameter as described by Clevert et al, 2016 | ||
| 170 | =cut | ||
| 171 | |||
| 172 | use AI::MXNet::Gluon::Mouse; | ||
| 173 | extends 'AI::MXNet::Gluon::HybridBlock'; | ||
| 174 | has 'alpha' => (is => 'ro', isa => 'Num', default => 1); | ||
| 175 | |||
| 176 | method python_constructor_arguments() | ||
| 177 | { | ||
| 178 | ['alpha']; | ||
| 179 | } | ||
| 180 | |||
| 181 | method hybrid_forward(GluonClass $F, GluonInput $x) | ||
| 182 | { | ||
| 183 | return $F->where($x > 0, $x, $self->alpha * ($F->exp($x) - 1)); | ||
| 184 | } | ||
| 185 | |||
| 186 | __PACKAGE__->register('AI::MXNet::Gluon::NN'); | ||
| 187 | |||
| 188 | package AI::MXNet::Gluon::NN::SELU; | ||
| 189 | =head1 | ||
| 190 | |||
| 191 | AI::MXNet::Gluon::NN::SELU - Scaled Exponential Linear Unit (SELU) | ||
| 192 | =cut | ||
| 193 | |||
| 194 | =head1 DESCRIPTION | ||
| 195 | |||
| 196 | Scaled Exponential Linear Unit (SELU) | ||
| 197 | "Self-Normalizing Neural Networks", Klambauer et al, 2017 | ||
| 198 | https://arxiv.org/abs/1706.02515 | ||
| 199 | =cut | ||
| 200 | |||
| 201 | use AI::MXNet::Gluon::Mouse; | ||
| 202 | extends 'AI::MXNet::Gluon::HybridBlock'; | ||
| 203 | |||
| 204 | method hybrid_forward(GluonClass $F, GluonInput $x) | ||
| 205 | { | ||
| 206 | $F->LeakyReLU($x, act_type=>'selu', name=>'fwd'); | ||
| 207 | } | ||
| 208 | |||
| 209 | __PACKAGE__->register('AI::MXNet::Gluon::NN'); | ||
| 210 | |||
| 211 | package AI::MXNet::Gluon::NN::Swish; | ||
| 212 | =head1 | ||
| 213 | |||
| 214 | AI::MXNet::Gluon::NN::Swish - Swish Activation function | ||
| 215 | =cut | ||
| 216 | |||
| 217 | =head1 DESCRIPTION | ||
| 218 | |||
| 219 | Swish Activation function | ||
| 220 | https://arxiv.org/pdf/1710.05941.pdf | ||
| 221 | |||
| 222 | Parameters | ||
| 223 | ---------- | ||
| 224 | beta : float | ||
| 225 | swish(x) = x * sigmoid(beta*x) | ||
| 226 | =cut | ||
| 227 | |||
| 228 | use AI::MXNet::Gluon::Mouse; | ||
| 229 | extends 'AI::MXNet::Gluon::HybridBlock'; | ||
| 230 | has 'beta' => (is => 'ro', isa => 'Num', default => 1); | ||
| 231 | |||
| 232 | method python_constructor_arguments() | ||
| 233 | { | ||
| 234 | ['beta']; | ||
| 235 | } | ||
| 236 | |||
| 237 | method hybrid_forward(GluonClass $F, GluonInput $x) | ||
| 238 | { | ||
| 239 | return return $x * $F->sigmoid($self->beta * $x, name=>'fwd'); | ||
| 240 | } | ||
| 241 | |||
| 242 | __PACKAGE__->register('AI::MXNet::Gluon::NN'); | ||
| 243 | |||
| 244 | 1; |