
2026/02/13 21:59
「PyTorch入門:ビジュアルガイド」
RSS: https://news.ycombinator.com/rss
要約▶
Japanese Translation:
提供された要約は明確で簡潔であり、主要ポイントのリストと完全に一致しているため、そのまま繰り返すことができます。
本文
PyTorch入門
わかりやすいビジュアルで学べる紹介。
2026年2月10日
目次
- PyTorchとは何か
- テンソルの基礎
- 自動微分(Autograd)
- Autograd実践例
- 単純なニューラルネットワークを構築する
- YouTube
PyTorchとは?
PyTorchは最も人気のあるディープラーニングフレームワークの一つです。Meta AIが管理し、Linux Foundationの一部として継続的にメンテナンスされているTorchライブラリをベースに構築されたオープンソースライブラリです。
テンソルの基礎
機械学習は数値で成り立っています。テンソルとは、PyTorchで扱う数値を格納するための特別なコンテナ―実質的には強力な配列です。
なぜテンソルなのか?
- 学習データを保持できる
- さまざまな組み込み演算が利用可能(例:
、torch.rand()
、torch.randn()
)torch.ones()
import torch rand_sample = torch.rand(10000) randn_sample = torch.randn(10000) zeros_sample = torch.zeros(10) ones_sample = torch.ones(10) arange_sample = torch.arange(0, 10) linspace_sample = torch.linspace(0, 10, steps=5) full_sample = torch.full((10,), 42) empty_sample = torch.empty(10) eye_sample = torch.eye(5)
| 関数 | 説明 |
|---|---|
| 0〜1の一様乱数 |
| 標準正規分布(平均 = 0、標準偏差 = 1) |
| 単位行列 |
| メモリを確保するが初期化はしない |
自前データの扱い
houses = torch.tensor([ [2, 65, 15, 285], [3, 95, 8, 425], [4, 120, 25, 380], [3, 88, 42, 295], [5, 180, 3, 675], [2, 58, 50, 245] ], dtype=torch.float32)
数値以外(文字列、画像、メッシュなど)はまず数値へマッピングする必要があります。
| データ型 | 例の形状 |
|---|---|
| 画像 | (グレースケール)または (RGB) |
| 3Dメッシュ | (x,y,z座標を持つ頂点) |
テンソルの数値演算
基本的な四則演算
x = torch.tensor([1.0, 2.0, 3.0]) y = torch.tensor([4.0, 5.0, 6.0]) print(x + y) # 加算 print(x * y) # 要素ごとの積 print(x @ y) # ドット積 # 集約関数 print(x.sum()) # 全要素の合計 print(x.mean()) # 平均 print(x.max()) # 最大値
活性化関数
import torch.nn.functional as F x = torch.tensor([-2.0, -1.0, 0.0, 1.0, 2.0]) print(F.relu(x)) # ReLU print(torch.sigmoid(x)) # Sigmoid print(torch.tanh(x)) # Tanh
自動微分(Autograd)
AutogradはPyTorchの自動微分エンジンです。
簡単な例
x = torch.tensor(2.0, requires_grad=True) f = x ** 2 # f(x) = x² f.backward() # ∂f/∂x を計算 print(x.grad) # 4.0
より複雑な関数
x = torch.tensor(1.0, requires_grad=True) y = torch.tensor(2.0, requires_grad=True) z = torch.tensor(0.5, requires_grad=True) f = torch.sin(x) * y**2 + torch.exp(z) f.backward() print(x.grad) # ≈ 2.16 print(y.grad) # ≈ 3.37 print(z.grad) # ≈ 1.65
単純なニューラルネットワークの構築
インポートとデータ準備
import torch, torch.nn as nn, torch.nn.functional as F import pandas as pd from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.metrics import mean_absolute_error, mean_absolute_percentage_error import matplotlib.pyplot as plt # データ読み込み data_raw = pd.read_csv('./london_houses_transformed.csv') # 特徴量とターゲットを分離 x = data_raw.drop('price', axis=1) y = data_raw['price'] # 学習/テスト分割(80/20) X_train_raw, X_test_raw, Y_train_raw, Y_test_raw = train_test_split( x.values, y.values, test_size=0.2, random_state=15) # 正規化 scaler_X = StandardScaler().fit(X_train_raw) scaler_Y = StandardScaler().fit(Y_train_raw.reshape(-1, 1)) price_mean, price_std = scaler_Y.mean_[0], scaler_Y.scale_[0] # テンソルへ変換 X_train = torch.FloatTensor(scaler_X.transform(X_train_raw)) X_test = torch.FloatTensor(scaler_X.transform(X_test_raw)) Y_train = torch.FloatTensor(scaler_Y.transform(Y_train_raw.reshape(-1, 1))) Y_test = torch.FloatTensor(scaler_Y.transform(Y_test_raw.reshape(-1, 1)))
モデル定義
class Model(nn.Module): def __init__(self, in_features=87, h1=64, h2=32, output_features=1): super().__init__() self.fc1 = nn.Linear(in_features, h1) self.fc2 = nn.Linear(h1, h2) self.out = nn.Linear(h2, output_features) def forward(self, x): x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) return self.out(x) model = Model()
学習ループ
epochs = 100 learning_rate = 0.01 torch.manual_seed(15) optimiser = torch.optim.Adam(model.parameters(), lr=learning_rate) loss_func = nn.MSELoss() losses = [] for i in range(epochs): optimiser.zero_grad() y_pred = model(X_train) loss = loss_func(y_pred, Y_train) losses.append(loss.item()) loss.backward() optimiser.step() torch.save(model.state_dict(), 'model.pth')
損失の可視化
plt.figure(figsize=(10, 6)) plt.plot(losses, linewidth=2, color='#e74c3c') plt.xlabel('Epoch', fontsize=12) plt.ylabel('MSE Loss (正規化)', fontsize=12) plt.title('学習経過:損失がゼロに近づく', fontsize=14, fontweight='bold') plt.grid(alpha=0.3) plt.ylim(bottom=0) plt.tight_layout() plt.show()
モデル評価
model.eval() with torch.no_grad(): predictions = model(X_test) # 正規化解除 predictions_real = predictions * price_std + price_mean Y_test_real = Y_test * price_std + price_mean mae = mean_absolute_error(Y_test_real, predictions_real) mape = mean_absolute_percentage_error(Y_test_real, predictions_real) * 100 pct_errors = torch.abs((Y_test_real - predictions_real) / Y_test_real) * 100 within_10 = (pct_errors <= 10).sum().item() within_20 = (pct_errors <= 20).sum().item() total = len(Y_test_real) print(f"\n全体の評価:") print(f" MAE: £{mae:,.0f}") print(f" MAPE: {mape:.1f}%") print(f" 10%以内: {within_10}/{total} ({within_10/total*100:.0f}%)") print(f" 20%以内: {within_20}/{total} ({within_20/total*100:.0f}%)")
結果
全体の評価: MAE: £329,798 MAPE: 18.6% 10%以内: 257/689 (37%) 20%以内: 447/689 (65%)
結論
以下の一連のパイプラインを構築しました:
- データ前処理 – 読み込み、分割、正規化、テンソル変換
- モデル定義 – 単純な全結合ネットワーク
- 学習ループ – フォワードパス、損失計算、逆伝播、オプティマイザ更新
- 評価 – MAE、MAPE、パーセンテージ誤差指標
性能はモデルが学習していることを示す一方で、特徴量(特に位置情報)の質が精度の限界となっていることを示しています。今後はより豊富な特徴量やXGBoostなどのツリーベース手法を試すことで改善できる可能性があります。
もっと学びたい方はニュースレターへ登録してください!