### Install PyTorch and verify version Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Drug_Discovery.ipynb Installs PyTorch and torchvision, essential libraries for deep learning, and then prints the installed PyTorch version. This confirms successful installation and compatibility. ```python !pip install torch torchvision !python -c "import torch; print(torch.__version__)" ``` -------------------------------- ### Install PyTorch and PyTorch Geometric Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Graph_Autoencoder.ipynb This command installs the PyTorch and PyTorch Geometric libraries, which are essential for working with graph neural networks. The installation process may fetch specific versions based on the environment's requirements. ```python !pip install torch torch_geometric ``` -------------------------------- ### Install and Print PyTorch Version Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Graph_Autoencoder.ipynb Installs required packages and prints the PyTorch version. It sets the TORCH environment variable to the PyTorch version for potential downstream use. Dependencies include os, torch, matplotlib, numpy, and pandas. ```python # Install required packages. import os import torch import matplotlib.pyplot as plt import numpy as np import pandas as pd os.environ['TORCH'] = torch.__version__ print(torch.__version__) ``` -------------------------------- ### Install RDKit with pip Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Drug_Discovery.ipynb Installs the RDKit package, a cheminformatics toolkit, using pip. This is a common dependency for molecular modeling and analysis tasks. ```python !pip -q install rdkit-pypi==2021.9.4 ``` -------------------------------- ### Model and Training Hyperparameter Setup in Python Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_6/Chapter_6_Implementation_of_NRI.ipynb Initializes the number of variables and builds the RefMLPEncoder model. It then prints the encoder to the console for verification. This setup is a prerequisite for training the GNN. ```python num_vars = 31 # Build Encoder encoder = RefMLPEncoder() print("ENCODER: ",encoder) ``` -------------------------------- ### Install and Import Packages and Datasets Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_7/Section_7_7_Batching.ipynb Installs necessary Python packages such as ogb, pyg-lib, torch-scatter, torch-sparse, torch-geometric, gputil, nvidia-ml-py3, and thop. It also imports standard and third-party libraries required for data manipulation, model building, and performance tracking. ```python # Find the CUDA version PyTorch was installed with !python -c "import torch; print(torch.version.cuda)" ``` ```python # PyTorch version !python -c "import torch; print(torch.__version__)" ``` ```python # Use the above information to fill in the http address below # %%capture !pip install ogb pyg-lib torch-scatter torch-sparse -f https://data.pyg.org/whl/torch-2.0.1+cu118.html !pip install torch-geometric ``` ```python !pip install gputil !pip install nvidia-ml-py3 !pip install thop ``` ```python # Standard library imports import collections import gc import logging import os import time # Third-party library imports import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sns import psutil import pynvml from thop import clever_format, profile from ogb.nodeproppred import PygNodePropPredDataset, Evaluator from scipy.special import softmax from sklearn.metrics import classification_report, confusion_matrix import torch import torch.nn.functional as F import torch_geometric.transforms as T from torch_geometric.loader import NeighborLoader, NeighborSampler from torch_geometric.nn import GCNConv, SAGEConv from torch.optim.lr_scheduler import ReduceLROnPlateau import os.path as osp ``` -------------------------------- ### Model and Optimizer Initialization (PyTorch) Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Drug_Discovery.ipynb Initializes a GraphDecoder and a VGAEWithPropertyPrediction model, moves the model to the specified device (e.g., GPU), and configures an Adam optimizer with a given learning rate. ```python decoder = GraphDecoder(latent_dim, adjacency_shape, feature_shape) model = VGAEWithPropertyPrediction(encoder,decoder,latent_dim) model = model.to(device) optimizer = torch.optim.Adam(model.parameters(), lr=lr) ``` -------------------------------- ### Install PyTorch Geometric Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_7/section-7-8-of-gnns-in-action.ipynb Installs the PyTorch Geometric library, which provides efficient implementations of graph neural network layers and other graph-based machine learning methods. Requires pip and internet access. ```bash pip install torch_geometric ``` -------------------------------- ### Install PyTorch Geometric Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_2/archived_files/Chapter_2_3_GNN_Embeddings.ipynb Installs the torch_geometric library, a crucial dependency for graph neural network operations in PyTorch. This command uses pip to fetch and install the package and its dependencies. ```python !pip install torch_geometric ``` -------------------------------- ### Call the Model Training Function Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Drug_Discovery.ipynb Demonstrates how to call the `train_model` function with initialized optimizer, model, data loaders, and the number of epochs. This snippet assumes these variables are already defined and populated. ```python train_loss_values, val_loss_values = train_model(optimizer, model, train_loader, val_loader, epochs=epochs) ``` -------------------------------- ### Install Python Libraries for Graph ML Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_4/GNN_GAT_Part_1.ipynb This code block installs necessary Python libraries for graph machine learning, including PyTorch Geometric, pyg-lib, torch-scatter, and torch-sparse. It's often necessary for projects involving graph data structures. The installation command may need to be run multiple times. ```shell # Use the above information to fill in the http address below %%capture !pip install ogb pyg-lib torch-scatter torch-sparse -f https://data.pyg.org/whl/torch-1.13.0+cu116.html !pip install torch-geometric ``` -------------------------------- ### Install Libraries for Graph Neural Networks Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_4/GNN_GAT_part2.ipynb Installs necessary libraries for graph neural network tasks, including PyTorch Geometric (PyG), ogb, pyg-lib, torch-scatter, and torch-sparse. The `-f` flag specifies a particular download source for pre-built wheels. ```shell # Use the above information to fill in the http address below %%capture !pip install ogb pyg-lib torch-scatter torch-sparse -f https://data.pyg.org/whl/torch-2.1.0+cu118.html !pip install torch-geometric ``` -------------------------------- ### Initialize and Configure VGAE Model Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Graph_Autoencoder.ipynb This Python snippet demonstrates initializing a Variational Graph Autoencoder (VGAE) model using the `torch_geometric.nn.VGAE` class. It instantiates the `VariationalGCNEncoder` and defines the optimizer (Adam) and loss function (BCEWithLogitsLoss). The model is moved to the appropriate device (e.g., GPU). This setup is essential for training the graph autoencoder. ```python from torch_geometric.nn import VGAE model = VGAE(VariationalGCNEncoder(input_size, layers, latent_dims)) model = model.to(device) optimizer = torch.optim.Adam(model.parameters(), lr=0.01) criterion = torch.nn.BCEWithLogitsLoss() ``` -------------------------------- ### Install PyTorch Geometric and Dependencies Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_8/GNNsInAction_Chapter_8.ipynb Installs the necessary packages for PyTorch Geometric, including torch-scatter and torch-sparse, with specific versions for CUDA 11.8 and PyTorch 2.0.1. These packages are essential for building and training Graph Neural Networks. ```bash !pip install -q torch-scatter -f https://pytorch-geometric.com/whl/torch-2.0.1+cu118.html !pip install -q torch-sparse -f https://pytorch-geometric.com/whl/torch-2.0.1+cu118.html !pip install -q torch-geometric ``` -------------------------------- ### Install PyG and Related Libraries (Python) Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_4/GNN_GAT_part3.ipynb Installs necessary libraries for PyTorch Geometric (PyG), including ogb, pyg-lib, torch-scatter, and torch-sparse. This command is specific to the detected CUDA version (12.1) and PyTorch version (2.3.0+cu121) for optimal compatibility. ```python # Find the CUDA version PyTorch was installed with !python -c "import torch; print(torch.version.cuda)" # PyTorch version !python -c "import torch; print(torch.__version__)" # Use the above information to fill in the http address below %%capture !pip install ogb pyg-lib torch-scatter torch-sparse -f https://data.pyg.org/whl/torch-2.3.0+cu121.html !pip install torch-geometric ``` -------------------------------- ### Install Required Libraries (PyG, OGB, etc.) Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_3/Chapter_3_using_Subgraph.ipynb Installs necessary libraries including ogb, pyg-lib, and specific PyTorch Geometric scatter/sparse components. It uses a direct URL for PyTorch components to ensure compatibility with the CUDA version detected. ```python # Use the above information to fill in the http address below %%capture !pip install ogb pyg-lib torch-scatter torch-sparse -f https://data.pyg.org/whl/torch-2.3.0+cu121.html !pip install torch-geometric ``` -------------------------------- ### Uninstall PyTorch and PyTorch Geometric Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Graph_Autoencoder.ipynb This command uninstalls the PyTorch and PyTorch Geometric libraries from the current Python environment. It is typically used to clean up previous installations or to prepare for a fresh installation. ```shell #!pip uninstall -y torch torch_geometric ``` -------------------------------- ### Set up DataLoaders for Training, Validation, and Testing in PyTorch Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_6/Chapter_6_Implementation_of_GAT.ipynb Creates DataLoader instances for training, validation, and testing datasets. These DataLoaders are configured with a batch size and shuffle option for the training set, facilitating efficient data iteration during the model training and evaluation phases. ```python train_loader = DataLoader(train_dataset, batch_size=8, shuffle=True) valid_loader = DataLoader(val_dataset, batch_size=8, shuffle=True) test_loader = DataLoader(test_dataset, batch_size=8, shuffle=True) ``` -------------------------------- ### Get PyTorch CUDA Version Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_7/section-7-8-of-gnns-in-action.ipynb Retrieves and prints the CUDA version that the installed PyTorch library was compiled with. This is essential for matching PyTorch builds with the system's CUDA toolkit. It requires the PyTorch library to be installed. ```python import torch print(torch.version.cuda) ``` -------------------------------- ### Get PyTorch Version Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_4/GNN_GAT_part2.ipynb Retrieves the installed PyTorch version. This information is crucial for ensuring compatibility with other libraries and for debugging purposes. ```python # PyTorch version !python -c "import torch; print(torch.__version__)" ``` -------------------------------- ### Train Model with Early Stopping and tqdm Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Drug_Discovery.ipynb Defines a function to train a model using provided optimizer, model, and data loaders. It incorporates tqdm for progress visualization during epochs and batches, tracks loss values, implements early stopping based on validation loss, and requires a `train` function and a `device` to be defined elsewhere. Returns training and validation loss histories. ```python from tqdm.notebook import tqdm def train_model(optimizer, model, train_loader, val_loader, epochs, batch_size=1, patience=10): loss_values = [] val_loss_values = [] best_val_loss = float('inf') early_stopping_counter = 0 for epoch in tqdm(range(epochs), desc='Epochs'): epoch_loss = 0 for batch in tqdm(train_loader, desc='Batches', leave=False): batch_loss = train(model, optimizer, batch.to(device)) epoch_loss += batch_loss avg_epoch_loss = epoch_loss / len(train_loader) loss_values.append(avg_epoch_loss) # Validation step val_loss = 0 for val_batch in val_loader: val_batch_loss = train(model, optimizer, batch.to(device), test=True) val_loss += val_batch_loss avg_val_loss = val_loss / len(val_loader) val_loss_values.append(avg_val_loss) print(f"Epoch {epoch+1}/{epochs}, Loss: {avg_epoch_loss}, Val Loss: {avg_val_loss}") # Early stopping if avg_val_loss < best_val_loss: best_val_loss = avg_val_loss early_stopping_counter = 0 else: early_stopping_counter += 1 if early_stopping_counter >= patience: print("Early stopping triggered") break return loss_values, val_loss_values ``` -------------------------------- ### Implement Graph AutoEncoder Training Loop Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Graph_Autoencoder.ipynb This function implements the training step for a Graph AutoEncoder. It performs a forward pass to get node embeddings, generates negative edge samples, concatenates positive and negative edges, and reconstructs edges. The loss is calculated using BCEWithLogitsLoss and backpropagated to update model weights. Dependencies include `torch_geometric.utils.negative_sampling`. ```python from torch_geometric.utils import negative_sampling def train(model, criterion, optimizer): model.train() optimizer.zero_grad() z = model.encoder(train_data.x, train_data.edge_index) neg_edge_index = negative_sampling( edge_index=train_data.edge_index, num_nodes=train_data.num_nodes, num_neg_samples=train_data.edge_label_index.size(1), method='sparse') edge_label_index = torch.cat([ train_data.edge_label_index, neg_edge_index], dim=-1,) out = model.decoder(z, edge_label_index).view(-1) edge_label = torch.cat([ train_data.edge_label, train_data.edge_label.new_zeros(neg_edge_index.size(1)) ], dim=0) loss = criterion(out, edge_label) loss.backward() optimizer.step() return loss ``` -------------------------------- ### Create PyTorch DataLoaders Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Drug_Discovery.ipynb Initializes DataLoaders for training, validation, and testing datasets using PyTorch's DataLoader class. It specifies batch size and shuffling behavior for each dataset. Assumes train_dataset, val_dataset, and test_dataset are pre-defined. ```python train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True) val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False) test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False) ``` -------------------------------- ### Install Specific PyTorch Version with CUDA (Python) Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_7/Section_7_5_Sparse_and_Dense_Graph_Representations.ipynb This command installs a specific version of PyTorch (1.13.0) along with CUDA 11.6 support from a provided index URL. It includes dependencies like nvidia-cuda-runtime-cu11 and nvidia-cudnn-cu11. The output indicates successful installation but also highlights potential conflicts with other installed packages. ```python !pip install torch==1.13.0 -f https://data.pyg.org/whl/torch-1.13.0+cu116.html ``` -------------------------------- ### Install CUDA 11.6 Toolkit Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_7/Section_7_5_Sparse_and_Dense_Graph_Representations.ipynb This set of commands downloads and installs CUDA version 11.6 for Ubuntu 18.04. It includes downloading the repository definition file, adding the GPG key, updating the package list, and finally installing the CUDA toolkit. ```bash !wget --no-clobber https://developer.download.nvidia.com/compute/cuda/11.6.0/local_installers/cuda-repo-ubuntu1804-11-6-local_11.6.0-510.39.01-1_amd64.deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.0.130-1_amd64.deb !dpkg -i cuda-repo-ubuntu1804-11-6-local_11.6.0-510.39.01-1_amd64.deb !sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda-repo-ubuntu1804-11-6-local/7fa2af80.pub !apt-get update !apt-get install cuda-11-6 ``` -------------------------------- ### Load ogbn-products Dataset and Access Initial Properties Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_7/Section_7_10_Graph_Coarsening.ipynb Loads the 'ogbn-products' dataset and prints the number of classes available. This step initializes the dataset for further processing. ```python dataset_dense = PygNodePropPredDataset( name='ogbn-products') dataset_dense.num_classes ``` -------------------------------- ### Install torch_geometric Package - Python Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_2/archived_files/Chapter_2_2_Node2Vec.ipynb Installs the 'torch_geometric' Python package. This library provides efficient implementations of graph neural network layers and utilities for PyTorch. ```python Collecting torch_geometric Downloading torch_geometric-2.3.1.tar.gz (661 kB)  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 661.6/661.6 kB 4.2 MB/s eta 0:00:00 [?25h Installing build dependencies ... [?25l[?25hdone Getting requirements to build wheel ... [?25l[?25hdone Preparing metadata (pyproject.toml) ... [?25l[?25hdone Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from torch_geometric) (4.66.1) Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from torch_geometric) (1.23.5) Requirement already satisfied: scipy in /usr/local/lib/python3.10/dist-packages (from torch_geometric) (1.11.2) Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch_geometric) (3.1.2) Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from torch_geometric) (2.31.0) Requirement already satisfied: pyparsing in /usr/local/lib/python3.10/dist-packages (from torch_geometric) (3.1.1) Requirement already satisfied: scikit-learn in /usr/local/lib/python3.10/dist-packages (from torch_geometric) (1.2.2) Requirement already satisfied: psutil>=5.8.0 in /usr/local/lib/python3.10/dist-packages (from torch_geometric) (5.9.5) Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch_geometric) (2.1.3) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->torch_geometric) (3.2.0) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->torch_geometric) (3.4) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->torch_geometric) (2.0.4) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->torch_geometric) (2023.7.22) Requirement already satisfied: joblib>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from scikit-learn->torch_geometric) (1.3.2) Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn->torch_geometric) (3.2.0) Building wheels for collected packages: torch_geometric Building wheel for torch_geometric (pyproject.toml) ... [?25l[?25hdone Created wheel for torch_geometric: filename=torch_geometric-2.3.1-py3-none-any.whl size=910454 sha256=3e2df5cc999207ba16a72bdefbe822068ca6c5ded22f4c2328bb32f1988e6083 Stored in directory: /root/.cache/pip/wheels/ac/dc/30/e2874821ff308ee67dcd7a66dbde912411e19e35a1addda028 Successfully built torch_geometric Installing collected packages: torch_geometric Successfully installed torch_geometric-2.3.1 ``` -------------------------------- ### Python: Model Training and Inference Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Drug_Discovery.ipynb This snippet demonstrates a typical training loop for a model, including forward passes, loss calculation, and accessing model outputs like predicted adjacency, features, and QED. It also shows how to move data to the device and detach tensors for further processing. The code assumes a PyTorch environment with a defined 'model' and 'device'. ```python model.train() pred_adj, pred_x, pred_qed, mu, logvar, _ = model(dataset[10].to(device)) pred_x ``` -------------------------------- ### Install psutil Python Package Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_7/Section_7_5_Sparse_and_Dense_Graph_Representations.ipynb Installs the 'psutil' library using pip. This package is commonly used for monitoring system processes and resource utilization. ```python !pip install psutil ``` -------------------------------- ### Load and Print Graph Data (Old Snippet) Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Graph_Autoencoder.ipynb Loads graph data from a specified '.npz' file using NumPy and prints it as a dictionary. This snippet assumes the data is already in a compatible format. Input is a filename; output is a dictionary of graph components. ```python #Old snippet import numpy as np data = np.load(filename) loader = dict(data) print(loader) ``` -------------------------------- ### Install node2vec Package - Python Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_2/archived_files/Chapter_2_2_Node2Vec.ipynb Installs the 'node2vec' Python package and its dependencies using pip. This package is essential for generating node embeddings using the node2vec algorithm. ```python !pip install node2vec ``` -------------------------------- ### Create Pose Datasets for Training, Validation, and Testing Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_6/Chapter_6_Implementation_of_GAT.ipynb Instantiates `PoseDataset` objects for training, validation, and testing splits. This involves specifying paths to data files and the size of the masked portion. ```python from torch_geometric.loader import DataLoader batch_size = 1 mask_size=10 train_dataset = PoseDataset( f'{DATA_PATH}/loc_train_cmu.npy', f'{DATA_PATH}/vel_train_cmu.npy', f'{DATA_PATH}/edges.npy', f'{DATA_PATH}/joint_masks.npy', mask_size = mask_size, transform=True ) val_dataset = PoseDataset( f'{DATA_PATH}/loc_valid_cmu.npy', f'{DATA_PATH}/vel_valid_cmu.npy', f'{DATA_PATH}/edges.npy', f'{DATA_PATH}/joint_masks.npy', mask_size = mask_size, transform=True ) test_dataset = PoseDataset( f'{DATA_PATH}/loc_test_cmu.npy', f'{DATA_PATH}/vel_test_cmu.npy', f'{DATA_PATH}/edges.npy', f'{DATA_PATH}/joint_masks.npy', mask_size = mask_size, transform=True ) ``` -------------------------------- ### Initialize GNN Model and Optimizer (Python) Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_2/chp2 embeddings.ipynb Initializes an instance of the SimpleGNN model and sets up an Adam optimizer for training. The number of features and hidden channels are specified, along with the learning rate for the optimizer. Assumes `data.x` is available for feature dimension. ```python model = SimpleGNN(num_features=data.x.shape[1], hidden_channels=64) optimizer = torch.optim.Adam(model.parameters(), lr=0.01) ``` -------------------------------- ### Optimizer and Loss Setup for GNN Training (Python) Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_3/Chapter_3_using_Subgraph.ipynb This code block focuses on setting up the training components for a GNN model, specifically an Adam optimizer and the Cross-Entropy Loss criterion. It shows how to initialize these components with the model's parameters and the appropriate loss function, ready for the training loop. ```python # Assuming 'model_layer_jk', 'subset_graph', 'optimizer_layer', and 'criterion' are defined # Create an optimizer for the model's parameters # optimizer_layer = torch.optim.Adam(model_layer_jk.parameters(), lr=0.01) # Define the loss criterion # criterion = torch.nn.CrossEntropyLoss() ``` -------------------------------- ### Install PyTorch Geometric and Dependencies Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_7/Section_7_5_Sparse_and_Dense_Graph_Representations.ipynb Installs PyTorch Geometric, along with its required dependencies like pyg-lib, torch-scatter, and torch-sparse. It specifies the PyTorch version for compatibility. ```shell !pip install pyg-lib torch-scatter torch-sparse -f https://data.pyg.org/whl/torch-1.13.0+cu117.html !pip install torch-geometric ``` -------------------------------- ### Install PyTorch Geometric Libraries Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_2/chp2 embeddings.ipynb This section details the installation of essential libraries for PyTorch Geometric, including torch-scatter, torch-cluster, and torch-spline-conv. These libraries provide optimized operations for graph neural networks. ```bash Successfully built torch-scatter torch-cluster torch-spline-conv Installing collected packages: torch-spline-conv, torch-scatter, torch-sparse, torch-cluster Successfully installed torch-cluster-1.6.3 torch-scatter-2.1.2 torch-sparse-0.6.18 torch-spline-conv-1.2.2 ``` -------------------------------- ### Create Molecule Dataset with Pandas Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Drug_Discovery.ipynb This snippet demonstrates how to create a molecular dataset from a CSV file using pandas. It samples a fraction of the data for training and initializes a MoleculeDataset object. The dataset is then used to inspect its size and the shapes of feature and edge tensors. ```python import pandas as pd from molecule_dataset import MoleculeDataset # Assuming MoleculeDataset is defined elsewhere filename = 'your_dataset.csv' # Replace with your actual filename df = pd.read_csv(filename) train_df = df.sample(frac=0.75, random_state=42) train_df.reset_index(drop=True, inplace=True) dataset = MoleculeDataset(train_df.iloc[:8000]) # Example usage and inspection print("Dataset size:", len(dataset)) sample_data = dataset[11] print("Feature tensor size:", sample_data.x.size()) print("Edge index size:", sample_data.edge_index.size()) # Accessing shape of a specific tensor print(dataset[0].x.shape) ``` -------------------------------- ### Install OGB and pynvml Libraries Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_7/Section_7_5_Sparse_and_Dense_Graph_Representations.ipynb Installs the Open Graph Benchmark (OGB) library and the pynvml library. OGB is used for graph datasets, and pynvml provides NVIDIA management library bindings. ```shell !pip install ogb !pip install pynvml ``` -------------------------------- ### Model and Training Hyperparameters in PyTorch Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Drug_Discovery.ipynb Sets up key hyperparameters for training a graph neural network model, including batch size, number of epochs, learning rate, maximum number of atoms, feature dimensions, latent space size, and network layer sizes. It also initializes the encoder model. ```python from torch.utils.data import random_split from torch_geometric.loader import DataLoader batch_size = 1 epochs = 100 lr = 5e-4 N_atoms = 120 # Maximum number of atoms num_features = 1 num_relations = 4 # Number of bond types latent_dim = 435 # Size of the latent space atom_dim = len(SMILES_list) # Number of atom types adjacency_shape=(num_relations, N_atoms, N_atoms) feature_shape=(N_atoms, num_features) layers = [512, 512, 512] encoder = VariationalRGCEncoder(num_features, layers, latent_dim, num_relations) ``` -------------------------------- ### Prepare Graph Data with PyTorch Geometric Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_5/chapter_5_Graph_Autoencoder.ipynb This snippet shows how to initialize a PyTorch Geometric Data object with features, edge indices, edge attributes, and labels. It also includes data transformation for normalization, moving to the correct device (GPU/CPU), and splitting into training, validation, and testing sets. Dependencies include `torch` and `torch_geometric.transforms`. ```python data = Data(x=feature_matrix, edge_index=edge_index, edge_attr=edge_attr, y=labels) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') transform = T.Compose([ T.NormalizeFeatures(), T.ToDevice(device), T.RandomLinkSplit(num_val=0.05, num_test=0.1, is_undirected=True, add_negative_train_samples=False), ]) train_data, val_data, test_data = transform(data) ``` -------------------------------- ### Install Packages for PyTorch Geometric Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_7/Section_7_10_Graph_Coarsening.ipynb Installs necessary packages for PyTorch Geometric, including ogb, pyg-lib, torch-scatter, torch-cluster, and torch-sparse, along with PyTorch Geometric itself. It specifies a PyTorch version compatible with CUDA 11.8. ```bash # Find the CUDA version PyTorch was installed with !python -c "import torch; print(torch.version.cuda)" # PyTorch version !python -c "import torch; print(torch.__version__)" # Use the above information to fill in the http address below # %%capture !pip install ogb pyg-lib torch-scatter torch-cluster torch-sparse -f https://data.pyg.org/whl/torch-2.0.1+cu118.html !pip install torch-geometric ``` -------------------------------- ### Uninstall PyTorch with Pip (Python) Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_7/Section_7_5_Sparse_and_Dense_Graph_Representations.ipynb This command forcefully uninstalls the currently installed PyTorch package. It is often used to resolve version conflicts or prepare for a new installation. The '-y' flag automatically confirms the uninstallation. ```python !pip uninstall torch -y ``` -------------------------------- ### Configure Training Parameters - Python Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_6/Chapter_6_Implementation_of_NRI.ipynb This section defines key training parameters for a neural network model. It sets up batch sizes for training and validation, specifies the number of training epochs, and configures gradient clipping. It also includes settings for teacher forcing during validation and whether to tune based on Negative Log-Likelihood (NLL). Data loaders for training and validation sets are created. ```python gpu = True batch_size = 8 val_batch_size = batch_size num_epochs = 100 clip_grad = None clip_grad_norm = None val_teacher_forcing = True tune_on_nll = True train_data_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, drop_last=True) val_data_loader = DataLoader(val_data, batch_size=val_batch_size)lr = 5e-4 decay_factor = 0.5 decay_steps = 300 wd = 0. ``` -------------------------------- ### Install Additional Libraries for Imbalanced Learning (Python) Source: https://github.com/keitabroadwater/gnns_in_action/blob/master/chapter_4/GNN_GAT_part3.ipynb Installs additional Python libraries, torchsampler and imbalanced-learn, which are useful for handling class imbalance in machine learning tasks, particularly when working with PyTorch Geometric. ```python !pip install torchsampler !pip install torch-geometric imbalanced-learn ```