Current section
Files
Jump to
Current section
Files
src/gleam_synapses@model@mathematics.erl
-module(gleam_synapses@model@mathematics).
-compile(no_auto_import).
-export([exp/1, log/1, tanh/1, dot_product/2, root_mean_square_error/1, accuracy/1]).
-spec exp(float()) -> float().
exp(A) ->
math:exp(A).
-spec log(float()) -> float().
log(A) ->
math:log(A).
-spec tanh(float()) -> float().
tanh(A) ->
math:tanh(A).
-spec dot_product(
gleam_zlists@interop:z_list(float()),
gleam_zlists@interop:z_list(float())
) -> float().
dot_product(Left, Right) ->
_pipe = gleam_zlists:zip(Left, Right),
_pipe@1 = gleam_zlists:map(
_pipe,
fun(X) ->
{A, B} = X,
A
* B
end
),
gleam_zlists:sum(_pipe@1).
-spec euclidean_distance(
gleam_zlists@interop:z_list(float()),
gleam_zlists@interop:z_list(float())
) -> float().
euclidean_distance(Xs, Ys) ->
{ok, Res@1} = case begin
_pipe = Xs,
_pipe@1 = gleam_zlists:zip(_pipe, Ys),
_pipe@2 = gleam_zlists:map(
_pipe@1,
fun(T) ->
{X, Y} = T,
Diff = X
- Y,
Diff
* Diff
end
),
_pipe@3 = gleam_zlists:sum(_pipe@2),
gleam@float:square_root(_pipe@3)
end of
{ok, Res} -> {ok, Res};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_synapses/model/mathematics"/utf8>>,
function => <<"euclidean_distance"/utf8>>,
line => 26})
end,
Res@1.
-spec root_mean_square_error(
gleam@iterator:iterator({gleam_zlists@interop:z_list(float()),
gleam_zlists@interop:z_list(float())})
) -> float().
root_mean_square_error(Y_hats_with_ys) ->
{N, S} = begin
_pipe = Y_hats_with_ys,
_pipe@1 = gleam@iterator:map(
_pipe,
fun(T) ->
{Y_hat, Y} = T,
D = euclidean_distance(Y_hat, Y),
D
* D
end
),
gleam@iterator:fold(
_pipe@1,
{0, 0.0},
fun(Acc, X) ->
{Acc_n, Acc_s} = Acc,
{Acc_n + 1, Acc_s + X}
end
)
end,
Avg = case gleam@int:to_float(N) of
0.0 -> 0.0;
Gleam@denominator -> S / Gleam@denominator
end,
{ok, Res@1} = case gleam@float:square_root(Avg) of
{ok, Res} -> {ok, Res};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_synapses/model/mathematics"/utf8>>,
function => <<"root_mean_square_error"/utf8>>,
line => 57})
end,
Res@1.
-spec index_of_max_val(gleam_zlists@interop:z_list(float())) -> integer().
index_of_max_val(Ys) ->
{ok, {Hd@1, Rst@1}} = case begin
_pipe = gleam_zlists:indices(),
_pipe@1 = gleam_zlists:zip(_pipe, Ys),
gleam_zlists:uncons(_pipe@1)
end of
{ok, {Hd, Rst}} -> {ok, {Hd, Rst}};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_synapses/model/mathematics"/utf8>>,
function => <<"index_of_max_val"/utf8>>,
line => 62})
end,
_pipe@2 = gleam_zlists:reduce(
Rst@1,
Hd@1,
fun(X, Acc) ->
{_@1, V} = X,
{_@2, Acc_v} = Acc,
case V > Acc_v of
true ->
X;
false ->
Acc
end
end
),
gleam@pair:first(_pipe@2).
-spec accuracy(
gleam@iterator:iterator({gleam_zlists@interop:z_list(float()),
gleam_zlists@interop:z_list(float())})
) -> float().
accuracy(Y_hats_with_ys) ->
{N, S} = begin
_pipe = Y_hats_with_ys,
_pipe@1 = gleam@iterator:map(
_pipe,
fun(T) ->
{Y_hat, Y} = T,
index_of_max_val(Y_hat)
=:= index_of_max_val(Y)
end
),
gleam@iterator:fold(
_pipe@1,
{0, 0},
fun(Acc, X) ->
{Acc_n, Acc_s} = Acc,
New_s = case X of
true ->
Acc_s + 1;
false ->
Acc_s
end,
{Acc_n + 1, New_s}
end
)
end,
case gleam@int:to_float(N) of
0.0 -> 0.0;
Gleam@denominator -> gleam@int:to_float(S) / Gleam@denominator
end.