### Configure ReactionTrainer Source: https://context7.com/mirunacrt/synflownet/llms.txt Provides the setup pattern for the ReactionTrainer class, including configuration initialization and hardware selection for the training loop. ```python config = init_empty(Config()) config.log_dir = "./logs/synflownet_run" config.device = "cuda" if torch.cuda.is_available() else "cpu" config.num_training_steps = 10000 ``` -------------------------------- ### Compute Molecule Embeddings and Use Reaction Templates (Python) Source: https://context7.com/mirunacrt/synflownet/llms.txt Illustrates utilities for computing molecular embeddings and fingerprints, and for working with reaction templates. It shows how to get embeddings for a list of building blocks and how to check if a molecule can participate in or be produced by a reaction. ```python from synflownet.utils.synthesis_utils import get_mol_embeddings, Reaction from rdkit import Chem # Compute fingerprint embeddings for building blocks building_blocks = ["CCO", "c1ccccc1", "CC(=O)O"] embeddings = get_mol_embeddings( building_blocks, fp_type="morgan_1024", # Options: morgan_1024, morgan_2048 fp_path=None # Path to precomputed fingerprints ) # Work with reaction templates template = "[C:1]([OH:2])>>[C:1]([O:2])" # Example SMIRKS reaction = Reaction(template=template) # Check if molecule can participate in reaction mol = Chem.MolFromSmiles("CCO") can_react = reaction.is_reactant(mol) can_produce = reaction.is_product(mol) # Run reaction if can_react: products = reaction.run_reactants((mol,)) ``` -------------------------------- ### Initialize and Run GFlowNet Trainer Source: https://context7.com/mirunacrt/synflownet/llms.txt Demonstrates how to configure the trainer, execute the training loop, and load models from checkpoints. This is the entry point for training GFlowNet models for reaction synthesis. ```python config.reward = "seh_reaction" config.algo.method = "TB" config.algo.max_len = 3 config.algo.num_from_policy = 64 config.algo.tb.do_parameterize_p_b = True trainer = ReactionTrainer(config) trainer.run() # Resume from checkpoint trainer = ReactionTrainer.load_from_checkpoint("./logs/model_state.pt") trainer.run() ``` -------------------------------- ### Initialize and Use ReactionTemplateEnvContext Source: https://context7.com/mirunacrt/synflownet/llms.txt Demonstrates how to configure the environment context, convert between RDKit/NetworkX representations, and generate action masks for reaction modeling. ```python ctx = ReactionTemplateEnvContext(num_cond_dim=32, strict_bck_masking=True) mol = Chem.MolFromSmiles("CCO") graph = ctx.obj_to_graph(mol) torch_data = ctx.graph_to_Data(graph, traj_len=1) uni_mask = ctx.create_masks(graph, GraphActionType.ReactUni, traj_len=1) batch = ctx.collate([torch_data]) ``` -------------------------------- ### Implement Reaction Synthesis Environment Source: https://context7.com/mirunacrt/synflownet/llms.txt Illustrates the Markov Decision Process (MDP) for molecule generation, including forward steps for adding reactants and applying reactions, as well as backward steps for molecule decomposition. ```python from synflownet.envs.synthesis_building_env import ReactionTemplateEnv from synflownet.envs.graph_building_env import GraphAction, GraphActionType env = ReactionTemplateEnv() graph = env.empty_graph() # Add first reactant action = GraphAction(action=GraphActionType.AddFirstReactant, bb=0) mol = env.step(graph, action) # Apply reaction action = GraphAction(action=GraphActionType.ReactUni, rxn=0) product = env.step(mol, action) # Backward step bck_action = GraphAction(action=GraphActionType.BckReactBi, rxn=1) parent_mol, _, _ = env.backward_step(product, bck_action) ``` -------------------------------- ### Sample Conditional Information and Transform Rewards (Python) Source: https://context7.com/mirunacrt/synflownet/llms.txt Demonstrates how to sample conditional information for reward transformation and then transform rewards to log-rewards using temperature. This is useful for setting up training objectives. ```python cond_info = task.sample_conditional_information(n=64, train_it=1000) log_rewards = task.cond_info_to_logreward(cond_info, obj_props) ``` -------------------------------- ### Set Reaction Task Configurations Source: https://context7.com/mirunacrt/synflownet/llms.txt Shows how to define paths for building blocks, reaction templates, and precomputed masks required for the synthesis environment. ```python from synflownet.tasks.config import ReactionTaskConfig ReactionTaskConfig.building_blocks_filename = "building_blocks.txt" ReactionTaskConfig.templates_filename = "hb.txt" ReactionTaskConfig.precomputed_bb_masks_filename = "precomputed_bb_masks.pkl" ReactionTaskConfig.sanitize_building_blocks = True ``` -------------------------------- ### Configure SynFlowNet Training Parameters Source: https://context7.com/mirunacrt/synflownet/llms.txt Demonstrates how to initialize and customize the training configuration using the Config dataclass. It covers optimizer settings, algorithm parameters, model architecture, and reward function selection. ```python from synflownet.config import Config, init_empty, override_config import torch config = init_empty(Config()) config.log_dir = "./logs/my_experiment" config.device = "cuda" if torch.cuda.is_available() else "cpu" config.num_training_steps = 5000 config.opt.learning_rate = 1e-4 config.algo.method = "TB" config.model.num_layers = 4 config.reward = "seh_reaction" ``` -------------------------------- ### Configure and Execute GraphTransformerSynGFN Source: https://context7.com/mirunacrt/synflownet/llms.txt Shows initialization of the GFlowNet policy network using a graph transformer, performing forward passes, and sampling actions from the resulting distributions. ```python model = GraphTransformerSynGFN(env_ctx=ctx, cfg=config, num_graph_out=2, do_bck=True) fwd_cat, bck_cat, graph_out = model(batch, cond_info) actions = fwd_cat.sample(nx_graphs=nx_graphs, model=model, random_action_prob=0.01) log_probs = fwd_cat.log_prob(actions=actions, nx_graphs=nx_graphs, model=model) ``` -------------------------------- ### ReactionTrainer Configuration and Usage Source: https://context7.com/mirunacrt/synflownet/llms.txt This section details the configuration parameters for the ReactionTrainer and its basic usage for training and resuming from checkpoints. ```APIDOC ## ReactionTrainer Configuration and Usage ### Description Configuration options for reward functions, algorithms (like TrajectoryBalance), and replay buffers, along with initialization and running of the `ReactionTrainer`. ### Method N/A (Configuration and Initialization) ### Endpoint N/A (Local Script Execution) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```python # Configuration config.reward = "seh_reaction" # Binding affinity proxy config.algo.method = "TB" config.algo.max_len = 3 config.algo.num_from_policy = 64 config.algo.tb.do_parameterize_p_b = True config.replay.use = False config.replay.capacity = 10000 # Initialize and run trainer trainer = ReactionTrainer(config) trainer.run() # Resume from checkpoint trainer = ReactionTrainer.load_from_checkpoint("./logs/model_state.pt") trainer.run() ``` ### Response #### Success Response (200) N/A (Local Script Execution) #### Response Example N/A ``` -------------------------------- ### SynthesisSampler - Trajectory Generation Source: https://context7.com/mirunacrt/synflownet/llms.txt Documentation for the SynthesisSampler class, used for generating molecular trajectories by sampling from the learned policy. ```APIDOC ## SynthesisSampler - Trajectory Generation ### Description Generates molecular trajectories by sampling from the learned policy. Supports sampling both forward and backward trajectories, with options for controlling sampling behavior. ### Method N/A (Class Implementation) ### Endpoint N/A (Class Implementation) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```python from synflownet.algo.reaction_sampling import SynthesisSampler import torch sampler = SynthesisSampler( cfg=config, ctx=ctx, env=env, max_len=3, correct_idempotent=False, pad_with_terminal_state=True ) cond_info = torch.randn(64, 32) data = sampler.sample_from_model( model=model, n=64, cond_info=cond_info, random_action_prob=0.0, use_argmax=False ) # Sample backward trajectories graphs = [ctx.obj_to_graph(mol) for mol in molecules] bck_data = sampler.sample_backward_from_graphs( graphs=graphs, model=model, cond_info=cond_info, random_action_prob=0.0 ) ``` ### Response #### Success Response (200) N/A (Class Implementation) #### Response Example N/A ``` -------------------------------- ### Manage Hierarchical Actions with ActionCategorical Source: https://context7.com/mirunacrt/synflownet/llms.txt Illustrates the manual construction and usage of ActionCategorical to handle complex hierarchical action spaces, including sampling and log-probability computation. ```python action_cat = ActionCategorical(graphs=batch, graph_embeddings=graph_emb, raw_logits=[stop_logits, uni_logits, bi_logits, add_first_logits], types=[GraphActionType.Stop, GraphActionType.ReactUni, GraphActionType.ReactBi, GraphActionType.AddFirstReactant], action_masks=[stop_mask, uni_mask, bi_mask, add_first_mask], fwd=True) actions = action_cat.sample(nx_graphs=nx_graphs, model=model, use_argmax=False) log_probs = action_cat.log_prob(actions=actions, nx_graphs=nx_graphs, model=model) ``` -------------------------------- ### Configure and Execute Trajectory Balance Algorithm Source: https://context7.com/mirunacrt/synflownet/llms.txt Configures the TrajectoryBalance algorithm, including loss variants and backward policy settings. It shows how to generate training data from samples and compute batch losses. ```python from synflownet.algo.trajectory_balance import TrajectoryBalance algo = TrajectoryBalance(env, ctx, config, sampler) # Create training data data = algo.create_training_data_from_own_samples(model=model, n=64, cond_info=cond_info) # Construct batch and compute losses batch = algo.construct_batch(trajs=[d["traj"] for d in data], cond_info=cond_info, log_rewards=log_rewards) loss, info = algo.compute_batch_losses(model, batch) ``` -------------------------------- ### Compute Rewards with ReactionTask Source: https://context7.com/mirunacrt/synflownet/llms.txt Computes objective properties and rewards for molecular graphs using various proxy models like SEH, QED, or Vina. It demonstrates how to initialize the task and process a batch of molecules. ```python from synflownet.tasks.reactions_task import ReactionTask from rdkit import Chem task = ReactionTask(cfg=config, wrap_model=lambda x: x) mols = [Chem.MolFromSmiles(s) for s in ["CCO", "c1ccccc1"]] traj_lens = torch.tensor([2, 3]) obj_props, is_valid = task.compute_obj_properties(mols, traj_lens) ``` -------------------------------- ### ReactionTask - Reward Computation Source: https://context7.com/mirunacrt/synflownet/llms.txt Details on the ReactionTask class for computing rewards using various proxy models and scoring functions. ```APIDOC ## ReactionTask - Reward Computation ### Description Handles reward computation for generated molecules using different proxy models and scoring functions, such as SEH binding affinity, QED, and Vina docking scores. ### Method N/A (Class Implementation) ### Endpoint N/A (Class Implementation) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```python from synflownet.tasks.reactions_task import ReactionTask from synflownet.config import Config from rdkit import Chem config = Config() config.reward = "seh_reaction" task = ReactionTask(cfg=config, wrap_model=lambda x: x) mols = [Chem.MolFromSmiles(s) for s in ["CCO", "c1ccccc1", "CC(=O)O"]] traj_lens = torch.tensor([2, 3, 2]) obj_props, is_valid = task.compute_obj_properties(mols, traj_lens) # Example for Vina docking configuration: config.vina.target = "kras" config.vina.vina_path = "bin/QuickVina2-GPU-2-1" ``` ### Response #### Success Response (200) N/A (Class Implementation) #### Response Example N/A ``` -------------------------------- ### Generate Forward and Backward Trajectories (Python) Source: https://context7.com/mirunacrt/synflownet/llms.txt Provides utility functions for generating forward and backward molecular trajectories. It shows how to initialize the environment context and generate trajectories of a specified length, then iterate through them to extract molecule information. ```python from synflownet.envs.synthesis_building_env import ( generate_forward_trajectory, generate_backward_trajectory, ReactionTemplateEnvContext, ReactionTemplateEnv ) from rdkit import Chem # Generate a random forward trajectory ctx = ReactionTemplateEnvContext() env = ReactionTemplateEnv() fwd_traj = generate_forward_trajectory( traj_len=5, # Maximum steps ctx=ctx, env=env ) # Each step is (graph, action) for graph, action in fwd_traj: mol = ctx.graph_to_obj(graph) print(f"Action: {action.action}, SMILES: {Chem.MolToSmiles(mol)}") # Generate backward trajectory from a molecule start_smiles = "c1ccc(CC(=O)O)cc1" bck_traj = generate_backward_trajectory( traj_len=5, s=start_smiles, ctx=ctx, env=env ) ``` -------------------------------- ### Generate Molecular Trajectories with SynthesisSampler Source: https://context7.com/mirunacrt/synflownet/llms.txt Uses SynthesisSampler to generate forward and backward molecular trajectories based on a learned policy. It extracts trajectory data including validity and building block usage. ```python from synflownet.algo.reaction_sampling import SynthesisSampler sampler = SynthesisSampler(cfg=config, ctx=ctx, env=env, max_len=3) # Sample forward trajectories data = sampler.sample_from_model(model=model, n=64, cond_info=torch.randn(64, 32)) # Sample backward trajectories bck_data = sampler.sample_backward_from_graphs(graphs=graphs, model=model, cond_info=cond_info) ``` -------------------------------- ### TrajectoryBalance - GFlowNet Loss Algorithm Source: https://context7.com/mirunacrt/synflownet/llms.txt Details on the TrajectoryBalance class for implementing GFlowNet objectives, including configuration options and usage in training. ```APIDOC ## TrajectoryBalance - GFlowNet Loss Algorithm ### Description Implements the trajectory balance objective and its variants (SubTB, DB) for training GFlowNet models. It handles configuration for backward policies, loss functions, and training data preparation. ### Method N/A (Class Implementation) ### Endpoint N/A (Class Implementation) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```python from synflownet.algo.trajectory_balance import TrajectoryBalance from synflownet.config import Config config = Config() config.algo.tb.do_parameterize_p_b = True config.algo.tb.do_sample_p_b = False config.algo.tb.do_predict_n = True config.algo.tb.do_correct_idempotent = False config.algo.tb.epsilon = None config.algo.tb.Z_learning_rate = 1e-3 config.algo.tb.bootstrap_own_reward = False config.algo.tb.loss_fn = "MSE" config.algo.tb.variant = "TB" config.algo.tb.backward_policy = "MaxLikelihood" algo = TrajectoryBalance(env, ctx, config, sampler) data = algo.create_training_data_from_own_samples( model=model, n=64, cond_info=cond_info, random_action_prob=0.01 ) batch = algo.construct_batch( trajs=[d["traj"] for d in data], cond_info=cond_info, log_rewards=log_rewards ) loss, info = algo.compute_batch_losses(model, batch) ``` ### Response #### Success Response (200) N/A (Class Implementation) #### Response Example N/A ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.