Packages

A plug-and-play library for neural networks written in Gleam

Current section

Files

Jump to
gleam_synapses src gleam_synapses@model@net_elems@activation@activation.erl
Raw

src/gleam_synapses@model@net_elems@activation@activation.erl

-module(gleam_synapses@model@net_elems@activation@activation).
-compile(no_auto_import).
-export([f/1, deriv/1, inverse/1, generator/0]).
-export_type([activation/0]).
-type activation() :: sigmoid | identity | tanh | leaky_re_lu.
-spec sigmoid_f(float()) -> float().
sigmoid_f(X) ->
case (1.0 + gleam_synapses@model@mathematics:exp(0.0 - X)) of
0.0 -> 0.0;
Gleam@denominator -> 1.0 / Gleam@denominator
end.
-spec f(activation()) -> fun((float()) -> float()).
f(Activation) ->
case Activation of
sigmoid ->
fun sigmoid_f/1;
identity ->
fun gleam@function:identity/1;
tanh ->
fun gleam_synapses@model@mathematics:tanh/1;
leaky_re_lu ->
fun(X) -> case X < 0.0 of
true ->
0.01 * X;
false ->
X
end end
end.
-spec deriv(activation()) -> fun((float()) -> float()).
deriv(Activation) ->
case Activation of
sigmoid ->
fun(D) -> sigmoid_f(D) * (1.0 - sigmoid_f(D)) end;
identity ->
gleam@function:constant(1.0);
tanh ->
fun(D@1) ->
1.0
- (gleam_synapses@model@mathematics:tanh(D@1)
* gleam_synapses@model@mathematics:tanh(D@1))
end;
leaky_re_lu ->
fun(D@2) -> case D@2 < 0.0 of
true ->
0.01;
false ->
1.0
end end
end.
-spec inverse(activation()) -> fun((float()) -> float()).
inverse(Activation) ->
case Activation of
sigmoid ->
fun(Y) ->
T = case (1.0
- Y) of
0.0 -> 0.0;
Gleam@denominator -> Y
/ Gleam@denominator
end,
gleam_synapses@model@mathematics:log(T)
end;
identity ->
fun gleam@function:identity/1;
tanh ->
fun(Y@1) ->
0.5
* gleam_synapses@model@mathematics:log(case (1.0 - Y@1) of
0.0 -> 0.0;
Gleam@denominator@1 -> (1.0 + Y@1) / Gleam@denominator@1
end)
end;
leaky_re_lu ->
fun(Y@2) -> case Y@2 < 0.0 of
true ->
Y@2 / 0.01;
false ->
Y@2
end end
end.
-spec generator() -> minigen:generator(activation()).
generator() ->
minigen:always(sigmoid).