Current section
Files
Jump to
Current section
Files
src/gleam_synapses@model@mathematics.erl
-module(gleam_synapses@model@mathematics).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([exp/1, log/1, tanh/1, dot_product/2, root_mean_square_error/1, accuracy/1]).
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/mathematics.gleam", 8).
-spec exp(float()) -> float().
exp(X) ->
math:exp(X).
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/mathematics.gleam", 11).
-spec log(float()) -> float().
log(X) ->
math:log(X).
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/mathematics.gleam", 14).
-spec tanh(float()) -> float().
tanh(X) ->
math:tanh(X).
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/mathematics.gleam", 16).
-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).
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/mathematics.gleam", 25).
-spec euclidean_distance(
gleam_zlists@interop:z_list(float()),
gleam_zlists@interop:z_list(float())
) -> float().
euclidean_distance(Xs, Ys) ->
_assert_subject = 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,
{ok, Res} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"gleam_synapses/model/mathematics"/utf8>>,
function => <<"euclidean_distance"/utf8>>,
line => 26})
end,
Res.
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/mathematics.gleam", 39).
-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;
-0.0 -> -0.0;
Gleam@denominator -> S / Gleam@denominator
end,
_assert_subject = gleam@float:square_root(Avg),
{ok, Res} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"gleam_synapses/model/mathematics"/utf8>>,
function => <<"root_mean_square_error"/utf8>>,
line => 54})
end,
Res.
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/mathematics.gleam", 58).
-spec index_of_max_val(gleam_zlists@interop:z_list(float())) -> integer().
index_of_max_val(Ys) ->
_assert_subject = begin
_pipe = gleam_zlists:indices(),
_pipe@1 = gleam_zlists:zip(_pipe, Ys),
gleam_zlists:uncons(_pipe@1)
end,
{ok, {Hd, Rst}} = case _assert_subject of
{ok, {_, _}} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"gleam_synapses/model/mathematics"/utf8>>,
function => <<"index_of_max_val"/utf8>>,
line => 59})
end,
_pipe@2 = gleam_zlists:reduce(
Rst,
Hd,
fun(X, Acc) ->
{_, V} = X,
{_, Acc_v} = Acc,
case V > Acc_v of
true ->
X;
false ->
Acc
end
end
),
gleam@pair:first(_pipe@2).
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/mathematics.gleam", 74).
-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;
-0.0 -> -0.0;
Gleam@denominator -> gleam@int:to_float(S) / Gleam@denominator
end.