brain_system (macula_tweann v0.18.1)
View SourceBrain 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.
Enable 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
Functions
-spec clear_experience(brain_system()) -> ok | {error, no_learner}.
Clear the experience buffer.
-spec disable_learning(brain_system()) -> ok | {error, no_learner}.
Disable learning.
-spec enable_learning(brain_system()) -> ok | {error, no_learner}.
Enable learning.
-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.
-spec get_brain_id(brain_system()) -> term().
Get the brain ID used for pubsub communication.
-spec get_inference_pid(brain_system()) -> pid().
Get the inference process PID.
-spec get_learner_pid(brain_system()) -> pid() | undefined.
Get the learner process PID.
-spec get_learning_rate(brain_system()) -> float() | {error, no_learner}.
Get the current learning rate.
-spec get_network(brain_system()) -> network_evaluator:network().
Get the current network.
-spec get_plasticity_rule(brain_system()) -> atom() | {error, no_learner}.
Get the current plasticity rule.
-spec get_topology(brain_system()) -> map().
Get network topology.
-spec get_viz(brain_system()) -> map() | undefined.
Get visualization data.
-spec has_learner(brain_system()) -> boolean().
Check if the system has a learner.
-spec is_learning_enabled(brain_system()) -> boolean().
Check if learning is enabled.
-spec learn_from_experience(brain_system()) -> {ok, non_neg_integer()} | {error, no_learner}.
Learn from buffered experiences with current reward.
-spec learn_from_experience(brain_system(), float()) -> {ok, non_neg_integer()} | {error, no_learner}.
Learn from buffered experiences with a specific reward.
-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.
-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.
-spec set_learning_rate(brain_system(), float()) -> ok | {error, no_learner}.
Set the learning rate.
-spec set_network(brain_system(), network_evaluator:network()) -> ok.
Replace the network.
-spec set_plasticity_rule(brain_system(), atom()) -> ok | {error, no_learner}.
Set the plasticity rule.
-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)
-spec start_link(term(), map()) -> {ok, brain_system()} | {error, term()}.
Start a named 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.
-spec stop(brain_system()) -> ok.
Stop a brain system.
-spec stop_recording(brain_system()) -> ok | {error, no_learner}.
Stop recording experiences.
This disables auto_record in the learner.
-spec subscribe(brain_system()) -> ok.
Subscribe to visualization updates.
-spec subscribe(brain_system(), pid()) -> ok.
Subscribe a specific process to visualization updates.
-spec unsubscribe(brain_system()) -> ok.
Unsubscribe from visualization updates.
-spec unsubscribe(brain_system(), pid()) -> ok.
Unsubscribe a specific process.