GitHub Actions के साथ मॉडल ट्रेनिंग

Machine Learning के लिए CI/CD

Ravi Bhadauria

Machine Learning Engineer

डेटासेट: ऑस्ट्रेलिया में मौसम पूर्वानुमान

  • बाइनरी क्लासिफिकेशन
    • कल बारिश की भविष्यवाणी
  • 5 श्रेणीगत फीचर्स
    • Location
    • WindGustDir
    • WindDir9am
    • WindDir3pm
    • RainToday
  • 17 संख्यात्मक फीचर्स
    • MinTemp
    • MaxTemp
    • Rainfall
    • Evaporation
    • ...
    • WindGustSpeed
    • Cloud3pm
    • Temp9am
    • RISK_MM
1 https://www.kaggle.com/datasets/rever3nd/weather-data
Machine Learning के लिए CI/CD

मॉडलिंग वर्कफ़्लो

  • डेटा प्रीप्रोसेसिंग
    • श्रेणीगत फीचर्स को संख्यात्मक में बदलें
    • मिसिंग वैल्यूज़ भरें
    • फीचर्स स्केल करें
  • Random Forest Classifier
    • max_depth = 2, n_estimators = 50
  • टेस्ट डेटा पर स्टैंडर्ड मेट्रिक्स
    • परफॉर्मेंस प्लॉट्स
      • कन्फ्यूजन मैट्रिक्स प्लॉट
Machine Learning के लिए CI/CD

डेटा तैयारी: टारगेट एनकोडिंग

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/
Machine Learning के लिए CI/CD

इम्प्यूटिंग और स्केलिंग

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)
Machine Learning के लिए CI/CD

ट्रेनिंग

  • 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)
  • Random Forest Classifier
from sklearn.ensemble import RandomForestClassifier

clf = RandomForestClassifier(
  max_depth=2, n_estimators=50, random_state=1993)
clf.fit(X_train, y_train)
Machine Learning के लिए CI/CD

मेट्रिक्स

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

# Calculate predictions
y_pred = model.predict(X_test)

# Calculate accuracy accuracy = accuracy_score(y_test, y_pred)
# Calculate precision precision = precision_score(y_test, y_pred)
# Calculate recall recall = recall_score(y_test, y_pred)
# Calculate f1 score f1 = f1_score(y_test, y_pred)
1 https://scikit-learn.org/stable/modules/model_evaluation.html#classification-metrics
Machine Learning के लिए CI/CD

प्लॉट्स

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

टेस्ट सेट पर कन्फ्यूजन मैट्रिक्स का प्लॉट

Machine Learning के लिए CI/CD

GitHub Actions वर्कफ़्लो

मॉडल ट्रेनिंग के कंटीन्यूस इंटीग्रेशन वर्कफ़्लो का प्लॉट

  • Continuous Machine Learning (CML)
    • Machine Learning के लिए CI/CD टूल
    • GitHub Actions इंटीग्रेशन
      • ट्रेनिंग मशीनें प्रोविजन करें
      • ट्रेनिंग और इवैल्यूएशन करें
      • एक्सपेरिमेंट्स की तुलना करें
      • डेटासेट्स मॉनिटर करें
      • विज़ुअल रिपोर्ट्स
1 https://cml.dev/ 2 https://martinfowler.com/bliki/FeatureBranch.html
Machine Learning के लिए CI/CD

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
Machine Learning के लिए CI/CD

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 }}
Machine Learning के लिए CI/CD

आउटपुट

CML द्वारा जनरेट की गई टिप्पणी दिखाते Pull Request पेज का स्क्रीनशॉट

Machine Learning के लिए CI/CD

अभ्यास करते हैं!

Machine Learning के लिए CI/CD

Preparing Video For Download...