Modellen trainen met de Estimators API

Introductie tot TensorFlow in Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Wat is de Estimators API?

  • High-level submodule
  • Minder flexibel
  • Dwingt best practices af
  • Snellere deployment
  • Veel kant‑en‑klare modellen

Dit schema toont de Estimators API en de relatie met lagere‑niveau API’s.

1 Afbeelding afkomstig van https://www.tensorflow.org/guide/premade_estimators
Introductie tot TensorFlow in Python

Model specificeren en trainen

  1. Definieer featurekolommen
  2. Laad en transformeer data
  3. Definieer een estimator
  4. Voer training uit
Introductie tot TensorFlow in Python

Featurekolommen definiëren

# 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"])
Introductie tot TensorFlow in Python

Featurekolommen definiëren

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

Data laden en transformeren

# 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
Introductie tot TensorFlow in Python

Een regressie-estimator definiëren en trainen

# 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)
Introductie tot TensorFlow in Python

Een deep neural network definiëren en trainen

# 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)
Introductie tot TensorFlow in Python

Laten we oefenen!

Introductie tot TensorFlow in Python

Preparing Video For Download...