Simulation Module Documentation
This module simulates scenarios and leverages a neural network to train on generated transaction data. The SimulationModule
is designed to work asynchronously, utilizing a neural network for processing and a storage system for saving the model's state. The module is designed to support multiple simulation scenarios, generating different types of mock transactions for each scenario and training the neural network based on the simulated data.
Overview of Components
-
Neural Network: The core of the module is a neural network that gets trained on the simulated transaction data. The neural network is shared across tasks using an asynchronous read-write lock (
RwLock
), allowing concurrent access to read the network while ensuring safe modification when training. -
Storage: The storage system (
Arc<Storage>
) is used to persist the neural network parameters after training. This ensures that model updates are stored and can be retrieved later for further use or analysis. -
UUID: A unique identifier (
uuid
) is associated with the module. This can be used for tracking or identifying different simulation sessions or instances.
Key Functionalities
Initialization
The module is initialized with three key components:
- A reference to the neural network.
- A storage instance for persisting neural network parameters.
- A unique identifier (UUID).
The initialization process binds these components together, creating an instance of the SimulationModule
that is ready to run simulations and train the neural network based on the results.
Running Simulations
The main purpose of the module is to run simulations, which simulate transaction scenarios and subsequently train the neural network based on the simulated data. This is done asynchronously, allowing multiple scenarios to be processed efficiently. Two simulation scenarios are defined as examples:
-
Scenario 1: Generates a set of mock transactions and trains the neural network with this data. The transactions are fabricated to represent a specific case that might occur in real-world applications.
-
Scenario 2: Similar to Scenario 1, but it represents a different set of conditions, allowing the neural network to be trained on varied datasets.
Each scenario is self-contained and generates its own transaction data.
Generating Simulated Data
For each scenario, a set of mock transaction data is generated. This data consists of a vector of Transaction
objects. Each transaction can represent various types of operations, such as payments, contracts, or predictions, using the TransactionData
struct. The generated transactions are used as training input for the neural network.
- Transaction Types: Transactions are represented using the
TransactionData
enum, which supports various transaction types such as payments, contracts, coin creation, predictions, forecasting, and fees. Each transaction type holds relevant data, and the module calculates the input size based on the properties of the transaction.
Training on Simulated Data
Once a scenario generates transaction data, the next step is training the neural network. Each transaction is converted into input features, which are fed into the neural network for training.
-
Input Calculation: The input features are derived from the transaction data. Depending on the type of transaction, different properties are used to compute the input size (e.g., the length of transaction fields such as data, contract details, or model predictions).
-
Target Calculation: The target values for training the neural network are binary (1.0 for positive transaction amounts, 0.0 otherwise), representing whether the transaction is considered valid based on its amount.
-
Training Process: For each input-target pair, the neural network is trained in an iterative manner (one epoch per scenario in this example). The neural network’s training process adjusts the model weights to learn from the simulated data.
Persisting the Neural Network State
After training on the simulated data, the neural network’s updated parameters are saved to persistent storage. This allows the neural network to continue learning from where it left off in future simulations or training sessions.
- Model Updates: The neural network provides a mechanism to retrieve the updated model parameters, which are then stored in the storage system using a unique key (
simulation_nn_parameters
). This ensures the latest state of the model is persisted after each training session.
Use Cases
-
Simulation for Neural Network Training: This module is particularly useful for creating simulated datasets to train neural networks in scenarios where real-world data might be unavailable or sensitive. By generating mock transactions, it provides a safe and controlled environment for experimenting with and improving machine learning models.
-
Transaction Data Processing: The module can simulate various types of financial or blockchain-related transactions, which can be used to train predictive models or anomaly detection systems in environments where transaction behavior needs to be analyzed.
-
Persistence of Neural Network Models: By saving the neural network's state after training, the system ensures that model updates are retained, which can be valuable in long-term learning applications or iterative model improvement scenarios.
Error Handling and Robustness
- Safe Access to Neural Network: The neural network is protected using an asynchronous read-write lock (
RwLock
), ensuring that training sessions are thread-safe and preventing data races or concurrent access issues. - Persistence with Storage: The storage system is used to ensure that neural network parameters are safely written to the database. If there is a failure to store the parameters, an error is logged, preventing silent failures.
Summary of Key Features:
-
Modular Design: Each simulation scenario is self-contained and can be expanded or modified independently. This makes it easy to test different conditions and their effects on the neural network.
-
Asynchronous Execution: All operations, including simulation and training, are asynchronous. This enables scalable and efficient processing of multiple scenarios or datasets.
-
Extensible Transaction Processing: The module supports various transaction types through the
TransactionData
enum. New transaction types can be added to the enum, allowing the module to handle a wide range of use cases.
This design makes the SimulationModule
a versatile tool for training neural networks on simulated transaction data and managing the neural network’s state efficiently.