### Install Neuromancer with Examples Source: https://github.com/pnnl/neuromancer/blob/master/examples/ODEs/Part_9_SINDy.ipynb Installs the Neuromancer library with example dependencies from its GitHub repository. This is typically used in environments like Google Colab. ```python !pip install "neuromancer[examples] @ git+https://github.com/pnnl/neuromancer.git@master" ``` -------------------------------- ### Install Neuromancer Library Source: https://github.com/pnnl/neuromancer/blob/master/examples/domain_examples/building_load_forecasting_Transformers.ipynb Installs the neuromancer library, which is required for the subsequent code examples. Run this command in your environment before proceeding. ```python # !pip install neuromancer ``` -------------------------------- ### Install Neuromancer Library Source: https://github.com/pnnl/neuromancer/blob/master/examples/KANs/p2_fbkan_vs_kan_noise_data_2d.ipynb Clones the Neuromancer repository from GitHub and installs it with necessary extras for documentation, tests, and examples. This is useful for setting up the environment to run the provided examples. ```python import os # Check if the neuromancer directory already exists if not os.path.isdir('neuromancer'): # Clone the specific branch of the repository !git clone --branch feature/fbkans https://github.com/pnnl/neuromancer.git # Navigate to the repository directory %cd neuromancer # Install the repository with the required extras !pip install -e .[docs,tests,examples] ``` -------------------------------- ### Setup Imports and Dataset Creation Source: https://github.com/pnnl/neuromancer/blob/master/examples/function_encoder/Part_1_Intro_to_Function_Encoders.ipynb Imports necessary libraries and defines a function to create datasets for training and testing. The dataset includes example and query data points along with function parameters for plotting. ```python import torch import matplotlib.pyplot as plt from neuromancer.dataset import DictDataset from neuromancer.system import Node from neuromancer.constraint import variable from neuromancer.loss import PenaltyLoss from neuromancer.problem import Problem from neuromancer.trainer import Trainer from neuromancer.modules.function_encoder import FunctionEncoder def create_dataset(f, device='cpu',test=False): # batch size n_functions = 100 n_datapoints = 1000 # samples from -1 to 1 example_xs = torch.rand(n_functions, 50, 1) * 2 - 1 if not test: query_xs = torch.rand(n_functions, n_datapoints, 1) * 2 - 1 else: query_xs = torch.linspace(-1, 1, n_datapoints).reshape(-1, 1).repeat(n_functions, 1, 1) # sample a,b,c from -3 to 3 for n_functions size a = torch.rand(n_functions, 1, 1) * 6 - 3 b = torch.rand(n_functions, 1, 1) * 6 - 3 c = torch.rand(n_functions, 1, 1) * 6 - 3 example_ys = f(example_xs, a, b, c) query_ys = f(query_xs, a, b, c) dataset = {"example_xs": example_xs.to(device), "example_ys": example_ys.to(device), "query_xs": query_xs.to(device), "query_ys": query_ys.to(device), "a": a.to(device), # NOTE: a,b,c are only used for plotting. "b": b.to(device), "c": c.to(device), } return dataset # Function to sample from f = lambda x, a, b, c: a * x ** 2 + b * x + c # Create dataset device = 'cuda' if torch.cuda.is_available() else 'cpu' train_dataset = create_dataset(f, device=device) test_dataset = create_dataset(f, device=device, test=True) ``` -------------------------------- ### Install Neuromancer and PyDOE Source: https://github.com/pnnl/neuromancer/blob/master/examples/PDEs/Part_1_PINN_DiffusionEquation.ipynb Installs the necessary libraries for running the tutorial. Skip this step if running locally. ```python !pip install neuromancer !pip install pyDOE ``` -------------------------------- ### Install Neuromancer and PyDOE (Colab Only) Source: https://github.com/pnnl/neuromancer/blob/master/examples/PDEs/Part_5_Pendulum_Stacked.ipynb Installs the Neuromancer library with example dependencies and the PyDOE library. Skip this when running locally. ```python # !pip install "neuromancer[examples] @ git+https://github.com/pnnl/neuromancer.git@master" # !pip install pyDOE ``` -------------------------------- ### Install Neuromancer and PyTorch Lightning Source: https://github.com/pnnl/neuromancer/blob/master/examples/lightning_integration_examples/Part_2_lightning_advanced_and_gpu_tutorial.ipynb Installs the Neuromancer library with example dependencies and PyTorch Lightning. Skip this when running locally. ```python !pip install "neuromancer[examples] @ git+https://github.com/pnnl/neuromancer.git@master" !pip install lightning ``` -------------------------------- ### Basic Training with Data Setup Function Source: https://github.com/pnnl/neuromancer/blob/master/examples/lightning_integration_examples/README.md Demonstrates a typical training workflow using LitTrainer. It defines a data setup function to prepare datasets and then fits the model using the trainer. ```python def data_setup_function(nsim=5000): train_data = DictDataset(nsim, ... , name='train') dev_data = DictDataset(nsim, ... , name='dev) batch_size = 32 return train_data, dev_data, None, batch_size problem = Problem(...) lit_trainer = LitTrainer(epochs=10, accelerator='gpu', devices=[1]) lit_trainer.fit(problem, data_setup_function, nsim=100) ``` -------------------------------- ### Install NeuroMANCER Source: https://github.com/pnnl/neuromancer/blob/master/README.md Install the NeuroMANCER library using pip. For manual installation, refer to the INSTALLATION.md file. ```bash pip install neuromancer ``` -------------------------------- ### Setup Optimizer, Logger, and Trainer Source: https://github.com/pnnl/neuromancer/blob/master/examples/domain_examples/NODE_building_dynamics.ipynb Configure the optimizer, logger, and trainer for the problem. Ensure the logger is set up to save results and display desired metrics. ```python optimizer = torch.optim.Adam(problem.parameters(), lr=0.003) logger = BasicLogger(args=None, savedir='test', verbosity=1, stdout=['dev_loss', 'train_loss']) trainer = Trainer( problem, train_loader, dev_loader, test_data, optimizer, patience=100, warmup=500, epochs=1000, eval_metric="dev_loss", train_metric="train_loss", dev_metric="dev_loss", test_metric="dev_loss", logger=logger, ) ``` -------------------------------- ### Install PyTorch Lightning Source: https://github.com/pnnl/neuromancer/blob/master/examples/lightning_integration_examples/other_examples/lightning_cvxpy_layers.ipynb Install PyTorch Lightning if not already present. This is a prerequisite for using its features. ```bash pip install lightning ``` -------------------------------- ### WandB Sweep Configuration Example Source: https://github.com/pnnl/neuromancer/blob/master/examples/lightning_integration_examples/README.md An example of a sweep configuration dictionary for WandB, specifying the sweep method and parameter distributions for tuning. ```bash sweep_config = { 'method': 'random', 'parameters': { 'learning_rate': { 'min': 0.001, 'max': .007 }, 'batch_size': { 'values': [16, 64, 128] } } } ``` -------------------------------- ### Data Setup Function Source: https://github.com/pnnl/neuromancer/blob/master/examples/lightning_integration_examples/other_examples/lightning_custom_training_example.ipynb Defines a function to create and return training, development, and testing datasets along with the batch size. ```python def data_setup_function(nsim, a_low, a_high, p_low, p_high): samples_train = {"a": torch.FloatTensor(nsim, 1).uniform_(a_low, a_high), "p": torch.FloatTensor(nsim, 1).uniform_(p_low, p_high)} samples_dev = {"a": torch.FloatTensor(nsim, 1).uniform_(a_low, a_high), "p": torch.FloatTensor(nsim, 1).uniform_(p_low, p_high)} samples_test = {"a": torch.FloatTensor(nsim, 1).uniform_(a_low, a_high), "p": torch.FloatTensor(nsim, 1).uniform_(p_low, p_high)} # create named dictionary datasets train_data = DictDataset(samples_train, name='train') dev_data = DictDataset(samples_dev, name='dev') test_data = DictDataset(samples_test, name='test') batch_size = 64 # Return the dict datasets in train, dev, test order, followed by batch_size return train_data, dev_data, test_data, batch_size ``` -------------------------------- ### Install NeuroMANCER Ecosystem Source: https://github.com/pnnl/neuromancer/blob/master/docs/index.md Install the NeuroMANCER library in editable mode without its dependencies, assuming the environment is already set up. ```bash (neuromancer) $ pip install -e . --no-deps ``` -------------------------------- ### Install NeuroMANCER and Dependencies Source: https://github.com/pnnl/neuromancer/blob/master/examples/ODEs/scratch/duffing_parameter_interactive.ipynb Installs necessary libraries for NeuroMANCER, including specific versions of setuptools and torchdiffeq. This is typically for Colab environments. ```python !pip install setuptools==61.0.0 pyts torchdiffeq mlflow plum-dispatch !pip install git+https://github.com/pnnl/neuromancer.git@master --ignore-requires-python --no-deps ``` -------------------------------- ### Install NeuroMANCER and Gym Source: https://github.com/pnnl/neuromancer/blob/master/examples/domain_examples/RL_DPC_building_control.ipynb Installs the necessary libraries for NeuroMANCER and Gym. Note: A potential pip dependency error with Lida 0.0.10 can be ignored. ```python !pip install neuromancer gym ``` -------------------------------- ### Custom Optimizer Setup Source: https://github.com/pnnl/neuromancer/blob/master/examples/lightning_integration_examples/README.md Illustrates how to pass a custom PyTorch optimizer to the LitTrainer. This is useful when you need specific optimizer configurations not available by default. ```python optimizer = torch.optim.AdamW(policy.parameters(), lr=0.001) lit_trainer = LitTrainer(custom_optimizer=optimizer) ``` -------------------------------- ### Initialize LitTrainer with Profiler Source: https://github.com/pnnl/neuromancer/blob/master/examples/lightning_integration_examples/README.md Example of initializing LitTrainer with the 'profiler' keyword argument to enable training run profiling. Supports 'simple', 'pytorch', and 'advanced' options. ```bash lit_trainer = LitTrainer(profiler='simple) ``` -------------------------------- ### Initialize Wandb Logger and Train Model Source: https://github.com/pnnl/neuromancer/blob/master/examples/lightning_integration_examples/other_examples/lightning_nonauto_DeepKoopman_logging_tensorboard.ipynb Sets up a Wandb logger and a PyTorch Lightning trainer to log training progress. Ensure Wandb is installed and configured. ```python from lightning.pytorch.loggers import WandbLogger wandb_logger = WandbLogger() lit_trainer = LitTrainer(epochs=300,accelerator='cpu', logger=wandb_logger) lit_trainer.fit(problem, data_setup_function , sys=modelSystem, nsim=nsim, nsteps=nsteps,ts=ts, bs=bs) ``` -------------------------------- ### Create Responsive and Vanilla Systems Source: https://github.com/pnnl/neuromancer/blob/master/examples/domain_examples/grid_response.ipynb Initializes a responsive control system and a vanilla system (a deep copy of the responsive one). Requires 'System', 'obs_model', 'policy', 'state_model', 'nsteps', and 'deepcopy' to be available. ```python resp_sys = System([obs_model, policy, state_model], nsteps=nsteps, name='cl_system') vanilla_sys = deepcopy(resp_sys) resp_sys.show() ``` -------------------------------- ### Setup and Closed-Loop Simulation for RL/DPC Source: https://github.com/pnnl/neuromancer/blob/master/examples/domain_examples/RL_DPC_building_control.ipynb Initializes the system, generates reference and disturbance signals, and prepares data for closed-loop simulation. This snippet is used to set up the environment and initial conditions before running the simulation. ```python nsteps_test = 1000 sys = psl.systems['LinearSimpleSingleZone']() # generate reference np_refs = psl.signals.step(nsteps_test+1, 1, min=18., max=22., randsteps=5) ymin_val = torch.tensor(np_refs, dtype=torch.float32).reshape(1, nsteps_test+1, 1) ymax_val = ymin_val+2.0 # generate disturbance signal torch_dist = torch.tensor(sys.get_D(nsteps_test+1)).unsqueeze(0) # initial data for closed loop simulation x0 = torch.tensor(sys.get_x0()).reshape(1, 1, problem_specs["nx"]) inf_data = {'x': x0, 'y': x0[:, :, [-1]], 'ymin': ymin_val, 'ymax': ymax_val, 'd': torch_dist} train_env.reset() outputs = train_env.inference(inf_data, agent) cl_system.nsteps = nsteps_test # perform closed-loop simulation trajectories = cl_system(inf_data) ``` -------------------------------- ### Install Neuromancer with Optional Dependencies via PIP Source: https://github.com/pnnl/neuromancer/blob/master/INSTALLATION.md Installs Neuromancer in editable mode with documentation, testing, and example dependencies. Use quotes for zsh compatibility. ```bash pip install -e.[docs,tests,examples] ``` ```zsh pip install -e.'[docs,tests,examples]' ``` -------------------------------- ### Custom Train Start Hook Source: https://github.com/pnnl/neuromancer/blob/master/examples/lightning_integration_examples/other_examples/lightning_custom_training_example.ipynb Define a custom hook to execute code at the very beginning of the training process. This example prints a simple message. ```python def on_train_start(self): print("HELLO WORLD") ``` -------------------------------- ### Setup and System Construction for Control Policy Evaluation Source: https://github.com/pnnl/neuromancer/blob/master/examples/control/Part_4_NODE_control.ipynb Switches the system backend to PyTorch and defines helper functions for state and action normalization/denormalization. Constructs the test system by chaining necessary nodes. ```python sys.change_backend('torch') # We will have to denormalize the policy actions according to the system stats # Conversely we will have to normalize the system states according to the system stats to hand to the policy def norm(x): return sys.normalize(x, key='X') def denorm(u): return sys.denormalize(u, key='U') normnode = Node(norm, ['xsys'], ['xn'], name='norm') denormnode = Node(denorm, ['U'], ['u'], name='denorm') sysnode = Node(sys, ['xsys', 'u'], ['xsys'], name='actuator') test_system = System([normnode, policy_node, denormnode, sysnode]) # test_system.show() ``` -------------------------------- ### Install Neuromancer Package without Dependencies via PIP Source: https://github.com/pnnl/neuromancer/blob/master/INSTALLATION.md Installs the Neuromancer package in editable mode without installing dependencies, assuming they have been manually installed. ```bash pip install -e . --no-deps ``` -------------------------------- ### Install CMake on MacOS (Apple M1) Source: https://github.com/pnnl/neuromancer/blob/master/INSTALLATION.md Installs CMake using Homebrew, a prerequisite for installing CVXPY on Apple M1 machines. ```zsh brew install cmake ``` -------------------------------- ### Initialize NeuroMANCER and PSL Systems Source: https://github.com/pnnl/neuromancer/blob/master/examples/ODEs/scratch/duffing_parameter_interactive.ipynb Sets up two simulation components, one using DynamicsNeuromancer and another using DynamicsPSL, and combines them into a SystemSimulator. This is useful for benchmarking heterogeneous models. ```python sim_steps = 400 nm_system = sim.DynamicsNeuromancer(dynamics_model, name='nm', input_key_map={'x': 'x_nm'}) psl_system = sim.DynamicsPSL(modelSystem, name='psl', input_key_map={'x': 'x_psl'}) psl_system.model.ts = ts components = [nm_system, psl_system] system = sim.SystemSimulator(components) system.plot_graph() ``` -------------------------------- ### Neuromancer Installation Output Source: https://github.com/pnnl/neuromancer/blob/master/examples/control/Part_2_stabilize_ODE.ipynb This output shows the successful installation of Neuromancer and its dependencies, including version information and any package uninstalls or installs that occurred. ```text Requirement already satisfied: pywin32>=304 in c:\users\drgo694\onedrive - pnnl\documents\anaconda3\lib\site-packages (from docker<7,>=4.0.0->mlflow==2.5.0->neuromancer[examples]@ git+https://github.com/pnnl/neuromancer.git@master) (305.1) Requirement already satisfied: websocket-client>=0.32.0 in c:\users\drgo694\onedrive - pnnl\documents\anaconda3\lib\site-packages (from docker<7,>=4.0.0->mlflow==2.5.0->neuromancer[examples]@ git+https://github.com/pnnl/neuromancer.git@master) (0.58.0) Requirement already satisfied: itsdangerous>=2.0 in c:\users\drgo694\onedrive - pnnl\documents\anaconda3\lib\site-packages (from Flask<3->mlflow==2.5.0->neuromancer[examples]@ git+https://github.com/pnnl/neuromancer.git@master) (2.0.1) Requirement already satisfied: Werkzeug>=2.2.2 in c:\users\drgo694\onedrive - pnnl\documents\anaconda3\lib\site-packages (from Flask<3->mlflow==2.5.0->neuromancer[examples]@ git+https://github.com/pnnl/neuromancer.git@master) (2.2.2) Requirement already satisfied: gitdb<5,>=4.0.1 in c:\users\drgo694\onedrive - pnnl\documents\anaconda3\lib\site-packages (from gitpython<4,>=2.1.0->mlflow==2.5.0->neuromancer[examples]@ git+https://github.com/pnnl/neuromancer.git@master) (4.0.10) Requirement already satisfied: zipp>=0.5 in c:\users\drgo694\onedrive - pnnl\documents\anaconda3\lib\site-packages (from importlib-metadata!=4.7.0,<7,>=3.7.0->mlflow==2.5.0->neuromancer[examples]@ git+https://github.com/pnnl/neuromancer.git@master) (3.11.0) Requirement already satisfied: MarkupSafe>=2.0 in c:\users\drgo694\onedrive - pnnl\documents\anaconda3\lib\site-packages (from Jinja2<4,>=3.0->mlflow==2.5.0->neuromancer[examples]@ git+https://github.com/pnnl/neuromancer.git@master) (2.1.1) Requirement already satisfied: llvmlite<0.40,>=0.39.0dev0 in c:\users\drgo694\onedrive - pnnl\documents\anaconda3\lib\site-packages (from numba>=0.55.2->pyts->neuromancer[examples]@ git+https://github.com/pnnl/neuromancer.git@master) (0.39.1) Requirement already satisfied: qdldl in c:\users\drgo694\onedrive - pnnl\documents\anaconda3\lib\site-packages (from osqp>=0.4.1->cvxpy->neuromancer[examples]@ git+https://github.com/pnnl/neuromancer.git@master) (0.1.7) Requirement already satisfied: charset-normalizer<3,>=2 in c:\users\drgo694\onedrive - pnnl\documents\anaconda3\lib\site-packages (from requests<3,>=2.17.3->mlflow==2.5.0->neuromancer[examples]@ git+https://github.com/pnnl/neuromancer.git@master) (2.0.4) Requirement already satisfied: idna<4,>=2.5 in c:\users\drgo694\onedrive - pnnl\documents\anaconda3\lib\site-packages (from requests<3,>=2.17.3->mlflow==2.5.0->neuromancer[examples]@ git+https://github.com/pnnl/neuromancer.git@master) (3.4) Requirement already satisfied: certifi>=2017.4.17 in c:\users\drgo694\onedrive - pnnl\documents\anaconda3\lib\site-packages (from requests<3,>=2.17.3->mlflow==2.5.0->neuromancer[examples]@ git+https://github.com/pnnl/neuromancer.git@master) (2022.12.7) Requirement already satisfied: greenlet!=0.4.17 in c:\users\drgo694\onedrive - pnnl\documents\anaconda3\lib\site-packages (from sqlalchemy<3,>=1.4.0->mlflow==2.5.0->neuromancer[examples]@ git+https://github.com/pnnl/neuromancer.git@master) (2.0.1) Requirement already satisfied: smmap<6,>=3.0.1 in c:\users\drgo694\onedrive - pnnl\documents\anaconda3\lib\site-packages (from gitdb<5,>=4.0.1->gitpython<4,>=2.1.0->mlflow==2.5.0->neuromancer[examples]@ git+https://github.com/pnnl/neuromancer.git@master) (5.0.0) Building wheels for collected packages: neuromancer Building wheel for neuromancer (pyproject.toml): started Building wheel for neuromancer (pyproject.toml): finished with status 'done' Created wheel for neuromancer: filename=neuromancer-1.4.0-py3-none-any.whl size=161107 sha256=11f85fb1e7f84227c85b89fb42735b28e08e429b083ab651bcb7c1508b65f2f6 Stored in directory: C:\Users\drgo694\AppData\Local\Temp\pip-ephem-wheel-cache-uabqm7j5\wheels\88\db\69\58e642f880e17cca6125cfc7a8bcc17b296381c19d2b6598d1 Successfully built neuromancer Installing collected packages: farama-notifications, pygame, jax-jumpy, graphviz, colorama, gymnasium, mlflow, neuromancer Attempting uninstall: colorama Found existing installation: colorama 0.4.6 Uninstalling colorama-0.4.6: Successfully uninstalled colorama-0.4.6 Attempting uninstall: mlflow Found existing installation: mlflow 2.4.1 Uninstalling mlflow-2.4.1: Successfully uninstalled mlflow-2.4.1 Attempting uninstall: neuromancer Found existing installation: neuromancer 1.4.0 Uninstalling neuromancer-1.4.0: Successfully uninstalled neuromancer-1.4.0 Successfully installed colorama-0.4.5 farama-notifications-0.0.4 graphviz-0.20.1 gymnasium-0.28.1 jax-jumpy-1.0.0 mlflow-2.5.0 neuromancer-1.4.0 pygame-2.5.1 ``` -------------------------------- ### Instantiate and Build System Source: https://github.com/pnnl/neuromancer/blob/master/examples/ODEs/scratch/system_id_tutorial.ipynb Instantiates the custom integrator, creates a Node, and constructs a System object with the defined Node. ```python nx = 2 hsize = 64 nlayers = 3 integrator = MultipleShootingEulerIntegrator(nx, hsize, nlayers, sys.ts) node = Node(integrator, ['X', 'xn'], ['xstep', 'xn']) system = System([node]) ``` -------------------------------- ### Initialize SimpleBuildingEnv Source: https://github.com/pnnl/neuromancer/blob/master/examples/domain_examples/RL_DPC_building_control.ipynb Initializes the SimpleBuildingEnv with problem specifications, training data, and data statistics. ```python env = SimpleBuildingEnv(problem_specs, data["train_data"], data_stats) ``` -------------------------------- ### Install Graphviz on MacOS Source: https://github.com/pnnl/neuromancer/blob/master/INSTALLATION.md Installs the Graphviz package using Homebrew on MacOS. ```zsh brew install graphviz ``` -------------------------------- ### Setup Training Parameters and Trainers for FSKAN and KAN Source: https://github.com/pnnl/neuromancer/blob/master/examples/KANs/p2_fbkan_vs_kan_noise_data_2d.ipynb Initializes training parameters, loggers, and trainers for both FSKAN and KAN models. This includes setting the number of epochs, learning rate, and defining the optimizer and data loaders. ```python # Parameters for training num_epochs = 1001 epoch_verbose = 50 # Initial learning rate init_lr = 0.02 # Create loggers logger_fbkan = LossLogger(args=None, savedir='test_fbkan', verbosity=epoch_verbose, stdout=['train_loss','dev_loss']) logger_kan = LossLogger(args=None, savedir='test_kan', verbosity=epoch_verbose, stdout=['train_loss', 'dev_loss']) # Create trainers trainer_fbkan = Trainer( problem_fbkan.to(device), train_data=train_loader, dev_data=dev_loader, optimizer= torch.optim.Adam(problem_fbkan.parameters(), lr=init_lr),#schedule_fbkan.optimizer, epoch_verbose=epoch_verbose, logger=logger_fbkan, epochs=num_epochs, train_metric='train_loss', eval_metric='dev_loss', dev_metric='dev_loss', warmup=num_epochs, device=device ) trainer_kan = Trainer( problem_kan.to(device), train_data=train_loader, dev_data=dev_loader, optimizer= torch.optim.Adam(problem_kan.parameters(), lr=init_lr),#schedule_kan.optimizer, epoch_verbose=epoch_verbose, logger=logger_kan, epochs=num_epochs, train_metric='train_loss', eval_metric='dev_loss', dev_metric='dev_loss', warmup=num_epochs, device=device ) ``` -------------------------------- ### Install Dependencies Source: https://github.com/pnnl/neuromancer/blob/master/examples/psl/signals.ipynb Installs necessary libraries for using Neuromancer, including specific versions of setuptools, pyts, mlflow, plum-dispatch, and torchdiffeq. It also installs the latest version of Neuromancer from its GitHub repository. ```python # Uncomment these pip installs and run this cell for Colab # !pip install setuptools==61.0.0 pyts mlflow plum-dispatch==1.7.2 torchdiffeq # !pip install git+https://github.com/pnnl/neuromancer.git@master --ignore-requires-python --no-deps ``` -------------------------------- ### Initialize Trainer for Network ODE Source: https://github.com/pnnl/neuromancer/blob/master/examples/ODEs/Part_6_NetworkODE.ipynb Sets up the trainer with the defined problem, data loaders, optimizer, and logging parameters. The trainer is configured for a specific number of epochs and patience for early stopping. ```python problem = Problem([dynamics_model], loss) optimizer = torch.optim.Adam(problem.parameters(), lr=0.01) logger = BasicLogger(args=None, savedir='test', verbosity=1, stdout=["dev_loss","train_loss"]) trainer = Trainer( problem, train_loader, dev_loader, test_data, optimizer, epochs=1000, patience=20, warmup=5, eval_metric="dev_loss", train_metric="train_loss", dev_metric="dev_loss", test_metric="dev_loss", logger=logger, ) ``` -------------------------------- ### Install NeuroMANCER Source: https://github.com/pnnl/neuromancer/blob/master/examples/ODEs/Part_2_param_estim_ODE.ipynb Installs the NeuroMANCER library. This is typically used when running in a Colab environment. ```python !pip install neuromancer ``` -------------------------------- ### Initialize and Simulate Gravitational System Source: https://github.com/pnnl/neuromancer/blob/master/examples/psl/coupled_systems.ipynb Sets up a gravitational system simulation with a given gravitational constant and number of bodies. Initializes positions and velocities, then simulates the system. ```python #adj = np.array([[0,1],[0,2],[0,3],[1,0],[2,0],[3,0]]).T # nsim = 10000 network = Gravitational_System(G=6.67e-11, nx=4) ninit = np.array([[1000000, 0, 0, 0, 0], [1, 1, 0, 0, 8.167e-3], [1, 0, 2, 4.0835e-3, 0], [1, -1, -1, 4e-3, -4e-3]]) Sim = network.simulate(nsim=nsim, x0=ninit) sim=Sim['Y'] ``` -------------------------------- ### Install Neuromancer Source: https://github.com/pnnl/neuromancer/blob/master/examples/KANs/p1_fbkan_vs_kan_noise_data_1d.ipynb Installs the Neuromancer library from a specific branch. This may require restarting the Colab session. ```python # import os # # Check if the neuromancer directory already exists # if not os.path.isdir('neuromancer'): # # Clone the specific branch of the repository # !git clone --branch feature/fbkans https://github.com/pnnl/neuromancer.git # # Navigate to the repository directory # %cd neuromancer # # Install the repository with the required extras # !pip install -e .[docs,tests,examples] ``` -------------------------------- ### Set Up Optimizer and Trainer Source: https://github.com/pnnl/neuromancer/blob/master/examples/ODEs/Part_3_UDE.ipynb Configures the Adam optimizer for the problem parameters and initializes the Trainer with data loaders, metrics, and training parameters. ```python optimizer = torch.optim.Adam(problem.parameters(), lr=0.003) trainer = Trainer( problem, train_loader, dev_loader, test_data, optimizer, patience=50, warmup=100, epochs=1000, eval_metric="dev_loss", train_metric="train_loss", dev_metric="dev_loss", test_metric="dev_loss", ) ``` -------------------------------- ### Install TorchSDE Source: https://github.com/pnnl/neuromancer/blob/master/examples/SDEs/README.md Install the TorchSDE library using pip. This is a prerequisite for using its functionality within the NeuroMANCER workflow. ```bash pip install torchsde ``` -------------------------------- ### Configure Training Parameters and Initialize Trainers Source: https://github.com/pnnl/neuromancer/blob/master/examples/KANs/p1_fbkan_vs_kan_noise_data_1d.ipynb Sets up training parameters such as epochs and learning rate, creates loggers, and initializes Adam optimizers and trainers for both FBKAN and KAN models. ```python # Parameters for training num_epochs = 2000 epoch_verbose = 50 # Initial learning rate init_lr = 1e-2 # Create loggers logger_fbkan = LossLogger(args=None, savedir='test_fbkan', verbosity=epoch_verbose, stdout=['train_loss','dev_loss']) logger_kan = LossLogger(args=None, savedir='test_kan', verbosity=epoch_verbose, stdout=['train_loss', 'dev_loss']) # Create trainers trainer_fbkan = Trainer( problem_fbkan.to(device), train_data=train_loader, dev_data=dev_loader, optimizer= torch.optim.Adam(problem_fbkan.parameters(), lr=init_lr), epoch_verbose=epoch_verbose, logger=logger_fbkan, epochs=num_epochs, train_metric='train_loss', eval_metric='dev_loss', dev_metric='dev_loss', warmup=num_epochs, device=device ) trainer_kan = Trainer( problem_kan.to(device), train_data=train_loader, dev_data=dev_loader, optimizer= torch.optim.Adam(problem_kan.parameters(), lr=init_lr), epoch_verbose=epoch_verbose, logger=logger_kan, epochs=num_epochs, train_metric='train_loss', eval_metric='dev_loss', dev_metric='dev_loss', warmup=num_epochs, device=device ) ``` -------------------------------- ### Install Graphviz on Linux (Debian, Ubuntu) Source: https://github.com/pnnl/neuromancer/blob/master/INSTALLATION.md Installs the Graphviz system package using apt for Debian-based Linux distributions. ```bash sudo apt install graphviz ``` -------------------------------- ### Define Data Setup Function Signature Source: https://github.com/pnnl/neuromancer/blob/master/examples/lightning_integration_examples/README.md The user is expected to define a data_setup_function that accepts arbitrary keyword arguments and returns training, validation, and test datasets along with the batch size. ```python def data_setup_function(*kwargs): # insert data generation code here ``` -------------------------------- ### Cloning Neuromancer Repository Source: https://github.com/pnnl/neuromancer/blob/master/examples/control/Part_2_stabilize_ODE.ipynb This command is used to clone the Neuromancer repository from GitHub. It is typically executed during the installation process when installing from source. ```bash Running command git clone --filter=blob:none --quiet https://github.com/pnnl/neuromancer.git 'C:\Users\drgo694\AppData\Local\Temp\pip-install-grude3rc\neuromancer_a948fdde05f447218e6f26c764267870' ``` -------------------------------- ### Create Training and Development Environments Source: https://github.com/pnnl/neuromancer/blob/master/examples/domain_examples/RL_DPC_building_control.ipynb Initializes separate SimpleBuildingEnv instances for training and development using generated data. ```python train_env = SimpleBuildingEnv(problem_specs, data["train_data"], data_stats) dev_env = SimpleBuildingEnv(problem_specs, data["dev_data"], data_stats) ``` -------------------------------- ### Install Sphinx and RTD Theme with Conda Source: https://github.com/pnnl/neuromancer/blob/master/docs/readme.md Installs Sphinx and the Read the Docs theme using Conda, essential for autogenerating documentation. ```bash $ conda activate neuromancer $ conda install sphinx -c anaconda $ conda install -c conda-forge sphinx_rtd_theme ``` -------------------------------- ### Install NeuroMANCER GPT Dependencies Source: https://github.com/pnnl/neuromancer/blob/master/assistant/README.md Install the required pip packages for the NeuroMANCER GPT script. These include pypandoc, panflute, and tqdm. ```sh pip install pypandoc panflute tqdm ``` -------------------------------- ### Define Data Setup Function Source: https://github.com/pnnl/neuromancer/blob/master/examples/lightning_integration_examples/Part_1_lightning_basics_tutorial.ipynb Creates and returns training, development, and testing datasets along with the batch size. Datasets are Neuromancer DictDatasets sampled from uniform parameter distributions. ```python def data_setup_function(nsim, a_low, a_high, p_low, p_high): samples_train = {"a": torch.FloatTensor(nsim, 1).uniform_(a_low, a_high), "p": torch.FloatTensor(nsim, 1).uniform_(p_low, p_high)} samples_dev = {"a": torch.FloatTensor(nsim, 1).uniform_(a_low, a_high), "p": torch.FloatTensor(nsim, 1).uniform_(p_low, p_high)} samples_test = {"a": torch.FloatTensor(nsim, 1).uniform_(a_low, a_high), "p": torch.FloatTensor(nsim, 1).uniform_(p_low, p_high)} # create named dictionary datasets train_data = DictDataset(samples_train, name='train') dev_data = DictDataset(samples_dev, name='dev') test_data = DictDataset(samples_test, name='test') batch_size = 64 # Return the dict datasets in train, dev, test order, followed by batch_size return train_data, dev_data, test_data, batch_size ``` -------------------------------- ### Initialize Data Loaders Source: https://github.com/pnnl/neuromancer/blob/master/examples/domain_examples/DPC_building_control.ipynb Sets up prediction horizon, number of samples, and batch size, then creates training and development data loaders using the `get_data` function. ```python nsteps = 100 # prediction horizon n_samples = 1000 # number of sampled scenarios batch_size = 100 # range for lower comfort bound xmin_range = torch.distributions.Uniform(18., 22.) train_loader, dev_loader = [ get_data(sys, nsteps, n_samples, xmin_range, batch_size, name=name) for name in ("train", "dev") ] ``` -------------------------------- ### Install NeuroMANCER with Conda Source: https://github.com/pnnl/neuromancer/blob/master/USER_GUIDE.md Sets up a dedicated virtual environment for development using Conda, clones the repository, and installs the library with development dependencies. ```bash # Create a virtual environment using Conda conda create -n neuromancer-dev python=3.11 conda activate neuromancer-dev git clone -b master https://github.com/pnnl/neuromancer.git --single-branch cd neuromancer # Install dependencies pip install -e .[docs,tests,examples] ``` -------------------------------- ### Environment Reset and Step Source: https://github.com/pnnl/neuromancer/blob/master/examples/domain_examples/RL_DPC_building_control.ipynb Demonstrates resetting the environment to its initial state and taking a step using a randomly sampled action. ```python env.reset() #resets the environment and returns the initial observation of the agent env.step(env.action_space.sample()) #takes a step forward with a randomly sampled action ``` -------------------------------- ### Install Neuromancer and Lightning in Colab Source: https://github.com/pnnl/neuromancer/blob/master/examples/lightning_integration_examples/other_examples/lightning_cvxpy_layers.ipynb Use these commands to install the Neuromancer library from GitHub and the Lightning library when running in a Google Colab environment. Skip this step if running locally. ```python !pip install "neuromancer[examples] @ git+https://github.com/pnnl/neuromancer.git@master" !pip install lightning ``` -------------------------------- ### Install PyTorch with CUDA 12.1 via PIP Source: https://github.com/pnnl/neuromancer/blob/master/INSTALLATION.md Use this command to install PyTorch with CUDA 12.1 support when using PIP. It's recommended to uninstall existing PyTorch first. ```bash pip uninstall torch -y pip install torch --index-url https://download.pytorch.org/whl/cu121 ``` -------------------------------- ### Closed-Loop Simulation Setup and Execution Source: https://github.com/pnnl/neuromancer/blob/master/examples/domain_examples/DPC_building_control.ipynb Sets up and executes a closed-loop simulation for building control. It generates reference and disturbance signals, initializes the system state, and performs the simulation. ```python nsteps_test = 2000 # generate reference np_refs = psl.signals.step(nsteps_test+1, 1, min=18., max=22., randsteps=5) ymin_val = torch.tensor(np_refs, dtype=torch.float32).reshape(1, nsteps_test+1, 1) ymax_val = ymin_val+2.0 # generate disturbance signal torch_dist = torch.tensor(sys.get_D(nsteps_test+1)).unsqueeze(0) # initial data for closed loop simulation x0 = torch.tensor(sys.get_x0()).reshape(1, 1, nx) data = {'x': x0, 'y': x0[:, :, [-1]], 'ymin': ymin_val, 'ymax': ymax_val, 'd': torch_dist} cl_system.nsteps = nsteps_test # perform closed-loop simulation trajectories = cl_system(data) ``` -------------------------------- ### Initialize Optimizer, Logger, and Trainer Source: https://github.com/pnnl/neuromancer/blob/master/examples/ODEs/scratch/duffing_parameter_interactive.ipynb Sets up the Adam optimizer, a basic logger for tracking training progress, and the Trainer object with specified data, metrics, and device. ```python optimizer = torch.optim.Adam(problem.parameters(), lr=0.1) logger = BasicLogger(args=None, savedir='test', verbosity=1, stdout="nstep_dev_"+reference_loss.output_keys[0]) trainer = Trainer( problem, train_data, dev_data, test_data, optimizer, patience=20, warmup=100, epochs=200, eval_metric="nstep_dev_"+reference_loss.output_keys[0], train_metric="nstep_train_loss", dev_metric="nstep_dev_loss", test_metric="nstep_test_loss", logger=logger, device=device, ) ``` -------------------------------- ### Manual Conda Dependency Installation (Ubuntu/Other) Source: https://github.com/pnnl/neuromancer/blob/master/INSTALLATION.md Manually installs core dependencies for Neuromancer using Conda, including PyTorch, SciPy, NumPy, and others. Pay attention to comments for non-Linux OS. ```bash conda create -n neuromancer python=3.10.4 conda activate neuromancer conda install pytorch pytorch-cuda=11.6 -c pytorch -c nvidia ## OR (for Mac): conda install pytorch -c pytorch conda config --append channels conda-forge conda install scipy numpy"<1.24.0" matplotlib scikit-learn pandas dill mlflow pydot=1.4.2 pyts numba conda install networkx=3.0 plum-dispatch=1.7.3 conda install -c anaconda pytest hypothesis conda install cvxpy cvxopt casadi seaborn imageio conda install tqdm torchdiffeq toml conda install lightning wandb -c conda-forge ## (for Windows): conda install -c defaults intel-openmp -f ``` -------------------------------- ### Launch Tensorboard Session Source: https://github.com/pnnl/neuromancer/blob/master/examples/lightning_integration_examples/README.md Commands to launch a Tensorboard session to visualize training progress logged in the 'lightning_logs' directory. Assumes Tensorboard plugin is installed in VS Code. ```bash %reload_ext tensorboard %tensorboard --logdir=lightning_logs/ ```