Huấn luyện theo lô

Nhập môn TensorFlow bằng Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Huấn luyện theo lô là gì?

Hình hiển thị dữ liệu về giá, diện tích và số phòng ngủ của nhà ở Quận King.

Hình hiển thị dữ liệu về giá, diện tích và số phòng ngủ của nhà ở Quận King, được chia thành các lô.

Nhập môn TensorFlow bằng Python

Tham số chunksize

  • pd.read_csv() cho phép tải dữ liệu theo lô
    • Tránh tải toàn bộ tập dữ liệu
    • Tham số chunksize đặt kích thước lô
# Import pandas and numpy
import pandas as pd
import numpy as np

# Load data in batches
for batch in pd.read_csv('kc_housing.csv', chunksize=100):
    # Extract price column
    price = np.array(batch['price'], np.float32)

    # Extract size column
    size = np.array(batch['size'], np.float32)
Nhập môn TensorFlow bằng Python

Huấn luyện mô hình tuyến tính theo lô

# Import tensorflow, pandas, and numpy
import tensorflow as tf
import pandas as pd
import numpy as np
# Define trainable variables
intercept = tf.Variable(0.1, tf.float32)
slope = tf.Variable(0.1, tf.float32)
# Define the model
def linear_regression(intercept, slope, features):
    return intercept + features*slope
Nhập môn TensorFlow bằng Python

Huấn luyện mô hình tuyến tính theo lô

# Compute predicted values and return loss function
def loss_function(intercept, slope, targets, features):
    predictions = linear_regression(intercept, slope, features)
    return tf.keras.losses.mse(targets, predictions)
# Define optimization operation
opt = tf.keras.optimizers.Adam()
Nhập môn TensorFlow bằng Python

Huấn luyện mô hình tuyến tính theo lô

# Load the data in batches from pandas
for batch in pd.read_csv('kc_housing.csv', chunksize=100):
    # Extract the target and feature columns
    price_batch = np.array(batch['price'], np.float32)
    size_batch = np.array(batch['lot_size'], np.float32)

    # Minimize the loss function
    opt.minimize(lambda: loss_function(intercept, slope, price_batch, size_batch), 
                 var_list=[intercept, slope])
# Print parameter values
print(intercept.numpy(), slope.numpy())
Nhập môn TensorFlow bằng Python

Toàn bộ mẫu và huấn luyện theo lô

  • Toàn bộ mẫu
    1. Một cập nhật mỗi epoch
    2. Nhận dữ liệu gốc, không cần chỉnh sửa
    3. Bị giới hạn bởi bộ nhớ
  • Huấn luyện theo lô
    1. Nhiều cập nhật mỗi epoch
    2. Cần chia nhỏ dữ liệu
    3. Không giới hạn kích thước dữ liệu
Nhập môn TensorFlow bằng Python

Ayo berlatih!

Nhập môn TensorFlow bằng Python

Preparing Video For Download...