Huấn luyện với Keras

Nhập môn TensorFlow bằng Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Tổng quan huấn luyện và đánh giá

  1. Nạp và làm sạch dữ liệu
  2. Định nghĩa mô hình
  3. Huấn luyện và xác thực
  4. Đánh giá mô hình
Nhập môn TensorFlow bằng Python

Cách huấn luyện mô hình

# Import tensorflow
import tensorflow as tf

# Define a sequential model
model = tf.keras.Sequential()
# Define the hidden layer
model.add(tf.keras.layers.Dense(16, activation='relu', input_shape=(784,)))
# Define the output layer
model.add(tf.keras.layers.Dense(4, activation='softmax'))
Nhập môn TensorFlow bằng Python

Cách huấn luyện mô hình

# Compile model
model.compile('adam', loss='categorical_crossentropy')
# Train model
model.fit(image_features, image_labels)
Nhập môn TensorFlow bằng Python

Phép fit()

  • Tham số bắt buộc
    • features
    • labels
  • Nhiều tham số tùy chọn
    • batch_size
    • epochs
    • validation_split
Nhập môn TensorFlow bằng Python

Kích thước batch và số epoch

Sơ đồ minh họa cách chia dữ liệu thành batch; ghép các batch tạo thành một epoch.

Nhập môn TensorFlow bằng Python

Thực hiện xác thực

Hình minh họa tách dữ liệu thành mẫu huấn luyện và mẫu xác thực.

Nhập môn TensorFlow bằng Python

Thực hiện xác thực

# Train model with validation split
model.fit(features, labels, epochs=10, validation_split=0.20)
Nhập môn TensorFlow bằng Python

Thực hiện xác thực

Hình hiển thị kết quả huấn luyện và xác thực qua 10 epoch.

Nhập môn TensorFlow bằng Python

Thay đổi chỉ số đánh giá

# Recomile the model with the accuracy metric
model.compile('adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train model with validation split
model.fit(features, labels, epochs=10, validation_split=0.20)
Nhập môn TensorFlow bằng Python

Thay đổi chỉ số đánh giá

Hình hiển thị kết quả huấn luyện và xác thực qua 10 epoch.

Nhập môn TensorFlow bằng Python

Phép evaluate()

Hình minh họa tách dữ liệu thành tập huấn luyện, xác thực và kiểm tra.

# Evaluate the test set
model.evaluate(test)
Nhập môn TensorFlow bằng Python

Vamos praticar!

Nhập môn TensorFlow bằng Python

Preparing Video For Download...