ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Categorical Cross-Entropy Loss

Measure the loss of a multi-class classifier from its predicted probabilities and the true classes.

สิ่งที่คุณจะได้เรียนรู้

  • Score predicted probabilities against true classes
  • Average the loss over samples
  • Avoid log(0) with clipping

โจทย์

ดูฉบับแปล

A multi-class classifier outputs one probability per class (for example from softmax). Categorical cross-entropy punishes it for giving the correct class a low probability:

loss = -(ln(p₁) + ln(p₂) + … + ln(pₙ)) / n

where pᵢ is the probability the model gave to the correct class of sample i. To avoid ln(0), first raise any probability below 0.0000001 to 0.0000001.

Implement categorical_cross_entropy(probabilities, labels).

Input

probabilities: One row per sample, with one probability per class.

labels: The index of the correct class for each sample (0-based).

Output

Return the average loss as a decimal number.

เงื่อนไข

1 ≤ samples ≤ 1,000, 2 ≤ classes ≤ 100

0 ≤ each probability ≤ 1

0 ≤ labels[i] < number of classes

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
probabilities = [
    [0.7, 0.2, 0.1],
    [0.1, 0.8, 0.1]
]
labels = [0, 1]

ผลลัพธ์ที่คาดหวัง:
expected = 0.289909
อินพุต:
std::vector<std::vector<double>> probabilities = {
    {0.7, 0.2, 0.1},
    {0.1, 0.8, 0.1}
};
std::vector<long long> labels = {0, 1};

ผลลัพธ์ที่คาดหวัง:
double expected = 0.289909;
อินพุต:
double[][] probabilities = new double[][] {
    new double[] {0.7, 0.2, 0.1},
    new double[] {0.1, 0.8, 0.1}
};
long[] labels = new long[] {0, 1};

ผลลัพธ์ที่คาดหวัง:
double expected = 0.289909;

คำอธิบาย

The correct classes got 0.7 and 0.8: -(ln(0.7) + ln(0.8)) / 2 = -(-0.356675 - 0.223144) / 2 ≈ 0.289909.

ตัวอย่างที่ 2
อินพุต:
probabilities = [
    [1.0, 0.0]
]
labels = [1]

ผลลัพธ์ที่คาดหวัง:
expected = 16.118096
อินพุต:
std::vector<std::vector<double>> probabilities = {
    {1.0, 0.0}
};
std::vector<long long> labels = {1};

ผลลัพธ์ที่คาดหวัง:
double expected = 16.118096;
อินพุต:
double[][] probabilities = new double[][] {
    new double[] {1.0, 0.0}
};
long[] labels = new long[] {1};

ผลลัพธ์ที่คาดหวัง:
double expected = 16.118096;

คำอธิบาย

The correct class got probability 0, which is raised to 0.0000001, so the loss is -ln(0.0000001) ≈ 16.118096: a confident wrong answer is punished hard.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Look up the probability assigned to each sample’s correct class, then average the negative logs of those values.

คำใบ้ 2

Clamp very small probabilities upward before taking logs to avoid undefined results.

คำใบ้ 3

Only the labeled class matters for each row, so ignore the other class probabilities when computing the loss.

ความซับซ้อนที่คาดหวัง

O(samples) time, O(1) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก