From 6042506b3b6a77c35471ce007d77ffbd6ae7420c Mon Sep 17 00:00:00 2001 From: TinWoodman92 Date: Sat, 25 Feb 2023 18:58:22 -0600 Subject: Initial Commit --- lib_act_func.r | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lib_act_func.r (limited to 'lib_act_func.r') 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 -- cgit v1.2.3