Packages

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

Current section

Files

Jump to
gleam_synapses src gleam_synapses@model@encoding@attribute@attribute.erl
Raw

src/gleam_synapses@model@encoding@attribute@attribute.erl

-module(gleam_synapses@model@encoding@attribute@attribute).
-compile(no_auto_import).
-export([parse/1, updated/2, encode/2, decode/2]).
-export_type([attribute/0]).
-type attribute() :: {discrete, binary(), gleam_zlists@interop:z_list(binary())} |
{continuous, binary(), float(), float()}.
-spec parse(binary()) -> float().
parse(S) ->
Trimmed = gleam@string:trim(S),
case {gleam@float:parse(Trimmed), gleam@int:parse(Trimmed)} of
{{ok, X}, _@1} ->
X;
{_@2, {ok, X@1}} ->
gleam@int:to_float(X@1)
end.
-spec updated(attribute(), gleam@map:map_(binary(), binary())) -> attribute().
updated(Attribute, Datapoint) ->
case Attribute of
{discrete, Key, Values} ->
{ok, V@1} = case gleam@map:get(Datapoint, Key) of
{ok, V} -> {ok, V};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_synapses/model/encoding/attribute/attribute"/utf8>>,
function => <<"updated"/utf8>>,
line => 29})
end,
Updated_values = case gleam_zlists:has_member(Values, V@1) of
true ->
Values;
false ->
gleam_zlists:cons(Values, V@1)
end,
{discrete, Key, Updated_values};
{continuous, Key@1, Min, Max} ->
{ok, V@3} = case begin
_pipe = Datapoint,
_pipe@1 = gleam@map:get(_pipe, Key@1),
gleam@result:map(_pipe@1, fun parse/1)
end of
{ok, V@2} -> {ok, V@2};
_try@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@1,
module => <<"gleam_synapses/model/encoding/attribute/attribute"/utf8>>,
function => <<"updated"/utf8>>,
line => 37})
end,
{continuous,
Key@1,
gleam@float:min(V@3, Min),
gleam@float:max(V@3, Max)}
end.
-spec encode(attribute(), binary()) -> gleam_zlists@interop:z_list(float()).
encode(Attribute, Value) ->
case Attribute of
{discrete, _@1, Values} ->
_pipe = Values,
gleam_zlists:map(_pipe, fun(X) -> case X =:= Value of
true ->
1.0;
false ->
0.0
end end);
{continuous, _@2, Min, Max} ->
_pipe@1 = case Min =:= Max of
true ->
0.5;
false ->
Nomin = parse(Value) - Min,
Denomin = Max - Min,
case Denomin of
0.0 -> 0.0;
Gleam@denominator -> Nomin / Gleam@denominator
end
end,
gleam_zlists:singleton(_pipe@1)
end.
-spec decode(attribute(), gleam_zlists@interop:z_list(float())) -> binary().
decode(Attribute, Encoded_values) ->
case Attribute of
{discrete, _@1, Values} ->
{ok, {Hd@1, Tl@1}} = case begin
_pipe = Values,
_pipe@1 = gleam_zlists:zip(_pipe, Encoded_values),
gleam_zlists:uncons(_pipe@1)
end of
{ok, {Hd, Tl}} -> {ok, {Hd, Tl}};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_synapses/model/encoding/attribute/attribute"/utf8>>,
function => <<"decode"/utf8>>,
line => 72})
end,
_pipe@2 = gleam_zlists:reduce(
Tl@1,
Hd@1,
fun(X, Acc) ->
{_@2, X_float_val} = X,
{_@3, Acc_float_val} = Acc,
case X_float_val > Acc_float_val of
true ->
X;
false ->
Acc
end
end
),
gleam@pair:first(_pipe@2);
{continuous, _@4, Min, Max} ->
_pipe@3 = case Min =:= Max of
true ->
Min;
false ->
{ok, V@1} = case gleam_zlists:head(Encoded_values) of
{ok, V} -> {ok, V};
_try@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@1,
module => <<"gleam_synapses/model/encoding/attribute/attribute"/utf8>>,
function => <<"decode"/utf8>>,
line => 94})
end,
Factor = Max - Min,
(V@1 * Factor) + Min
end,
_pipe@4 = gleam@json:float(_pipe@3),
gleam@json:to_string(_pipe@4)
end.