Huấn luyện mô hình với GitHub Actions

CI/CD cho Machine Learning

Ravi Bhadauria

Machine Learning Engineer

Dataset: Dự báo thời tiết tại Úc

  • Phân loại nhị phân
    • Dự đoán mưa vào ngày mai
  • 5 đặc trưng phân loại
    • Location
    • WindGustDir
    • WindDir9am
    • WindDir3pm
    • RainToday
  • 17 đặc trưng số
    • MinTemp
    • MaxTemp
    • Rainfall
    • Evaporation
    • ...
    • WindGustSpeed
    • Cloud3pm
    • Temp9am
    • RISK_MM
1 https://www.kaggle.com/datasets/rever3nd/weather-data
CI/CD cho Machine Learning

Quy trình mô hình hóa

  • Tiền xử lý dữ liệu
    • Chuyển đặc trưng phân loại sang số
    • Thay giá trị thiếu
    • Chuẩn hóa đặc trưng
  • Bộ phân loại Random Forest
    • max_depth = 2, n_estimators = 50
  • Chỉ số chuẩn trên dữ liệu test
    • Biểu đồ hiệu năng
      • Ma trận nhầm lẫn
CI/CD cho Machine Learning

Chuẩn bị dữ liệu: target encoding

def target_encode_categorical_features(
    df: pd.DataFrame, categorical_columns: List[str], target_column: str
) -> pd.DataFrame:
    encoded_data = df.copy()

    # Iterate through categorical columns
    for col in categorical_columns:
        # Calculate mean target value for each category
        encoding_map = df.groupby(col)[target_column].mean().to_dict()

        # Apply target encoding
        encoded_data[col] = encoded_data[col].map(encoding_map)

    return encoded_data
1 https://maxhalford.github.io/blog/target-encoding/
CI/CD cho Machine Learning

Điền khuyết và chuẩn hóa

def impute_and_scale_data(df_features: pd.DataFrame) -> pd.DataFrame:
    # Impute data with mean strategy
    imputer = SimpleImputer(strategy="mean")
    X_preprocessed = imputer.fit_transform(df_features.values)

    # Scale and fit with zero mean and unit variance
    scaler = StandardScaler()
    X_preprocessed = scaler.fit_transform(X_preprocessed)

    return pd.DataFrame(X_preprocessed, columns=df_features.columns)
CI/CD cho Machine Learning

Huấn luyện

  • Chia Train/Test
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
  data.drop(TARGET_COLUMN), data[TARGET_COLUMN], random_state=1993)
  • Bộ phân loại Random Forest
from sklearn.ensemble import RandomForestClassifier

clf = RandomForestClassifier(
  max_depth=2, n_estimators=50, random_state=1993)
clf.fit(X_train, y_train)
CI/CD cho Machine Learning

Chỉ số đánh giá

from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score

# Tính dự đoán
y_pred = model.predict(X_test)

# Tính accuracy accuracy = accuracy_score(y_test, y_pred)
# Tính precision precision = precision_score(y_test, y_pred)
# Tính recall recall = recall_score(y_test, y_pred)
# Tính F1-score f1 = f1_score(y_test, y_pred)
1 https://scikit-learn.org/stable/modules/model_evaluation.html#classification-metrics
CI/CD cho Machine Learning

Biểu đồ

from sklearn.metrics import ConfusionMatrixDisplay
ConfusionMatrixDisplay.from_estimator(model, X_test, y_test,cmap=plt.cm.Blues)

Biểu đồ ma trận nhầm lẫn trên tập kiểm tra

CI/CD cho Machine Learning

Quy trình GitHub Actions

Sơ đồ quy trình CI huấn luyện mô hình

  • Continuous Machine Learning (CML)
    • Công cụ CI/CD cho Machine Learning
    • Tích hợp GitHub Actions
      • Cấp máy huấn luyện
      • Huấn luyện và đánh giá
      • So sánh thí nghiệm
      • Giám sát dữ liệu
      • Báo cáo trực quan
1 https://cml.dev/ 2 https://martinfowler.com/bliki/FeatureBranch.html
CI/CD cho Machine Learning

Lệnh CML

# Enable setup-cml action to be used later
- uses: iterative/setup-cml@v1
- name: Train model
  run: |
    # Your ML workflow goes here
    pip install -r requirements.txt
    python3 train.py
1 https://www.markdownguide.org/basic-syntax/#images
CI/CD cho Machine Learning

Lệnh CML

- name: Write CML report
  run: |
    # Add results and plots to markdown
    cat results.txt >> report.md
    echo "![training graph](./graph.png)" >> report.md

# Create comment from markdown report cml comment create report.md
env: REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CI/CD cho Machine Learning

Kết quả

Ảnh chụp trang pull request hiển thị bình luận do CML tạo

CI/CD cho Machine Learning

Ayo berlatih!

CI/CD cho Machine Learning

Preparing Video For Download...