損失関数

Introduction to TensorFlow in Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

損失関数の概要

  • tensorflow の基本操作
    • 学習に使用
    • モデル適合度の指標
  • 値が大きいほど適合が悪い
    • 損失関数を最小化する
Introduction to TensorFlow in Python

TensorFlow の代表的な損失関数

  • 一般的な損失関数は TensorFlow に実装済み

    • 二乗平均誤差 (MSE)
    • 平均絶対誤差 (MAE)
    • Huber 誤差
  • 損失関数は tf.keras.losses() から利用可能

    • tf.keras.losses.mse()
    • tf.keras.losses.mae()
    • tf.keras.losses.Huber()
Introduction to TensorFlow in Python

損失関数を気にする理由

この画像は、-2 から 2 の範囲における MSE、MAE、Huber の損失関数のプロットを示しています。

  • MSE
    • 外れ値を強く罰する
    • 最小付近で勾配感度が高い
  • MAE
    • 誤差の大きさに線形に比例
    • 最小付近で感度が低い
  • Huber
    • 最小付近は MSE に類似
    • 離れると MAE に類似
Introduction to TensorFlow in Python

損失関数の定義

# 標準エイリアスで TensorFlow をインポート
import tensorflow as tf

# MSE 損失を計算
loss = tf.keras.losses.mse(targets, predictions)
Introduction to TensorFlow in Python

損失関数の定義

# 線形回帰モデルを定義
def linear_regression(intercept, slope = slope, features = features):
    return intercept + features*slope
# MSE を計算する損失関数を定義
def loss_function(intercept, slope, targets = targets, features = features):
    # 線形モデルの予測を計算
    predictions = linear_regression(intercept, slope)

    # 損失を返す
    return tf.keras.losses.mse(targets, predictions)
Introduction to TensorFlow in Python

損失関数の定義

# テストデータで損失を計算
loss_function(intercept, slope, test_targets, test_features)
10.77
# 既定データで損失を計算
loss_function(intercept, slope)
5.43
Introduction to TensorFlow in Python

Ayo berlatih!

Introduction to TensorFlow in Python

Preparing Video For Download...