From aa9cf8107bd1138e952bbb5988c419e277d22613 Mon Sep 17 00:00:00 2001 From: jafraustro Date: Tue, 8 Jul 2025 08:12:11 -0700 Subject: [PATCH] Add `accelerator` API to GCN example. - Add accel argument; update requirements file Signed-off-by: jafraustro --- gcn/README.md | 21 ++++++--------------- gcn/main.py | 23 ++++++++--------------- gcn/requirements.txt | 6 +++--- 3 files changed, 17 insertions(+), 33 deletions(-) diff --git a/gcn/README.md b/gcn/README.md index 842ed803b9..f84bd13620 100644 --- a/gcn/README.md +++ b/gcn/README.md @@ -5,17 +5,14 @@ This repository contains an implementation of Graph Convolutional Networks (GCN) ## Overview This project implements the GCN model proposed in the paper for semi-supervised node classification on graph-structured data. GCN leverages graph convolutions to aggregate information from neighboring nodes and learn node representations for downstream tasks. The implementation provides a flexible and efficient GCN model for graph-based machine learning tasks. -# Requirements -- Python 3.7 or higher -- PyTorch 2.0 or higher -- Requests 2.31 or higher -- NumPy 1.24 or higher - - -# Installation +## Requirements ```bash pip install -r requirements.txt -python main.py +``` + +# Usage +```bash +python main.py --epochs 200 --lr 0.01 --l2 5e-4 --dropout-p 0.5 --hidden-dim 16 --val-every 20 --include-bias ``` # Dataset @@ -24,12 +21,6 @@ The implementation includes support for the Cora dataset, a standard benchmark d ## Model Architecture The GCN model architecture follows the details provided in the paper. It consists of multiple graph convolutional layers with ReLU activation, followed by a final softmax layer for classification. The implementation supports customizable hyperparameters such as the number of hidden units, the number of layers, and dropout rate. -## Usage -To train and evaluate the GCN model on the Cora dataset, use the following command: -```bash -python train.py --epochs 200 --lr 0.01 --l2 5e-4 --dropout-p 0.5 --hidden-dim 16 --val-every 20 --include-bias False --no-cuda False -``` - # Results The model achieves a classification accuracy of 82.5% on the test set of the Cora dataset after 200 epochs of training. This result is comparable to the performance reported in the original paper. However, the results can vary due to the randomness of the train/val/test split. diff --git a/gcn/main.py b/gcn/main.py index 5c8362b576..fe19248eb6 100644 --- a/gcn/main.py +++ b/gcn/main.py @@ -165,7 +165,6 @@ def load_cora(path='./cora', device='cpu'): return features.to_sparse().to(device), labels.to(device), adj_mat.to_sparse().to(device) - def train_iter(epoch, model, optimizer, criterion, input, target, mask_train, mask_val, print_every=10): start_t = time.time() model.train() @@ -199,8 +198,6 @@ def test(model, criterion, input, target, mask): if __name__ == '__main__': - device = 'cuda' if torch.cuda.is_available() else 'cpu' - parser = argparse.ArgumentParser(description='PyTorch Graph Convolutional Network') parser.add_argument('--epochs', type=int, default=200, help='number of epochs to train (default: 200)') @@ -214,29 +211,25 @@ def test(model, criterion, input, target, mask): help='dimension of the hidden representation (default: 16)') parser.add_argument('--val-every', type=int, default=20, help='epochs to wait for print training and validation evaluation (default: 20)') - parser.add_argument('--include-bias', action='store_true', default=False, + parser.add_argument('--include-bias', action='store_true', help='use bias term in convolutions (default: False)') - parser.add_argument('--no-cuda', action='store_true', default=False, - help='disables CUDA training') - parser.add_argument('--no-mps', action='store_true', default=False, - help='disables macOS GPU training') - parser.add_argument('--dry-run', action='store_true', default=False, + parser.add_argument('--no-accel', action='store_true', + help='disables accelerator') + parser.add_argument('--dry-run', action='store_true', help='quickly check a single pass') parser.add_argument('--seed', type=int, default=42, metavar='S', help='random seed (default: 42)') args = parser.parse_args() - use_cuda = not args.no_cuda and torch.cuda.is_available() - use_mps = not args.no_mps and torch.backends.mps.is_available() + use_accel = not args.no_accel and torch.accelerator.is_available() torch.manual_seed(args.seed) - if use_cuda: - device = torch.device('cuda') - elif use_mps: - device = torch.device('mps') + if use_accel: + device = torch.accelerator.current_accelerator() else: device = torch.device('cpu') + print(f'Using {device} device') cora_url = 'https://linqs-data.soe.ucsc.edu/public/lbc/cora.tgz' diff --git a/gcn/requirements.txt b/gcn/requirements.txt index 32e2cb8d1c..7b756e4ede 100644 --- a/gcn/requirements.txt +++ b/gcn/requirements.txt @@ -1,4 +1,4 @@ -torch -torchvision==0.20.0 +torch>=2.6 +torchvision requests -numpy<2 +numpy