brain_system (macula_tweann v0.18.1)

View Source

Brain system facade - unified API for neural network inference and learning.

This module provides a high-level API for working with brain systems. A brain system consists of: - An inference process (brain.erl) for forward propagation - An optional learner process (brain_learner.erl) for weight adaptation

Both processes are supervised by brain_sup.erl.

Starting a Brain System

Simple inference-only brain: Network = network_evaluator:create_feedforward(42, [16, 8], 6), {ok, Pid} = brain_system:start_link(#{network => Network})

Brain with learning enabled: {ok, Pid} = brain_system:start_link(#{ network => Network, learning_enabled => true, plasticity_rule => modulated })

Evaluation

Evaluate with recording for later learning: Outputs = brain_system:evaluate(Pid, Inputs)

Learning

Online learning (immediate weight updates): brain_system:reward(Pid, 1.0) %% Positive reward brain_system:learn_step(Pid) %% Apply learning

Batch learning (from experience buffer): brain_system:start_recording(Pid) %% ... run episode ... brain_system:learn_from_experience(Pid, FinalReward)

Architecture

brain_system (this module - facade) | v brain_sup (supervisor) | +-- brain (inference) +-- brain_learner (learning, optional)

Summary

Functions

Clear the experience buffer.

Disable learning.

Evaluate the brain with sensor inputs.

Get the brain ID used for pubsub communication.

Get the inference process PID.

Get the learner process PID.

Get the current learning rate.

Get the current network.

Get the current plasticity rule.

Get network topology.

Get visualization data.

Check if the system has a learner.

Check if learning is enabled.

Learn from buffered experiences with current reward.

Learn from buffered experiences with a specific reward.

Perform a learning step using current reward.

Provide a reward signal.

Set the learning rate.

Replace the network.

Set the plasticity rule.

Start a brain system.

Start a named brain system.

Start recording experiences for batch learning.

Stop a brain system.

Stop recording experiences.

Subscribe to visualization updates.

Subscribe a specific process to visualization updates.

Unsubscribe from visualization updates.

Unsubscribe a specific process.

Types

brain_system/0

-opaque brain_system()

Functions

clear_experience(Brain_system)

-spec clear_experience(brain_system()) -> ok | {error, no_learner}.

Clear the experience buffer.

disable_learning(Brain_system)

-spec disable_learning(brain_system()) -> ok | {error, no_learner}.

Disable learning.

enable_learning(Brain_system)

-spec enable_learning(brain_system()) -> ok | {error, no_learner}.

Enable learning.

evaluate(Brain_system, Inputs)

-spec evaluate(brain_system(), [float()]) -> [float()].

Evaluate the brain with sensor inputs.

The brain publishes an 'evaluated' event via pubsub after each evaluation. If the learner has auto_record enabled, it will automatically record experiences from these events.

get_brain_id(Brain_system)

-spec get_brain_id(brain_system()) -> term().

Get the brain ID used for pubsub communication.

get_inference_pid(Brain_system)

-spec get_inference_pid(brain_system()) -> pid().

Get the inference process PID.

get_learner_pid(Brain_system)

-spec get_learner_pid(brain_system()) -> pid() | undefined.

Get the learner process PID.

get_learning_rate(Brain_system)

-spec get_learning_rate(brain_system()) -> float() | {error, no_learner}.

Get the current learning rate.

get_network(Brain_system)

-spec get_network(brain_system()) -> network_evaluator:network().

Get the current network.

get_plasticity_rule(Brain_system)

-spec get_plasticity_rule(brain_system()) -> atom() | {error, no_learner}.

Get the current plasticity rule.

get_topology(Brain_system)

-spec get_topology(brain_system()) -> map().

Get network topology.

get_viz(Brain_system)

-spec get_viz(brain_system()) -> map() | undefined.

Get visualization data.

has_learner(Brain_system)

-spec has_learner(brain_system()) -> boolean().

Check if the system has a learner.

is_learning_enabled(Brain_system)

-spec is_learning_enabled(brain_system()) -> boolean().

Check if learning is enabled.

learn_from_experience(System)

-spec learn_from_experience(brain_system()) -> {ok, non_neg_integer()} | {error, no_learner}.

Learn from buffered experiences with current reward.

learn_from_experience(Brain_system, Reward)

-spec learn_from_experience(brain_system(), float()) -> {ok, non_neg_integer()} | {error, no_learner}.

Learn from buffered experiences with a specific reward.

learn_step(Brain_system)

-spec learn_step(brain_system()) -> {ok, non_neg_integer()} | {error, no_learner}.

Perform a learning step using current reward.

This applies the plasticity rule to update weights based on the most recent experience and current reward.

reward(Brain_system, Reward)

-spec reward(brain_system(), float()) -> ok | {error, no_learner}.

Provide a reward signal.

For modulated learning, positive rewards strengthen active connections, negative rewards weaken them.

This publishes a 'reward_received' event via pubsub, which the learner receives and uses for future weight updates.

set_learning_rate(Brain_system, Rate)

-spec set_learning_rate(brain_system(), float()) -> ok | {error, no_learner}.

Set the learning rate.

set_network(Brain_system, Network)

-spec set_network(brain_system(), network_evaluator:network()) -> ok.

Replace the network.

set_plasticity_rule(Brain_system, Rule)

-spec set_plasticity_rule(brain_system(), atom()) -> ok | {error, no_learner}.

Set the plasticity rule.

start_link(Opts)

-spec start_link(map()) -> {ok, brain_system()} | {error, term()}.

Start a brain system.

This starts the supervisor and all child processes, then connects the learner to the inference process if learning is enabled.

Options: - network - The neural network (required) - learning_enabled - Start learner process (default: false) - plasticity_rule - Rule for learning (default: modulated) - learning_rate - Learning rate (default: 0.01) - input_labels - Labels for visualization - viz_enabled - Enable visualization (default: true)

start_link(Name, Opts)

-spec start_link(term(), map()) -> {ok, brain_system()} | {error, term()}.

Start a named brain system.

start_recording(Brain_system)

-spec start_recording(brain_system()) -> ok | {error, no_learner}.

Start recording experiences for batch learning.

This enables auto_record in the learner, which will automatically record experiences from 'evaluated' events published by the brain.

stop(Brain_system)

-spec stop(brain_system()) -> ok.

Stop a brain system.

stop_recording(Brain_system)

-spec stop_recording(brain_system()) -> ok | {error, no_learner}.

Stop recording experiences.

This disables auto_record in the learner.

subscribe(Brain_system)

-spec subscribe(brain_system()) -> ok.

Subscribe to visualization updates.

subscribe(Brain_system, SubscriberPid)

-spec subscribe(brain_system(), pid()) -> ok.

Subscribe a specific process to visualization updates.

unsubscribe(Brain_system)

-spec unsubscribe(brain_system()) -> ok.

Unsubscribe from visualization updates.

unsubscribe(Brain_system, SubscriberPid)

-spec unsubscribe(brain_system(), pid()) -> ok.

Unsubscribe a specific process.