Entraînement par lots

Introduction à TensorFlow en Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Qu'est-ce que l'entraînement par lots ?

Cette image présente le prix, la superficie et le nombre de chambres des maisons du comté de King.

Cette image présente le prix, la superficie et le nombre de chambres des maisons du comté de King, divisés en lots.

Introduction à TensorFlow en Python

Le paramètre chunksize

  • pd.read_csv() permet de charger les données par lots
    • Évitez de charger tout l'ensemble de données
    • Le paramètre chunksize fixe la taille du lot
# Import pandas and numpy
import pandas as pd
import numpy as np

# Load data in batches
for batch in pd.read_csv('kc_housing.csv', chunksize=100):
    # Extract price column
    price = np.array(batch['price'], np.float32)

    # Extract size column
    size = np.array(batch['size'], np.float32)
Introduction à TensorFlow en Python

Entraîner un modèle linéaire par lots

# Import tensorflow, pandas, and numpy
import tensorflow as tf
import pandas as pd
import numpy as np
# Define trainable variables
intercept = tf.Variable(0.1, tf.float32)
slope = tf.Variable(0.1, tf.float32)
# Define the model
def linear_regression(intercept, slope, features):
    return intercept + features*slope
Introduction à TensorFlow en Python

Entraîner un modèle linéaire par lots

# Compute predicted values and return loss function
def loss_function(intercept, slope, targets, features):
    predictions = linear_regression(intercept, slope, features)
    return tf.keras.losses.mse(targets, predictions)
# Define optimization operation
opt = tf.keras.optimizers.Adam()
Introduction à TensorFlow en Python

Entraîner un modèle linéaire par lots

# Load the data in batches from pandas
for batch in pd.read_csv('kc_housing.csv', chunksize=100):
    # Extract the target and feature columns
    price_batch = np.array(batch['price'], np.float32)
    size_batch = np.array(batch['lot_size'], np.float32)

    # Minimize the loss function
    opt.minimize(lambda: loss_function(intercept, slope, price_batch, size_batch), 
                 var_list=[intercept, slope])
# Print parameter values
print(intercept.numpy(), slope.numpy())
Introduction à TensorFlow en Python

Échantillon complet ou entraînement par lots

  • Échantillon complet
    1. Une mise à jour par époque
    2. Accepte l'ensemble tel quel
    3. Limité par la mémoire
  • Entraînement par lots
    1. Mises à jour multiples par époque
    2. Nécessite de diviser l'ensemble
    3. Aucune limite de taille
Introduction à TensorFlow en Python

Passons à la pratique !

Introduction à TensorFlow en Python

Preparing Video For Download...