diff options
author | TinWoodman92 <chrhodgden@gmail.com> | 2023-02-25 18:58:22 -0600 |
---|---|---|
committer | TinWoodman92 <chrhodgden@gmail.com> | 2023-02-25 18:58:22 -0600 |
commit | 6042506b3b6a77c35471ce007d77ffbd6ae7420c (patch) | |
tree | 8c96f61f303666c2449851e6039af7e9ffc85250 /lib_act_func.r |
Diffstat (limited to 'lib_act_func.r')
-rw-r--r-- | lib_act_func.r | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib_act_func.r b/lib_act_func.r new file mode 100644 index 0000000..2329892 --- /dev/null +++ b/lib_act_func.r @@ -0,0 +1,35 @@ +# Activation function library + +relu <- function(x) return(x * (x > 0)) +Drelu <- function(x) { + lx <- as.numeric(x > 0) + attributes(lx) <- attributes(x) + return(lx) +} + +sigmoid <- function(x) 1 / (1 + exp(-x)) +Dsigmoid <- function(x) eval(D(expression(1 / (1 + exp(-x))), "x")) + +#tanh +Dtanh <- function(x) return(eval(D(expression(tanh(x)), "x"))) + +no_func <- function(x) return(x) +Dno_func <- function(x) { + lx <- rep(1, length(x)) + attributes(lx) <- attributes(x) + return(lx) +} + +sel_g <- function(func_name) { + if (func_name == "relu") return(relu) + if (func_name == "sigmoid") return(sigmoid) + if (func_name == "tanh") return(tanh) + if (func_name == "no_func") return(no_func) +} + +sel_d <- function(func_name) { + if (func_name == "relu") return(Drelu) + if (func_name == "sigmoid") return(Dsigmoid) + if (func_name == "tanh") return(Dtanh) + if (func_name == "no_func") return(Dno_func) +}
\ No newline at end of file |