blob: 51ab925a412a71dd1a89835dfe47e0a4fae7f2e7 [file] [log] [blame]
#-------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
#-------------------------------------------------------------
/*
* Exponential Linear Units (ELUs) nonlinearity layer.
*/
forward = function(matrix[double] X, int alpha)
return (matrix[double] out) {
/*
* Computes the forward pass for a ELUs nonlinearity layer.
* Reference paper https://arxiv.org/abs/1511.07289v1
* Performs an element-wise evaluation of
* `f(x) = x if x ≥ 0 else α (exp(x) − 1)`.
*
* Inputs:
* - X: Inputs, of shape (any, any).
* - alpha: Input, minimum value that the ELU can reach
* Typical value 1
*
* Outputs:
* - out: Outputs, of same shape as `X`.
*/
out = max(0, X) + min(0, alpha * (exp(X) - 1))
}
backward = function(matrix[double] dout, matrix[double] X, int alpha)
return (matrix[double] dX) {
/*
* Computes the backward pass for a ELU nonlinearity layer.
*
* Inputs:
* - dout: Gradient wrt `out` from upstream, of same shape as `X`.
* - X: Previous input data matrix, of shape (any, any).
* - alpha: Minimum value that the ELU can reach
* Typical value 1
*
* Outputs:
* - dX: Gradient wrt `X`, of same shape as `X`.
*/
dX = ((X > 0) + (X < 0) * (alpha * exp(X))) * dout
}