Training dei modelli con la Estimators API

Introduzione a TensorFlow in Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Che cos'è la Estimators API?

  • Sottomodulo di alto livello
  • Meno flessibile
  • Impone buone pratiche
  • Deployment più rapido
  • Molti modelli predefiniti

Questa immagine mostra uno schema della Estimators API e di come si collega alle API di livello inferiore.

1 Immagine tratta da https://www.tensorflow.org/guide/premade_estimators
Introduzione a TensorFlow in Python

Specifiche del modello e training

  1. Definisci le feature columns
  2. Carica e trasforma i dati
  3. Definisci un estimator
  4. Avvia il training
Introduzione a TensorFlow in Python

Definire le feature columns

# Import tensorflow under its standard alias
import tensorflow as tf

# Define a numeric feature column
size = tf.feature_column.numeric_column("size")
# Define a categorical feature column
rooms = tf.feature_column.categorical_column_with_vocabulary_list("rooms",\
["1", "2", "3", "4", "5"])
Introduzione a TensorFlow in Python

Definire le feature columns

# Create feature column list
features_list = [size, rooms]
# Define a matrix feature column
features_list = [tf.feature_column.numeric_column('image', shape=(784,))]
Introduzione a TensorFlow in Python

Caricare e trasformare i dati

# Define input data function
def input_fn():
    # Define feature dictionary
    features = {"size": [1340, 1690, 2720], "rooms": [1, 3, 4]}
    # Define labels
    labels = [221900, 538000, 180000]
    return features, labels
Introduzione a TensorFlow in Python

Definire e addestrare un estimator di regressione

# Define a deep neural network regression
model0 = tf.estimator.DNNRegressor(feature_columns=feature_list,\
    hidden_units=[10, 6, 6, 3])

# Train the regression model
model0.train(input_fn, steps=20)
Introduzione a TensorFlow in Python

Definire e addestrare una deep neural network

# Define a deep neural network classifier
model1 = tf.estimator.DNNClassifier(feature_columns=feature_list,\ 
   hidden_units=[32, 16, 8], n_classes=4)

# Train the classifier
model1.train(input_fn, steps=20)
Introduzione a TensorFlow in Python

Esercitiamoci!

Introduzione a TensorFlow in Python

Preparing Video For Download...