-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #813 from nlmixr2/812-r-nn-interface
Create R NN activation interface
- Loading branch information
Showing
35 changed files
with
2,247 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#' Exponential Linear Unit (ELU) Activation Function | ||
#' | ||
#' @family Activation Functions | ||
#' @param x A numeric vector. All elements must be finite and | ||
#' non-missing. | ||
#' @param alpha A numeric scalar. All elements must be finite and | ||
#' non-missing. | ||
#' @return A numeric vector where the ReLU function has been applied | ||
#' to each element of `x`. | ||
#' @author Matthew Fidler | ||
#' @export | ||
#' @examples | ||
#' | ||
#' ELU(c(-1, 0, 1, 2), 2) | ||
#' | ||
#' # Can also be used in rxode2: | ||
#' x <- rxode2({ | ||
#' r=SELU(time) | ||
#' }) | ||
#' | ||
#' e <- et(c(-1, 0, 1, 2)) | ||
#' | ||
#' rxSolve(x, e) | ||
#' | ||
ELU <- function(x, alpha=1) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
checkmate::assertNumeric(alpha, finite=TRUE, any.missing=FALSE) | ||
.df <- data.frame(x=x, alpha=alpha) | ||
.Call(`_rxode2_activationF2`, .df$x, .df$alpha, 1L) | ||
} | ||
#' Derivatives of the Exponential Linear Unit (ELU) Activation Function | ||
#' | ||
#' | ||
#' @param x A numeric vector. All elements must be finite and | ||
#' non-missing. | ||
#' @param alpha A numeric scalar. All elements must be finite and | ||
#' non-missing. | ||
#' @return A numeric vector where the derivative(s) of the ELU function has been applied | ||
#' to each element of `x`. | ||
#' @export | ||
#' @author Matthew L. Fidler | ||
#' @family Activation Functions | ||
#' @examples | ||
#' dELU(c(-1, 0, 1, 2), 2) | ||
#' d2ELU(c(-1, 0, 1, 2), 2) | ||
#' d2aELU(c(-1, 0, 1, 2), 2) | ||
#' dELUa(c(-1, 0, 1, 2), 2) | ||
#' d2ELUa(c(-1, 0, 1, 2), 2) | ||
#' | ||
#' # Can also be used in rxode2: | ||
#' r <- rxode2({ | ||
#' r1=dELU(time, 2) | ||
#' r2=d2ELU(time, 2) | ||
#' r2a=d2aELU(time, 2) | ||
#' ra=dELUa(time, 2) | ||
#' r2a=d2ELUa(time, 2) | ||
#' }) | ||
#' | ||
#' e <- et(c(-1, 0, 1, 2)) | ||
#' rxSolve(r, e) | ||
dELU <- function(x, alpha=1) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
checkmate::assertNumeric(alpha, finite=TRUE, any.missing=FALSE) | ||
.df <- data.frame(x=x, alpha=alpha) | ||
.Call(`_rxode2_activationF2`, .df$x, .df$alpha, 2L) | ||
} | ||
|
||
#' @rdname dELU | ||
#' @export | ||
d2ELU <- function(x, alpha=1) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
checkmate::assertNumeric(alpha, finite=TRUE, any.missing=FALSE) | ||
.df <- data.frame(x=x, alpha=alpha) | ||
.Call(`_rxode2_activationF2`, .df$x, .df$alpha, 3L) | ||
} | ||
|
||
#' @rdname dELU | ||
#' @export | ||
d2aELU <- function(x, alpha=1) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
checkmate::assertNumeric(alpha, finite=TRUE, any.missing=FALSE) | ||
.df <- data.frame(x=x, alpha=alpha) | ||
.Call(`_rxode2_activationF2`, .df$x, .df$alpha, 4L) | ||
} | ||
|
||
#' @rdname dELU | ||
#' @export | ||
dELUa <- function(x, alpha=1) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
checkmate::assertNumeric(alpha, finite=TRUE, any.missing=FALSE) | ||
.df <- data.frame(x=x, alpha=alpha) | ||
.Call(`_rxode2_activationF2`, .df$x, .df$alpha, 5L) | ||
} | ||
|
||
#' @rdname dELU | ||
#' @export | ||
d2ELUa <- function(x, alpha=1) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
checkmate::assertNumeric(alpha, finite=TRUE, any.missing=FALSE) | ||
.df <- data.frame(x=x, alpha=alpha) | ||
.Call(`_rxode2_activationF2`, .df$x, .df$alpha, 6L) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
|
||
#' GELU activation function | ||
#' @param x numeric vector | ||
#' @return numeric vector | ||
#' @family Activation Functions | ||
#' @export | ||
#' @examples | ||
#' | ||
#' GELU(c(-2, -1, 0, 1, 2)) | ||
#' | ||
#' # you can use rxode2 as well | ||
#' r <- rxode2({ | ||
#' r = GELU(time) | ||
#' }) | ||
#' et <- et(c(-2, -1, 0, 1, 2)) | ||
#' rxSolve(r, et) | ||
#' | ||
GELU <- function(x) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
.Call(`_rxode2_activationF`, x, 1L) | ||
} | ||
|
||
|
||
#' Derivatives of GELU | ||
#' | ||
#' @param x numeric vector | ||
#' @return numeric vector | ||
#' @family Activation Functions | ||
#' @export | ||
#' @examples | ||
#' dGELU(c(-2, -1, 0, 1, 2)) | ||
#' d2GELU(c(-2, -1, 0, 1, 2)) | ||
#' d3GELU(c(-2, -1, 0, 1, 2)) | ||
#' d4GELU(c(-2, -1, 0, 1, 2)) | ||
#' # you can use rxode2 as well | ||
#' r <- rxode2({ | ||
#' r1 <- dGELU(time) | ||
#' r2 <- d2GELU(time) | ||
#' r3 <- d3GELU(time) | ||
#' r4 <- d4GELU(time) | ||
#' }) | ||
#' et <- et(c(-2, -1, 0, 1, 2)) | ||
#' rxSolve(r, et) | ||
dGELU <- function(x) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
.Call(`_rxode2_activationF`, x, 9L) | ||
} | ||
|
||
#' @rdname dGELU | ||
#' @export | ||
d2GELU <- function(x) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
.Call(`_rxode2_activationF`, x, 10L) | ||
} | ||
|
||
#' @rdname dGELU | ||
#' @export | ||
d3GELU <- function(x) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
.Call(`_rxode2_activationF`, x, 11L) | ||
} | ||
|
||
#' @rdname dGELU | ||
#' @export | ||
d4GELU <- function(x) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
.Call(`_rxode2_activationF`, x, 12L) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#' Leaky ReLU activation function | ||
#' | ||
#' @param x numeric vector | ||
#' @return numeric vector | ||
#' @family Activation Functions | ||
#' @export | ||
#' @examples | ||
#' | ||
#' lReLU(c(-1, 0, 1)) | ||
#' | ||
#' # Can use in rxode2 as well | ||
#' | ||
#' r <- rxode2({r <- lReLU(time)}) | ||
#' e <- et(c(-1, 0, 1)) | ||
#' rxSolve(r, e) | ||
lReLU <- function(x) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
.Call(`_rxode2_activationF`, x, 5L) | ||
} | ||
|
||
#' Derivative of Leaky ReLU activation function | ||
#' | ||
#' @param x numeric vector | ||
#' @return numeric vector | ||
#' @family Activation Functions | ||
#' @export | ||
#' @examples | ||
#' | ||
#' dlReLU(c(-1, 0, 1)) | ||
#' | ||
#' # Can use in rxode2 as well | ||
#' | ||
#' r <- rxode2({r <- dlReLU(time)}) | ||
#' e <- et(c(-1, 0, 1)) | ||
#' rxSolve(r, e) | ||
#' | ||
dlReLU <- function(x) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
.Call(`_rxode2_activationF`, x, 8L) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#' Parametric ReLU Activation Function | ||
#' | ||
#' @family Activation Functions | ||
#' @param x A numeric vector. All elements must be finite and | ||
#' non-missing. | ||
#' @param alpha A numeric scalar. All elements must be finite and | ||
#' non-missing. | ||
#' @return A numeric vector where the ReLU function has been applied | ||
#' to each element of `x`. | ||
#' @author Matthew Fidler | ||
#' @export | ||
#' @examples | ||
#' | ||
#' PReLU(c(-1, 0, 1, 2), 2) | ||
#' | ||
#' # Can also be used in rxode2: | ||
#' x <- rxode2({ | ||
#' r=PReLU(time, 2) | ||
#' }) | ||
#' | ||
#' e <- et(c(-1, 0, 1, 2)) | ||
#' | ||
#' rxSolve(x, e) | ||
#' | ||
PReLU <- function(x, alpha=1) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
checkmate::assertNumeric(alpha, finite=TRUE, any.missing=FALSE) | ||
.df <- data.frame(x=x, alpha=alpha) | ||
.Call(`_rxode2_activationF2`, .df$x, .df$alpha, 7L) | ||
} | ||
#' Derivatives Parametric ReLU Activation Function | ||
#' | ||
#' | ||
#' @param x A numeric vector. All elements must be finite and | ||
#' non-missing. | ||
#' @param alpha A numeric scalar. All elements must be finite and | ||
#' non-missing. | ||
#' @return A numeric vector where the derivative(s) of the ELU function has been applied | ||
#' to each element of `x`. | ||
#' @export | ||
#' @author Matthew L. Fidler | ||
#' @family Activation Functions | ||
#' @examples | ||
#' | ||
#' dPReLU(c(-1, 0, 1, 2), 2) | ||
#' dPReLUa(c(-1, 0, 1, 2), 2) | ||
#' dPReLUa1(c(-1, 0, 1, 2), 2) | ||
#' | ||
#' | ||
#' # Can also be used in rxode2: | ||
#' r <- rxode2({ | ||
#' r1=dPReLU(time, 2) | ||
#' r2a=dPReLUa(time, 2) | ||
#' ra=dPReLUa1(time, 2) | ||
#' }) | ||
#' | ||
#' e <- et(c(-1, 0, 1, 2)) | ||
#' rxSolve(r, e) | ||
dPReLU <- function(x, alpha=1) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
checkmate::assertNumeric(alpha, finite=TRUE, any.missing=FALSE) | ||
.df <- data.frame(x=x, alpha=alpha) | ||
.Call(`_rxode2_activationF2`, .df$x, .df$alpha, 8L) | ||
} | ||
|
||
#' @rdname dPReLU | ||
#' @export | ||
dPReLUa <- function(x, alpha=1) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
checkmate::assertNumeric(alpha, finite=TRUE, any.missing=FALSE) | ||
.df <- data.frame(x=x, alpha=alpha) | ||
.Call(`_rxode2_activationF2`, .df$x, .df$alpha, 9L) | ||
} | ||
|
||
#' @rdname dPReLU | ||
#' @export | ||
dPReLUa1 <- function(x, alpha=1) { | ||
checkmate::assertNumeric(x, finite=TRUE, any.missing=FALSE) | ||
checkmate::assertNumeric(alpha, finite=TRUE, any.missing=FALSE) | ||
.df <- data.frame(x=x, alpha=alpha) | ||
.Call(`_rxode2_activationF2`, .df$x, .df$alpha, 10L) | ||
} |
Oops, something went wrong.