brain (macula_tweann v0.18.1)

View Source

Brain process for real-time neural network inference.

A brain is a running neural network that accepts sensor inputs and produces actuator outputs. Unlike the process-per-neuron architecture used during training, this is a single GenServer optimized for real-time inference in applications like games and robotics.

Features

- Synchronous evaluation via evaluate/2 - Internal state persistence for LTC neurons - Visualization data for UI rendering - PubSub notifications for state changes

Usage

Start a brain with a network: Network = network_evaluator:create_feedforward(42, [16, 8], 6), {ok, Pid} = brain:start_link(#{network => Network})

Evaluate with sensor inputs: Outputs = brain:evaluate(Pid, Inputs)

Get visualization data: VizData = brain:get_viz(Pid)

Subscribe to viz updates (Erlang): brain:subscribe(Pid) receive {brain_viz, Pid, VizData} -> ... end

Summary

Functions

Evaluate the brain with sensor inputs.

Evaluate and return both outputs and all layer activations.

Get the current network.

Get network topology information.

Get current visualization data.

Replace the network (e.g., after mutation).

Start a brain process.

Start a named brain process.

Stop the brain process.

Subscribe to visualization updates.

Subscribe a specific process to visualization updates.

Unsubscribe from visualization updates.

Unsubscribe a specific process from visualization updates.

Functions

evaluate(Pid, Inputs)

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

Evaluate the brain with sensor inputs.

Returns the output values from the neural network. Also updates internal state and notifies subscribers.

evaluate_with_activations(Pid, Inputs)

-spec evaluate_with_activations(pid(), [float()]) -> {[float()], [[float()]]}.

Evaluate and return both outputs and all layer activations.

This is used by the learning system to record experiences. Returns {Outputs, Activations} where Activations includes all layers.

get_network(Pid)

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

Get the current network.

get_topology(Pid)

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

Get network topology information.

get_viz(Pid)

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

Get current visualization data.

Returns a map with nodes, connections, and activation levels.

handle_call(Request, From, State)

handle_cast(Msg, State)

handle_info(Info, State)

init(Opts)

set_network(Pid, Network)

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

Replace the network (e.g., after mutation).

start_link(Opts)

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

Start a brain process.

Options: - network - The neural network (required, from network_evaluator) - id - Optional identifier for this brain - input_labels - Labels for input neurons (for visualization) - viz_enabled - Enable visualization data (default: true)

start_link(Name, Opts)

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

Start a named brain process.

stop(Pid)

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

Stop the brain process.

subscribe(BrainPid)

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

Subscribe to visualization updates.

The calling process will receive {brain_viz, Pid, VizData} messages after each evaluation.

subscribe(BrainPid, SubscriberPid)

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

Subscribe a specific process to visualization updates.

terminate(Reason, State)

unsubscribe(BrainPid)

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

Unsubscribe from visualization updates.

unsubscribe(BrainPid, SubscriberPid)

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

Unsubscribe a specific process from visualization updates.