TensorFlow ── Google の深層学習フレームワーク
深層学習を実装するときの2大選択肢が TensorFlow と PyTorch。 大企業の本番AIパイプライン、 Google Cloud、 Android端末の機械学習推論など、 「実用に乗せる」段階でTFを見かけることが多いです。
TF 2.x で MNIST 分類を書くと、 たった数行:
1 2 3 4 5 6 7 8 | import tensorflow as tf model = tf.keras.Sequential([ tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dense(10, activation='softmax'), ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(X_train, y_train, epochs=5) |
この5行でNN構築・学習・評価。 これが現代のフレームワークの威力です。
TensorFlowが管理するのは 計算グラフ:
SSDSE 都道府県データで MLP 回帰を書く例(教育費を予測):
最小限のスニペットで動作確認できる例。 公的データ(SSDSE 等)を想定しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import tensorflow as tf import pandas as pd df = pd.read_csv('data/raw/SSDSE-B-2026.csv', encoding='utf-8', skiprows=1) X = df[['消費支出','人口','高齢化率']].values.astype('float32') y = df['教育費'].values.astype('float32') model = tf.keras.Sequential([ tf.keras.layers.Dense(64, activation='relu', input_shape=(3,)), tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(1), ]) model.compile(optimizer='adam', loss='mse', metrics=['mae']) model.fit(X, y, epochs=200, verbose=0) print(model.evaluate(X, y)) |
| 項目 | TensorFlow | PyTorch |
|---|---|---|
| 開発元 | Meta | |
| 主用途 | 本番運用 | 研究 |
| APIスタイル | Keras(宣言的)+ Eager | 命令型 |
| モバイル | TFLite | PyTorch Mobile |
| サービング | TF Serving | TorchServe |
| 論文採用率 | 30% | 70% |
| 項目 | TensorFlow | PyTorch |
|---|---|---|
| 開発元 | Meta | |
| 主用途 | 本番運用 | 研究 |
| APIスタイル | Keras(宣言的)+ Eager | 命令型 |
| モバイル | TFLite | PyTorch Mobile |
| サービング | TF Serving | TorchServe |
| 論文採用率 | 30% | 70% |