Навчання моделей з Estimators API

Вступ до TensorFlow на Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Що таке Estimators API?

  • Високорівневий підмодуль
  • Менша гнучкість
  • Дотримання кращих практик
  • Швидше розгортання
  • Багато готових моделей

На цій діаграмі показано схему Estimators API та його зв'язок із нижчорівневими API.

1 Image taken from https://www.tensorflow.org/guide/premade_estimators
Вступ до TensorFlow на Python

Специфікація моделі та навчання

  1. Визначте стовпці ознак
  2. Завантажте й перетворіть дані
  3. Задайте estimator
  4. Запустіть тренування
Вступ до TensorFlow на Python

Визначення стовпців ознак

# 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"])
Вступ до TensorFlow на Python

Визначення стовпців ознак

# Create feature column list
features_list = [size, rooms]
# Define a matrix feature column
features_list = [tf.feature_column.numeric_column('image', shape=(784,))]
Вступ до TensorFlow на Python

Завантаження й перетворення даних

# 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
Вступ до TensorFlow на Python

Задати й натренувати регресійний estimator

# 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)
Вступ до TensorFlow на Python

Задати й натренувати глибоку нейронну мережу

# 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)
Вступ до TensorFlow на Python

Давайте потренуємось!

Вступ до TensorFlow на Python

Preparing Video For Download...