ฟังก์ชันการสูญเสีย

TensorFlow เบื้องต้นใน Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

แนะนำฟังก์ชันการสูญเสีย

  • การดำเนินการพื้นฐานใน tensorflow
    • ใช้สำหรับฝึกโมเดล
    • วัดความเหมาะสมของโมเดล
  • ค่าสูง -> ความเหมาะสมต่ำ
    • ลดค่าฟังก์ชันการสูญเสียให้น้อยที่สุด
TensorFlow เบื้องต้นใน Python

ฟังก์ชันการสูญเสียทั่วไปใน TensorFlow

  • TensorFlow มีฟังก์ชันการสูญเสียทั่วไปให้พร้อมใช้

    • Mean squared error (MSE)
    • Mean absolute error (MAE)
    • Huber error
  • เรียกใช้ฟังก์ชันการสูญเสียผ่าน tf.keras.losses()

    • tf.keras.losses.mse()
    • tf.keras.losses.mae()
    • tf.keras.losses.Huber()
TensorFlow เบื้องต้นใน Python

ทำไมต้องสนใจฟังก์ชันการสูญเสีย?

ภาพแสดงกราฟของฟังก์ชันการสูญเสีย mse, mae และ huber ในช่วง -2 ถึง 2

  • MSE
    • ลงโทษค่าผิดปกติอย่างรุนแรง
    • ความไว (gradient) สูงบริเวณจุดต่ำสุด
  • MAE
    • สเกลเชิงเส้นตามขนาดของความคลาดเคลื่อน
    • ความไวต่ำบริเวณจุดต่ำสุด
  • Huber
    • คล้าย MSE บริเวณจุดต่ำสุด
    • คล้าย MAE เมื่อห่างจากจุดต่ำสุด
TensorFlow เบื้องต้นใน Python

การกำหนดฟังก์ชันการสูญเสีย

# Import TensorFlow under standard alias
import tensorflow as tf

# Compute the MSE loss
loss = tf.keras.losses.mse(targets, predictions)
TensorFlow เบื้องต้นใน Python

การกำหนดฟังก์ชันการสูญเสีย

# Define a linear regression model
def linear_regression(intercept, slope = slope, features = features):
    return intercept + features*slope
# Define a loss function to compute the MSE
def loss_function(intercept, slope, targets = targets, features = features):
    # Compute the predictions for a linear model
    predictions = linear_regression(intercept, slope)

    # Return the loss
    return tf.keras.losses.mse(targets, predictions)
TensorFlow เบื้องต้นใน Python

การกำหนดฟังก์ชันการสูญเสีย

# Compute the loss for test data inputs
loss_function(intercept, slope, test_targets, test_features)
10.77
# Compute the loss for default data inputs
loss_function(intercept, slope)
5.43
TensorFlow เบื้องต้นใน Python

มาฝึกกันเถอะ!

TensorFlow เบื้องต้นใน Python

Preparing Video For Download...