initialize

open suspend override fun initialize(state: KarlContainerState?, coroutineScope: CoroutineScope)

Initializes the neural network engine with optional state restoration.

This method performs complete engine initialization including neural network weight initialization, state restoration from previous sessions, and coroutine scope configuration for asynchronous operations.

Initialization Process:

  1. Atomicity Check: Ensures single initialization using AtomicBoolean.compareAndSet

  2. Scope Assignment: Stores provided CoroutineScope for async training operations

  3. State Restoration: Attempts to restore neural network from saved KarlContainerState

  4. Fallback Initialization: Creates new model if restoration fails or no state provided

  5. Validation: Confirms successful initialization with comprehensive logging

State Restoration Strategy:

  • Primary Path: Deserialize neural network weights from state.data

  • Fallback Path: Initialize new Xavier-distributed weights if restoration fails

  • Error Handling: Graceful degradation with detailed error logging

  • Validation: Verify restored state integrity before proceeding

Concurrency Considerations:

  • Thread Safety: Atomic initialization flag prevents concurrent initialization

  • Mutex Protection: Model state modifications protected by modelMutex

  • Coroutine Integration: All operations compatible with provided scope

Error Recovery:

  • State restoration errors trigger new model initialization

  • Comprehensive error logging for debugging and monitoring

  • Graceful fallback ensures engine always reaches operational state

Parameters

state

Optional KarlContainerState containing serialized neural network weights and training metadata from previous session. If null, initializes new model.

coroutineScope

CoroutineScope for executing asynchronous training operations. Must remain active throughout engine lifetime.

See also

New model weight initialization

State deserialization and restoration

Persistent state data structure

Throws

If called multiple times on the same instance