PyTorch Geometric Temporalのインストールと動作確認を行う.
【目次】
【文献】
Benedek Rozemberczki and Paul Scherer and Yixuan He and George Panagopoulos and Alexander Riedel and Maria Astefanoaei and Oliver Kiss and Ferenc Beres and Guzman Lopez and Nicolas Collignon and Rik Sarkar, PyTorch Geometric Temporal: Spatiotemporal Signal Processing with Neural Machine Learning Models, Proceedings of the 30th ACM International Conference on Information and Knowledge Management, pp. 4564-4573, 2021.
【関連する外部ページ】
https://github.com/benedekrozemberczki/pytorch_geometric_temporal
https://pytorch-geometric-temporal.readthedocs.io/en/latest/
https://github.com/benedekrozemberczki/pytorch_geometric_temporal/tree/master/notebooks
Gitは,バージョン管理システム.ソースコードの管理や複数人での共同に役立つ.
【サイト内の関連ページ】
Windows での Git のインストール: 別ページ »で説明している.
【関連する外部ページ】
Git の公式ページ: https://git-scm.com/
【サイト内の関連ページ】
【関連する外部ページ】
Python の公式ページ: https://www.python.org/
【サイト内の関連ページ】
NVIDIA グラフィックスボードを搭載しているパソコンの場合には, NVIDIA ドライバ, NVIDIA CUDA ツールキット, NVIDIA cuDNN のインストールを行う.
【関連する外部ページ】
コマンドプロンプトを管理者として実行: 別ページ »で説明
PyTorch のページ: https://pytorch.org/index.html
次のコマンドは, PyTorch 2.0 (NVIDIA CUDA 11.8 用) をインストールする. 但し,Anaconda3を使いたい場合には別手順になる.
事前に NVIDIA CUDA のバージョンを確認しておくこと(ここでは,NVIDIA CUDA ツールキット 11.8 が前もってインストール済みであるとする).
PyTorch で,GPU が動作している場合には,「torch.cuda.is_available()」により,True が表示される.
python -m pip install -U --ignore-installed pip python -m pip install -U torch torchvision torchaudio numpy --index-url https://download.pytorch.org/whl/cu118 python -c "import torch; print(torch.__version__, torch.cuda.is_available())"
Anaconda3を使いたい場合には, Anaconda プロンプト (Anaconda Prompt) を管理者として実行し, 次のコマンドを実行する. (PyTorch と NVIDIA CUDA との連携がうまくいかない可能性があるため,Anaconda3を使わないことも検討して欲しい).
conda install -y pytorch torchvision torchaudio pytorch-cuda=11.8 cudnn -c pytorch -c nvidia py -c "import torch; print(torch.__version__, torch.cuda.is_available())"
【サイト内の関連ページ】
【関連する外部ページ】
コマンドプロンプトを管理者として実行: 別ページ »で説明
python -m pip install torch-geometric-temporal
エラーメッセージが出ていないこと
エラーメッセージが出ていないこと
cd %HOMEPATH% rmdir /s /q pytorch_geometric_temporal git clone https://github.com/benedekrozemberczki/pytorch_geometric_temporal cd pytorch_geometric_temporal python -m pytest test
次のページに記載のプログラムを実行.
from torch_geometric_temporal.dataset import ChickenpoxDatasetLoader from torch_geometric_temporal.signal import temporal_signal_split loader = ChickenpoxDatasetLoader() dataset = loader.get_dataset() train_dataset, test_dataset = temporal_signal_split(dataset, train_ratio=0.2) import torch import torch.nn.functional as F from torch_geometric_temporal.nn.recurrent import DCRNN class RecurrentGCN(torch.nn.Module): def __init__(self, node_features): super(RecurrentGCN, self).__init__() self.recurrent = DCRNN(node_features, 32, 1) self.linear = torch.nn.Linear(32, 1) def forward(self, x, edge_index, edge_weight): h = self.recurrent(x, edge_index, edge_weight) h = F.relu(h) h = self.linear(h) return h from tqdm import tqdm model = RecurrentGCN(node_features = 4) optimizer = torch.optim.Adam(model.parameters(), lr=0.01) model.train() for epoch in tqdm(range(200)): cost = 0 for time, snapshot in enumerate(train_dataset): y_hat = model(snapshot.x, snapshot.edge_index, snapshot.edge_attr) cost = cost + torch.mean((y_hat-snapshot.y)**2) cost = cost / (time+1) cost.backward() optimizer.step() optimizer.zero_grad() model.eval() cost = 0 for time, snapshot in enumerate(test_dataset): y_hat = model(snapshot.x, snapshot.edge_index, snapshot.edge_attr) cost = cost + torch.mean((y_hat-snapshot.y)**2) print(time, y_hat, snapshot.y) cost = cost / (time+1) cost = cost.item() print("MSE: {:.4f}".format(cost))