Introduction to TensorFlow in Python
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School
tensorflow の基本操作一般的な損失関数は TensorFlow に実装済み
損失関数は tf.keras.losses() から利用可能
tf.keras.losses.mse()tf.keras.losses.mae()tf.keras.losses.Huber()
# 標準エイリアスで TensorFlow をインポート
import tensorflow as tf
# MSE 損失を計算
loss = tf.keras.losses.mse(targets, predictions)
# 線形回帰モデルを定義
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)
# テストデータで損失を計算
loss_function(intercept, slope, test_targets, test_features)
10.77
# 既定データで損失を計算
loss_function(intercept, slope)
5.43
Introduction to TensorFlow in Python