AI学習設定を柔軟に管理
Building a Gin Config Controlled PyTorch Pipeline with Configurable MLP Variants, Cosine Scheduling, and Runtime Parameter Overrides

Gin Configを使うと、AI学習の実験設定をコードから分離し、変更しやすくなる。これにより、AI開発者は効率的に多様な実験を試せるため重要。
このチュートリアルでは、Gin Configによって制御されるPyTorch実験パイプラインを実装します。これにより、実行可能なトレーニングコードは安定したまま、実験の自由度が宣言的な設定ファイルに移行されます。非線形スパイラル二値分類タスクを構築し、スコープ付きのアーキテクチャバリアントを持つ構成可能なMLPを定義し、オプティマイザ、スケジューラ、損失、バッチ処理、シード、およびトレーニングループのパラメータを@gin.configurableバインディングを介して公開します。Ginのスコープ付き参照を使用して個別のモデル構成をインスタンス化し、ソースコードを編集せずに選択したパラメータを上書きするためのランタイムバインディングを使用し、各トレーニング実行を生成する正確な解決済み構成をキャプチャするためにオペレーティブ構成エクスポートを使用します。Gin Configのインストールとスパイラルデータセットの構築
!pip -q install gin-config
import os
import json
import math
import random
import textwrap
from pathlib import Path
import gin
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import TensorDataset, DataLoader
import matplotlib.pyplot as plt
ROOT = Path("/content/gin_config_sharp_tutorial")
CONFIG_DIR = ROOT / "configs"
RUN_DIR = ROOT / "runs"
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
RUN_DIR.mkdir(parents=True, exist_ok=True)
gin.clear_config()
@gin.configurable
def seed_everything(seed=42):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
return seed
@gin.configurable
def make_spiral_dataset(
n_per_class=gin.REQUIRED,
noise=0.18,
rotations=1.75,
train_fraction=0.8,
seed=0,
):
rng = np.random.default_rng(seed)
radius_0 = np.linspace(0.05, 1.0, n_per_class)
theta_0 = rotations * 2 * np.pi * radius_0
theta_0 += rng.normal(0.0, noise, size=n_per_class)
x0 = np.stack(
[
radius_0 * np.cos(theta_0),
radius_0 * np.sin(theta_0),
],
axis=1,
)
radius_1 = np.linspace(0.05, 1.0, n_per_class)
theta_1 = rotations * 2 * np.pi * radius_1 + np.pi
この記事について質問
記事の内容に答えます。記事外のことは都度ウェブで調べます。