Funkcje straty

Wprowadzenie do TensorFlow w Pythonie

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Wprowadzenie do funkcji straty

  • Podstawowa operacja tensorflow
    • Używana do trenowania modelu
    • Miernik dopasowania modelu
  • Wyższa wartość -> gorsze dopasowanie
    • Minimalizacja funkcji straty
Wprowadzenie do TensorFlow w Pythonie

Popularne funkcje straty w TensorFlow

  • TensorFlow oferuje operacje dla popularnych funkcji straty

    • Błąd średniokwadratowy (MSE)
    • Średni błąd bezwzględny (MAE)
    • Błąd Hubera
  • Funkcje straty są dostępne w tf.keras.losses()

    • tf.keras.losses.mse()
    • tf.keras.losses.mae()
    • tf.keras.losses.Huber()
Wprowadzenie do TensorFlow w Pythonie

Dlaczego funkcje straty są ważne?

Wykres funkcji straty MSE, MAE i Hubera w przedziale od -2 do 2.

  • MSE
    • Silnie penalizuje wartości odstające
    • Wysoka czułość (gradientu) w pobliżu minimum
  • MAE
    • Rośnie liniowo wraz z błędem
    • Niska czułość w pobliżu minimum
  • Huber
    • Podobny do MSE w pobliżu minimum
    • Podobny do MAE dalej od minimum
Wprowadzenie do TensorFlow w Pythonie

Definiowanie funkcji straty

# Import TensorFlow under standard alias
import tensorflow as tf

# Compute the MSE loss
loss = tf.keras.losses.mse(targets, predictions)
Wprowadzenie do TensorFlow w Pythonie

Definiowanie funkcji straty

# 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)
Wprowadzenie do TensorFlow w Pythonie

Definiowanie funkcji straty

# 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
Wprowadzenie do TensorFlow w Pythonie

Czas na ćwiczenia!

Wprowadzenie do TensorFlow w Pythonie

Preparing Video For Download...