Hàm mất mát

Nhập môn TensorFlow bằng Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Giới thiệu về hàm mất mát

  • Thao tác cốt lõi của tensorflow
    • Dùng để huấn luyện mô hình
    • Đo độ phù hợp của mô hình
  • Giá trị cao -> khớp kém
    • Cần tối thiểu hóa hàm mất mát
Nhập môn TensorFlow bằng Python

Các hàm mất mát phổ biến trong TensorFlow

  • TensorFlow có các hàm mất mát phổ biến

    • Sai số bình phương trung bình (MSE)
    • Sai số tuyệt đối trung bình (MAE)
    • Huber
  • Truy cập qua tf.keras.losses()

    • tf.keras.losses.mse()
    • tf.keras.losses.mae()
    • tf.keras.losses.Huber()
Nhập môn TensorFlow bằng Python

Vì sao cần quan tâm đến hàm mất mát?

Hình minh họa các hàm mất mát MSE, MAE và Huber trên khoảng -2 đến 2.

  • MSE
    • Phạt mạnh ngoại lệ
    • Độ nhạy (gradient) cao gần điểm tối thiểu
  • MAE
    • Tỉ lệ tuyến tính với độ lớn sai số
    • Độ nhạy thấp gần điểm tối thiểu
  • Huber
    • Giống MSE gần điểm tối thiểu
    • Giống MAE khi xa điểm tối thiểu
Nhập môn TensorFlow bằng Python

Định nghĩa hàm mất mát

# Import TensorFlow under standard alias
import tensorflow as tf

# Compute the MSE loss
loss = tf.keras.losses.mse(targets, predictions)
Nhập môn TensorFlow bằng Python

Định nghĩa hàm mất mát

# 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)
Nhập môn TensorFlow bằng Python

Tính hàm mất mát

# 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
Nhập môn TensorFlow bằng Python

Ayo berlatih!

Nhập môn TensorFlow bằng Python

Preparing Video For Download...