การเทรนโมเดลด้วย Estimators API

TensorFlow เบื้องต้นใน Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Estimators API คืออะไร?

  • submodule ระดับสูง
  • ยืดหยุ่นน้อยกว่า
  • บังคับใช้ best practices
  • ติดตั้งใช้งานได้เร็วขึ้น
  • มีโมเดลสำเร็จรูปให้เลือกมาก

ภาพแสดงโครงสร้างของ Estimators API และความสัมพันธ์กับ API ระดับล่าง

1 Image taken from https://www.tensorflow.org/guide/premade_estimators
TensorFlow เบื้องต้นใน Python

การกำหนดค่าและเทรนโมเดล

  1. กำหนด feature columns
  2. โหลดและแปลงข้อมูล
  3. กำหนด estimator
  4. ดำเนินการ train
TensorFlow เบื้องต้นใน Python

การกำหนด 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"])
TensorFlow เบื้องต้นใน Python

การกำหนด 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,))]
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

กำหนดและเทรน regression 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

กำหนดและเทรน 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)
TensorFlow เบื้องต้นใน Python

มาฝึกกันเถอะ!

TensorFlow เบื้องต้นใน Python

Preparing Video For Download...