Функції втрат

Вступ до TensorFlow на Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Вступ до функцій втрат

  • Базова операція tensorflow
    • Використовується для навчання моделі
    • Міра відповідності моделі
  • Вище значення -> гірша відповідність
    • Мінімізуйте функцію втрат
Вступ до TensorFlow на Python

Типові функції втрат у TensorFlow

  • У TensorFlow є операції для типових функцій втрат

    • Середньоквадратична помилка (MSE)
    • Середня абсолютна помилка (MAE)
    • Помилка Губера
  • Функції втрат доступні через tf.keras.losses()

    • tf.keras.losses.mse()
    • tf.keras.losses.mae()
    • tf.keras.losses.Huber()
Вступ до TensorFlow на Python

Навіщо нам функції втрат?

На зображенні показано графіки функцій втрат mse, mae та huber на інтервалі від -2 до 2.

  • MSE
    • Сильно штрафує викиди
    • Висока чутливість (градієнта) біля мінімуму
  • 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...