Packages

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

Current section

Files

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

src/gleam_synapses@model@encoding@preprocessor.erl

-module(gleam_synapses@model@encoding@preprocessor).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([init/2, encode/2, decode/2, to_json/1, of_json/1, realized/1]).
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/encoding/preprocessor.gleam", 16).
-spec init(
gleam_zlists@interop:z_list({binary(), boolean()}),
gleam@iterator:iterator(gleam@dict:dict(binary(), binary()))
) -> gleam_zlists@interop:z_list(gleam_synapses@model@encoding@attribute@attribute:attribute()).
init(Keys_with_flags, Dataset) ->
_assert_subject = gleam@iterator:step(Dataset),
{next, Head, Tail} = case _assert_subject of
{next, _, _} -> _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/encoding/preprocessor"/utf8>>,
function => <<"init"/utf8>>,
line => 20})
end,
_pipe = Keys_with_flags,
_pipe@1 = gleam_zlists:map(
_pipe,
fun(T) ->
{Key, Is_distinct} = T,
_assert_subject@1 = gleam@dict:get(Head, Key),
{ok, Str_val} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"gleam_synapses/model/encoding/preprocessor"/utf8>>,
function => <<"init"/utf8>>,
line => 24})
end,
case Is_distinct of
true ->
{discrete, Key, gleam_zlists:singleton(Str_val)};
false ->
V = gleam_synapses@model@encoding@attribute@attribute:parse(
Str_val
),
{continuous, Key, V, V}
end
end
),
gleam@iterator:fold(
Tail,
_pipe@1,
fun(Acc, X) ->
gleam_zlists:map(
Acc,
fun(Attr) ->
gleam_synapses@model@encoding@attribute@attribute:updated(
Attr,
X
)
end
)
end
).
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/encoding/preprocessor.gleam", 40).
-spec encode(
gleam_zlists@interop:z_list(gleam_synapses@model@encoding@attribute@attribute:attribute()),
gleam@dict:dict(binary(), binary())
) -> gleam_zlists@interop:z_list(float()).
encode(Preprocessor, Datapoint) ->
gleam_zlists:flat_map(
Preprocessor,
fun(Attr) ->
Key = case Attr of
{discrete, K, _} ->
K;
{continuous, K@1, _, _} ->
K@1
end,
_assert_subject = gleam@dict:get(Datapoint, Key),
{ok, V} = 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/encoding/preprocessor"/utf8>>,
function => <<"encode"/utf8>>,
line => 49})
end,
gleam_synapses@model@encoding@attribute@attribute:encode(Attr, V)
end
).
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/encoding/preprocessor.gleam", 54).
-spec decode_acc_f(
gleam_synapses@model@encoding@attribute@attribute:attribute(),
{gleam_zlists@interop:z_list(float()),
gleam_zlists@interop:z_list({binary(), binary()})}
) -> {gleam_zlists@interop:z_list(float()),
gleam_zlists@interop:z_list({binary(), binary()})}.
decode_acc_f(Attr, Acc) ->
{Unprocessed_floats, Processed_ks_vs} = Acc,
{Key@2, Split_index} = case Attr of
{discrete, Key, Values} ->
{Key, gleam_zlists:count(Values)};
{continuous, Key@1, _, _} ->
{Key@1, 1}
end,
{Encoded_values_ls, Next_floats_zls} = gleam_zlists:split(
Unprocessed_floats,
Split_index
),
Decoded_value = begin
_pipe = Encoded_values_ls,
_pipe@1 = gleam_zlists:of_list(_pipe),
gleam_synapses@model@encoding@attribute@attribute:decode(Attr, _pipe@1)
end,
Next_ks_vs = gleam_zlists:cons(Processed_ks_vs, {Key@2, Decoded_value}),
{Next_floats_zls, Next_ks_vs}.
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/encoding/preprocessor.gleam", 73).
-spec decode(
gleam_zlists@interop:z_list(gleam_synapses@model@encoding@attribute@attribute:attribute()),
gleam_zlists@interop:z_list(float())
) -> gleam@dict:dict(binary(), binary()).
decode(Preprocessor, Encoded_datapoint) ->
_pipe = Preprocessor,
_pipe@1 = gleam_zlists:reduce(
_pipe,
{Encoded_datapoint, gleam_zlists:new()},
fun(X, Acc) -> decode_acc_f(X, Acc) end
),
_pipe@2 = gleam@pair:second(_pipe@1),
_pipe@3 = gleam_zlists:to_list(_pipe@2),
maps:from_list(_pipe@3).
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/encoding/preprocessor.gleam", 86).
-spec to_json(
gleam_zlists@interop:z_list(gleam_synapses@model@encoding@attribute@attribute:attribute())
) -> binary().
to_json(Preprocessor) ->
_pipe = Preprocessor,
_pipe@1 = gleam_zlists:map(
_pipe,
fun gleam_synapses@model@encoding@attribute@attribute_serialized:serialized/1
),
_pipe@2 = gleam_zlists:to_list(_pipe@1),
_pipe@3 = gleam@json:array(_pipe@2, fun gleam@function:identity/1),
gleam@json:to_string(_pipe@3).
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/encoding/preprocessor.gleam", 94).
-spec of_json(binary()) -> gleam_zlists@interop:z_list(gleam_synapses@model@encoding@attribute@attribute:attribute()).
of_json(S) ->
_assert_subject = begin
_pipe = gleam_synapses@model@encoding@attribute@attribute_serialized:decoder(
),
_pipe@1 = gleam@dynamic:list(_pipe),
gleam@json:decode(S, _pipe@1)
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/encoding/preprocessor"/utf8>>,
function => <<"of_json"/utf8>>,
line => 95})
end,
gleam_zlists:of_list(Res).
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/encoding/preprocessor.gleam", 102).
-spec realized(
gleam_zlists@interop:z_list(gleam_synapses@model@encoding@attribute@attribute:attribute())
) -> gleam_zlists@interop:z_list(gleam_synapses@model@encoding@attribute@attribute:attribute()).
realized(Preprocessor) ->
_pipe = Preprocessor,
_pipe@1 = gleam_zlists:map(
_pipe,
fun gleam_synapses@model@encoding@attribute@attribute_serialized:realized/1
),
gleam_zlists:to_list(_pipe@1),
Preprocessor.