gleam_synapses/net

This namespace contains functions that are related to the neural networks.

Types

pub type Net =
  network.Network

Functions

pub fn errors(net: ZList(ZList(Neuron)), input_values: List(Float), expected_output: List(
    Float,
  ), in_parallel: Bool) -> List(Float)
pub fn fit(net: ZList(ZList(Neuron)), learning_rate: Float, input_values: List(
    Float,
  ), expected_output: List(Float)) -> ZList(ZList(Neuron))

Returns the neural network with its weights adjusted to the provided observation.

learning_rate is a number that controls how much the weights are adjusted to the observation. input_values is the feature values of the observation and its size should be equal to the size of the input layer. expected_output is the expected output of the observation and its size should be equal to the size of the output layer.

In order for a network to be fully trained, it should fit with multiple observations, usually by folding over an iterator.

pub fn from_json(json: String) -> ZList(ZList(Neuron))

Parses and returns a neural network.

net.from_json("[[{\"activationF\":\"sigmoid\",\"weights\":[-0.4,-0.1,-0.8]}]]")
pub fn new(layers: List(Int)) -> ZList(ZList(Neuron))

Creates a random neural network by accepting its layer sizes.

net.new([3, 4, 2])
pub fn new_custom(layers: List(Int), activation_f: fn(Int) ->
    Activation, weight_init_f: fn(Int) -> Float) -> ZList(
  ZList(Neuron),
)

Creates a neural network by accepting the size, the activation function and the weights for each layer.

net.new_custom([3, 4, 2], fn(_){fun.sigmoid()}, fn(_){float.random(0.0, 1.0)})
pub fn new_with_seed(layers: List(Int), seed: Int) -> ZList(
  ZList(Neuron),
)

Creates a non-random neural network by accepting its layer sizes and a seed. seed is the number used to initialize the internal pseudorandom number generator.

net.new([3, 4, 2], 1000)
pub fn par_fit(net: ZList(ZList(Neuron)), learning_rate: Float, input_values: List(
    Float,
  ), expected_output: List(Float)) -> ZList(ZList(Neuron))

Returns the neural network with its weights adjusted to the provided observation.

The calculation is performed in parallel. When the neural network has huge layers, the parallel calculation boosts the performance.

pub fn par_predict(net: ZList(ZList(Neuron)), input_values: List(
    Float,
  )) -> List(Float)

Calculates the prediction for the provided input in parallel. It’s usefull for neural networks with huge layers.

pub fn predict(net: ZList(ZList(Neuron)), input_values: List(
    Float,
  )) -> List(Float)

Makes a prediction for the provided input. The size of the returned list should be equal to the size of the output layer.

input_values should be the list that contains the values of the features. Its size should be equal to the size of the input layer.

pub fn to_json(net: ZList(ZList(Neuron))) -> String

The JSON representation of the neural network.

pub fn to_svg(net: ZList(ZList(Neuron))) -> String

Returns the SVG representation of the neural network.

The color of each neuron depends on its activation function while the transparency of the synapses depends on their weight.