SGIRMolecularPredictor

Semi-supervised learning for imbalanced regression targets.

from torch_molecule import SGIRMolecularPredictor

Examples · Methods

Constructor

class SGIRMolecularPredictor(num_anchor=10, warmup_epoch=20, labeling_interval=5, augmentation_interval=5, top_quantile=0.1, label_logscale=False, lw_aug=1, gamma=0.4, num_task=1, task_type='regression', num_layer=5, hidden_size=300, gnn_type='gin-virtual', drop_ratio=0.5, norm_layer='batch_norm', graph_pooling='sum', augmented_feature=None, batch_size=128, epochs=500, learning_rate=0.001, weight_decay=0.0, grad_clip_value=None, patience=50, use_lr_scheduler=False, scheduler_factor=0.5, scheduler_patience=5, loss_criterion=None, evaluate_criterion=None, evaluate_higher_better=None, verbose='none', device=None, model_name='SGIRMolecularPredictor')[source]

This predictor implements SGIR for semi-supervised graph imbalanced regression.

It trains the GREA model based on pseudo-labeling and data augmentation.

References

Parameters:
  • num_anchor (int, default=10) – Number of anchor points used to split the label space during pseudo-labeling.

  • warmup_epoch (int, default=20) – Number of epochs to train before starting pseudo-labeling and data augmentation.

  • labeling_interval (int, default=5) – Interval (in epochs) between two pseudo-labeling steps. It controls the update frequency of pseudo-labeling.

  • augmentation_interval (int, default=5) – Interval (in epochs) between two data augmentation steps. It controls the update frequency of data augmentation.

  • top_quantile (float, default=0.1) – Quantile threshold for selecting high confidence predictions during pseudo-labeling.

  • label_logscale (bool, default=False) – Whether to use log scale for the label space during pseudo-labeling and data augmentation.

  • lw_aug (float, default=1) – Loss weight for the augmented data.

  • gamma (float, default=0.4) – GREA-specific parameter that penalize the size of the rationales (ratio between the number of nodes in the rationales and the number of nodes in the original graph).

  • num_task (int, default=1) – Number of prediction tasks. SGIR currently only supports regression tasks with 1 task.

  • task_type (str, default="regression") – Type of prediction task, either “regression” or “classification”.

  • num_layer (int, default=5) – Number of GNN layers.

  • hidden_size (int, default=300) – Dimension of hidden node features.

  • gnn_type (str, default="gin-virtual") – Type of GNN architecture to use. One of [“gin-virtual”, “gcn-virtual”, “gin”, “gcn”].

  • drop_ratio (float, default=0.5) – Dropout probability.

  • norm_layer (str, default="batch_norm") – Type of normalization layer to use. One of [“batch_norm”, “layer_norm”, “instance_norm”, “graph_norm”, “size_norm”, “pair_norm”].

  • graph_pooling (str, default="sum") – Method for aggregating node features to graph-level representations. One of [“sum”, “mean”, “max”].

  • augmented_feature (list or None, default=None) – Additional molecular fingerprints to use as features. It will be concatenated with the graph representation after pooling. Examples like [“morgan”, “maccs”] or None.

  • batch_size (int, default=128) – Number of samples per batch for training.

  • epochs (int, default=500) – Maximum number of training epochs.

  • loss_criterion (callable, optional) – Loss function for training.

  • evaluate_criterion (str or callable, optional) – Metric for model evaluation.

  • evaluate_higher_better (bool, optional) – Whether higher values of the evaluation metric are better.

  • learning_rate (float, default=0.001) – Learning rate for optimizer.

  • grad_clip_value (float, optional) – Maximum norm of gradients for gradient clipping.

  • weight_decay (float, default=0.0) – L2 regularization strength.

  • patience (int, default=50) – Number of epochs to wait for improvement before early stopping.

  • use_lr_scheduler (bool, default=False) – Whether to use learning rate scheduler.

  • scheduler_factor (float, default=0.5) – Factor by which to reduce learning rate when plateau is reached.

  • scheduler_patience (int, default=5) – Number of epochs with no improvement after which learning rate will be reduced.

  • verbose (str, default="none") – Whether to display progress info. Options are: “none”, “progress_bar”, “print_statement”. If any other, “none” is automatically chosen.

  • device (torch.device or str, optional) – Device to run the model on. If None, will auto-detect GPU or use CPU.

  • model_name (str, default="SGIRMolecularPredictor") – Name identifier for the model.

Example

examples/prediction/sgir.py
import numpy as np
from torch_molecule import SGIRMolecularPredictor
from torch_molecule.utils.search import ParameterType, ParameterSpec
import os

def test_sgir_predictor():
    # Test data
    smiles_list = [
        'CNC[C@H]1OCc2cnnn2CCCC(=O)N([C@H](C)CO)C[C@@H]1C',
        'CNC[C@@H]1OCc2cnnn2CCCC(=O)N([C@H](C)CO)C[C@H]1C',
        'C[C@H]1CN([C@@H](C)CO)C(=O)CCCn2cc(nn2)CO[C@@H]1CN(C)C(=O)CCC(F)(F)F',
        'CC1=CC=C(C=C1)C2=CC(=NN2C3=CC=C(C=C3)S(=O)(=O)N)C(F)(F)F'
    ]
    properties = np.array([1.2, 2.3, 3.4, 4.5])  # Regression values
    unlabeled_smiles = [
        'CC1=CC=C(C=C1)C2=CC(=NN2C3=CC=C(C=C3)S(=O)(=O)N)C(F)(F)F',
        'CNC[C@H]1OCc2cnnn2CCCC(=O)N([C@H](C)CO)C[C@@H]1C'
    ]

    # Initialize model
    print("\n=== Testing SGIR model initialization ===")
    model = SGIRMolecularPredictor(
        num_task=1,
        task_type="regression",
        gamma=0.8,
        num_layer=3,
        hidden_size=128,
        batch_size=4,
        epochs=20,
        verbose="progress_bar",
        warmup_epoch=5,
        labeling_interval=1,
        augmentation_interval=1
    )

    # Test fitting with unlabeled data
    print("\n=== Testing SGIR model fitting with unlabeled data ===")
    model.fit(smiles_list[:3], properties[:3], X_unlbl=unlabeled_smiles)

    # Test prediction
    print("\n=== Testing SGIR model prediction ===")
    predictions = model.predict(smiles_list[3:])
    print(f"Prediction shape: {predictions['prediction'].shape}")
    print(f"Prediction value: {predictions['prediction']}")

    # Test auto-fitting
    search_parameters = {
        'num_layer': ParameterSpec(
            param_type=ParameterType.INTEGER,
            value_range=(2, 4)
        ),
        'hidden_size': ParameterSpec(
            param_type=ParameterType.INTEGER,
            value_range=(64, 256)
        ),
        'learning_rate': ParameterSpec(
            param_type=ParameterType.LOG_FLOAT,
            value_range=(1e-4, 1e-2)
        )
    }
    
    model_auto = SGIRMolecularPredictor(
        num_task=1,
        task_type="regression",
        epochs=3,
        verbose="progress_bar"
    )
    
    model_auto.autofit(
        smiles_list,
        properties,
        X_unlbl=unlabeled_smiles,
        search_parameters=search_parameters,
        n_trials=2
    )

    # Test save/load
    save_path = "test_sgir_model.pt"
    model.save(save_path)
    
    new_model = SGIRMolecularPredictor(
        num_task=1,
        task_type="regression"
    )
    new_model.load(save_path)

    # # Test invalid SMILES
    # try:
    #     invalid_smiles = ['CC', 'INVALID_SMILES']
    #     predictions = model.predict(invalid_smiles)
    # except Exception as e:
    #     print(f"Invalid SMILES handled with error: {str(e)}")

    # Clean up
    if os.path.exists(save_path):
        os.remove(save_path)

if __name__ == "__main__":
    test_sgir_predictor()

Methods

fit

Fit the model to training data with optional validation set.

autofit

Automatically find the best hyperparameters using Optuna optimization.

predict

Make predictions using the fitted model.

For shared checkpoint and parameter operations, see Base Modules.

fit

SGIRMolecularPredictor.fit(X_train, y_train, X_val=None, y_val=None, X_unlbl=None)[source]

Fit the model to training data with optional validation set.

Parameters:
  • X_train (List[str])

  • y_train (List | ndarray | None)

  • X_val (List[str] | None)

  • y_val (List | ndarray | None)

  • X_unlbl (List[str] | None)

Return type:

SGIRMolecularPredictor

autofit

SGIRMolecularPredictor.autofit(X_train, y_train, X_val=None, y_val=None, X_unlbl=None, search_parameters=None, n_trials=10)

Automatically find the best hyperparameters using Optuna optimization.

Parameters:
  • X_train (List[str])

  • y_train (List | ndarray | None)

  • X_val (List[str] | None)

  • y_val (List | ndarray | None)

  • X_unlbl (List[str] | None)

  • search_parameters (Dict[str, ParameterSpec] | None)

  • n_trials (int)

Return type:

GNNMolecularPredictor

predict

SGIRMolecularPredictor.predict(X)

Make predictions using the fitted model.

Parameters:

X (List[str]) – List of SMILES strings to make predictions for

Returns:

Dictionary containing:
  • ’prediction’: Model predictions (shape: [n_samples, n_tasks])

  • ’variance’: Prediction variances (shape: [n_samples, n_tasks])

  • ’node_importance’: A nested list where the outer list has length n_samples and each inner list has length n_nodes for that molecule

Return type:

Dict[str, np.ndarray]