LeVanLoi miscellaneous articles

  1. Trang chủ
  2. Lưu
  3. Thẻ
  4. Hỏi - Đáp

 
 
LeVanLoi'log, ⌚ 2024-11-18
***
What is cross-entropy loss in machine learning?
Tác giả: Lê Văn Lợi tổng hợp

English:

Cross-Entropy Loss in Machine Learning

Cross-entropy loss is a widely used loss function in machine learning, especially for classification tasks. It measures the dissimilarity between two probability distributions: the true labels (actual data distribution) and the predicted probabilities (model's output). The goal is to minimize this dissimilarity by training the model to produce predictions closer to the true labels.


Mathematical Definition

For a single data point in a classification problem with CC classes, cross-entropy loss is defined as:

L=c=1Cyclog(y^c)mathcal{L} = - sum_{c=1}^C y_c log(hat{y}_c)

Where:

  • ycy_c: The true label for class cc (1 if the class is the correct one, 0 otherwise, in one-hot encoding).
  • y^chat{y}_c: The predicted probability for class cc (output from the model, typically obtained via a softmax function).

For a dataset with NN samples, the average cross-entropy loss is:

L=1Ni=1Nc=1Cyi,clog(y^i,c)mathcal{L} = - frac{1}{N} sum_{i=1}^N sum_{c=1}^C y_{i,c} log(hat{y}_{i,c})

Key Intuitions

  1. Probabilistic Output:

    • Cross-entropy assumes the model outputs probabilities (values between 0 and 1 that sum to 1 for all classes).
    • It heavily penalizes incorrect predictions with high confidence.
  2. Logarithmic Penalty:

    • The log(y^c)log(hat{y}_c) term increases the penalty for predictions that are far from the true label.
    • If the predicted probability for the correct class (y^chat{y}_c) is close to 1, the loss is small.
    • If y^chat{y}_c is close to 0, the loss is very large, encouraging the model to avoid low confidence on the true label.
  3. Relationship with Likelihood:

    • Cross-entropy loss is equivalent to the negative log-likelihood of the true labels under the predicted probability distribution.

Example

Suppose we have a 3-class classification problem, and the true label is class 1 ([1,0,0][1, 0, 0] in one-hot encoding). If the model predicts probabilities [0.7,0.2,0.1][0.7, 0.2, 0.1], the cross-entropy loss is:

L=(1log(0.7)+0log(0.2)+0log(0.1))=log(0.7)0.3567mathcal{L} = - left(1 cdot log(0.7) + 0 cdot log(0.2) + 0 cdot log(0.1)right) = - log(0.7) approx 0.3567

If the prediction changes to [0.2,0.4,0.4][0.2, 0.4, 0.4], the loss becomes:

L=log(0.2)1.609mathcal{L} = - log(0.2) approx 1.609

This demonstrates that more accurate predictions yield lower cross-entropy loss.


Applications

  • Binary Classification:

    • For two classes, cross-entropy reduces to the binary cross-entropy loss: L=1Ni=1N(yilog(y^i)+(1yi)log(1y^i))mathcal{L} = - frac{1}{N} sum_{i=1}^N left( y_i log(hat{y}_i) + (1 - y_i) log(1 - hat{y}_i) right)
    • Commonly used in tasks like spam detection or medical diagnosis.
  • Multi-Class Classification:

    • Used with softmax activation to handle multiple class probabilities.
  • Deep Learning:

    • Standard loss function for neural networks in tasks such as image recognition, text classification, and more.

Advantages

  1. Interpretability: It directly relates to the likelihood of the true labels, making optimization meaningful.
  2. Sensitivity: Penalizes incorrect predictions more when the confidence is misplaced.
  3. Compatibility: Naturally fits probabilistic models, like those using softmax.

Challenges

  1. Imbalanced Datasets:

    • For datasets where one class dominates, cross-entropy loss may lead to biased predictions.
    • Solutions: Weighted cross-entropy or other loss functions like focal loss.
  2. Numerical Stability:

    • When y^chat{y}_c is very close to 0, log(y^c)log(hat{y}_c) becomes undefined. To address this, implementations clip predictions to avoid taking the log of 0.

Summary

Cross-entropy loss quantifies the difference between predicted and true distributions and is widely used for classification tasks due to its alignment with probabilistic modeling and ability to penalize incorrect predictions strongly.

Vietnamese:

Hàm mất mát Cross-Entropy trong học máy

Hàm mất mát Cross-Entropy là một hàm mất mát được sử dụng phổ biến trong học máy, đặc biệt cho các bài toán phân loại. Nó đo lường sự khác biệt giữa hai phân phối xác suất: phân phối nhãn thật (dữ liệu thực tế) và xác suất dự đoán (đầu ra của mô hình). Mục tiêu là giảm thiểu sự khác biệt này bằng cách huấn luyện mô hình sao cho dự đoán gần với nhãn thật hơn.


Định nghĩa Toán học

Đối với một điểm dữ liệu duy nhất trong bài toán phân loại với CC lớp, hàm mất mát cross-entropy được định nghĩa là:

L=c=1Cyclog(y^c)mathcal{L} = - sum_{c=1}^C y_c log(hat{y}_c)

Trong đó:

  • ycy_c: Nhãn thực tế cho lớp cc (1 nếu lớp đó là đúng, 0 nếu không, trong mã hóa one-hot).
  • y^chat{y}_c: Xác suất dự đoán cho lớp cc (đầu ra của mô hình, thường được tính qua hàm softmax).

Với tập dữ liệu có NN mẫu, hàm mất mát trung bình là:

L=1Ni=1Nc=1Cyi,clog(y^i,c)mathcal{L} = - frac{1}{N} sum_{i=1}^N sum_{c=1}^C y_{i,c} log(hat{y}_{i,c})

Trực quan hóa ý nghĩa

  1. Đầu ra mang tính xác suất:

    • Cross-entropy giả định rằng đầu ra của mô hình là xác suất (các giá trị trong khoảng [0, 1] và tổng bằng 1 với tất cả các lớp).
    • Nó phạt nặng những dự đoán sai với độ tin cậy cao.
  2. Hình phạt theo hàm log:

    • Thành phần log(y^c)log(hat{y}_c) tăng mức phạt đối với các dự đoán xa nhãn thật.
    • Nếu xác suất dự đoán cho lớp đúng (y^chat{y}_c) gần bằng 1, hàm mất mát rất nhỏ.
    • Nếu y^chat{y}_c gần bằng 0, hàm mất mát rất lớn, khuyến khích mô hình không đưa ra dự đoán sai với độ tin cậy thấp.
  3. Liên hệ với hàm likelihood:

    • Cross-entropy tương đương với hàm negative log-likelihood của nhãn thật dựa trên phân phối xác suất dự đoán.

Ví dụ

Giả sử ta có một bài toán phân loại 3 lớp và nhãn thực là lớp 1 ([1,0,0][1, 0, 0] theo mã hóa one-hot). Nếu mô hình dự đoán xác suất [0.7,0.2,0.1][0.7, 0.2, 0.1], hàm mất mát cross-entropy là:

L=(1log(0.7)+0log(0.2)+0log(0.1))=log(0.7)0.3567mathcal{L} = - left(1 cdot log(0.7) + 0 cdot log(0.2) + 0 cdot log(0.1)right) = - log(0.7) approx 0.3567

Nếu dự đoán thay đổi thành [0.2,0.4,0.4][0.2, 0.4, 0.4], mất mát sẽ là:

L=log(0.2)1.609mathcal{L} = - log(0.2) approx 1.609

Điều này cho thấy rằng dự đoán chính xác hơn dẫn đến hàm mất mát cross-entropy thấp hơn.


Ứng dụng

  • Phân loại nhị phân:

    • Với hai lớp, cross-entropy trở thành binary cross-entropy: L=1Ni=1N(yilog(y^i)+(1yi)log(1y^i))mathcal{L} = - frac{1}{N} sum_{i=1}^N left( y_i log(hat{y}_i) + (1 - y_i) log(1 - hat{y}_i) right)
    • Thường được sử dụng trong các tác vụ như phát hiện spam hoặc chẩn đoán y khoa.
  • Phân loại đa lớp:

    • Được sử dụng cùng với hàm softmax để xử lý xác suất của nhiều lớp.
  • Học sâu:

    • Là hàm mất mát tiêu chuẩn cho các mạng nơ-ron trong các tác vụ như nhận diện hình ảnh, phân loại văn bản và nhiều ứng dụng khác.

Ưu điểm

  1. Dễ hiểu: Liên quan trực tiếp đến xác suất của nhãn thật, làm cho việc tối ưu hóa có ý nghĩa.
  2. Nhạy cảm: Phạt nặng các dự đoán sai, đặc biệt khi độ tin cậy bị đặt sai.
  3. Tương thích tốt: Phù hợp tự nhiên với các mô hình xác suất, như các mô hình sử dụng softmax.

Thách thức

  1. Dữ liệu mất cân bằng:

    • Với tập dữ liệu mà một lớp chiếm ưu thế, cross-entropy có thể dẫn đến dự đoán thiên lệch.
    • Giải pháp: Sử dụng cross-entropy có trọng số hoặc các hàm mất mát khác như focal loss.
  2. Ổn định số học:

    • Khi y^chat{y}_c gần bằng 0, log(y^c)log(hat{y}_c) trở nên không xác định. Để giải quyết, các triển khai thực tế thường giới hạn giá trị của dự đoán để tránh log của 0.

Kết luận

Hàm mất mát cross-entropy đo lường sự khác biệt giữa dự đoán và nhãn thật, và được sử dụng rộng rãi trong các bài toán phân loại nhờ khả năng kết hợp tự nhiên với mô hình xác suất và khả năng phạt mạnh các dự đoán sai.

🏷