Huấn luyện mô hình với Estimators API

Nhập môn TensorFlow bằng Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Estimators API là gì?

  • Tiểu mô-đun cấp cao
  • Ít linh hoạt hơn
  • Áp dụng thực hành tốt
  • Triển khai nhanh hơn
  • Nhiều mô hình dựng sẵn

Hình minh họa sơ đồ Estimators API và mối liên hệ với các API cấp thấp hơn.

1 Ảnh từ https://www.tensorflow.org/guide/premade_estimators
Nhập môn TensorFlow bằng Python

Đặc tả mô hình và huấn luyện

  1. Định nghĩa cột đặc trưng
  2. Tải và biến đổi dữ liệu
  3. Định nghĩa bộ ước lượng
  4. Huấn luyện mô hình
Nhập môn TensorFlow bằng Python

Định nghĩa cột đặc trưng

# 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"])
Nhập môn TensorFlow bằng Python

Định nghĩa cột đặc trưng

# Create feature column list
features_list = [size, rooms]
# Define a matrix feature column
features_list = [tf.feature_column.numeric_column('image', shape=(784,))]
Nhập môn TensorFlow bằng Python

Tải và biến đổi dữ liệu

# 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
Nhập môn TensorFlow bằng Python

Định nghĩa và huấn luyện bộ ước lượng hồi quy

# 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)
Nhập môn TensorFlow bằng Python

Định nghĩa và huấn luyện mạng nơ-ron sâu

# 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)
Nhập môn TensorFlow bằng Python

Ayo berlatih!

Nhập môn TensorFlow bằng Python

Preparing Video For Download...