Packages

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

Current section

Files

Jump to
gleam_synapses gen src gleam_synapses@model@mathematics.erl
Raw

gen/src/gleam_synapses@model@mathematics.erl

-module(gleam_synapses@model@mathematics).
-compile(no_auto_import).
-export([dot_product/2, root_mean_square_error/1]).
-spec dot_product(
gleam_zlists@interop:z_list(float()),
gleam_zlists@interop:z_list(float())
) -> float().
dot_product(Left, Right) ->
gleam_zlists:sum(
gleam_zlists:map(gleam_zlists:zip(Left, Right), fun(X) -> {A, B} = X,
A * B end)
).
-spec euclidean_distance(
gleam_zlists@interop:z_list(float()),
gleam_zlists@interop:z_list(float())
) -> float().
euclidean_distance(Xs, Ys) ->
{ok, Res} = gleam@float:square_root(
gleam_zlists:sum(
gleam_zlists:map(gleam_zlists:zip(Xs, Ys), fun(T) -> {X, Y} = T,
Diff = X - Y,
Diff * Diff end)
)
),
Res.
-spec root_mean_square_error(
gleam_zlists@interop:z_list({gleam_zlists@interop:z_list(float()),
gleam_zlists@interop:z_list(float())})
) -> float().
root_mean_square_error(Y_hats_with_ys) ->
{N, S} = gleam_zlists:reduce(
gleam_zlists:map(Y_hats_with_ys, fun(T) -> {Y_hat, Y} = T,
D = euclidean_distance(Y_hat, Y),
D * D end),
{0, 0.0},
fun(X, Acc) -> {Acc_n, Acc_s} = Acc,
{Acc_n + 1, Acc_s + X} end
),
Avg = case gleam@int:to_float(N) of
0.0 -> 0.0;
Gleam@denominator -> S / Gleam@denominator
end,
{ok, Res} = gleam@float:square_root(Avg),
Res.