Fonctions de perte

Introduction à TensorFlow en Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Introduction aux fonctions de perte

  • Opération tensorflow fondamentale
    • Sert à entraîner un modèle
    • Mesure de l'ajustement du modèle
  • Valeur élevée -> moins bon ajustement
    • Minimiser la fonction de perte
Introduction à TensorFlow en Python

Fonctions de perte courantes dans TensorFlow

  • TensorFlow propose des opérations pour des fonctions de perte courantes

    • Erreur quadratique moyenne (MSE)
    • Erreur absolue moyenne (MAE)
    • Perte de Huber
  • Fonctions de perte accessibles via tf.keras.losses()

    • tf.keras.losses.mse()
    • tf.keras.losses.mae()
    • tf.keras.losses.Huber()
Introduction à TensorFlow en Python

Pourquoi s'intéresser aux fonctions de perte ?

Cette image affiche les fonctions de perte mse, mae et huber tracées sur l'intervalle -2 à 2.

  • MSE
    • Pénalise fortement les valeurs aberrantes
    • Forte sensibilité (du gradient) près du minimum
  • MAE
    • Croît linéairement avec la taille de l'erreur
    • Faible sensibilité près du minimum
  • Huber
    • Proche du MSE près du minimum
    • Proche du MAE loin du minimum
Introduction à TensorFlow en Python

Définir une fonction de perte

# Import TensorFlow under standard alias
import tensorflow as tf

# Compute the MSE loss
loss = tf.keras.losses.mse(targets, predictions)
Introduction à TensorFlow en Python

Définir une fonction de perte

# 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)
Introduction à TensorFlow en Python

Définir la fonction de perte

# 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
Introduction à TensorFlow en Python

Passons à la pratique !

Introduction à TensorFlow en Python

Preparing Video For Download...