Einfach 0 gelöst 0% Erfolgsquote

Softmax Function

Turn raw scores into probabilities that add up to 1, without overflowing on large values.

Das lernst du

  • Turn scores into probabilities
  • Subtract the maximum to avoid overflow
  • Make outputs sum to 1

Problem

Übersetzung anzeigen

The softmax function turns a list of scores (logits) into probabilities. Language models use it to choose the next word:

softmax(x)_i = e^(x_i) / (e^(x_1) + e^(x_2) + … + e^(x_n))

Large scores make e^x overflow, so first subtract the largest score from every score. The result is mathematically the same.

Implement softmax(logits).

Input

logits: A list of scores.

Output

Return the list of probabilities, in the same order. They are all between 0 and 1 and add up to 1.

Einschränkungen

1 ≤ length ≤ 1,000

-1,000 ≤ each score ≤ 1,000

Beispiele

Beispiel 1
Eingabe:
logits = [1.0, 2.0, 3.0]

Erwartete Ausgabe:
expected = [0.090031, 0.244728, 0.665241]
Eingabe:
std::vector<double> logits = {1.0, 2.0, 3.0};

Erwartete Ausgabe:
std::vector<double> expected = {0.090031, 0.244728, 0.665241};
Eingabe:
double[] logits = new double[] {1.0, 2.0, 3.0};

Erwartete Ausgabe:
double[] expected = new double[] {0.090031, 0.244728, 0.665241};

Erläuterung

e¹ ≈ 2.718, e² ≈ 7.389 and e³ ≈ 20.086 add up to 30.193, so the probabilities are about [0.090031, 0.244728, 0.665241].

Beispiel 2
Eingabe:
logits = [1000.0, 1000.0]

Erwartete Ausgabe:
expected = [0.5, 0.5]
Eingabe:
std::vector<double> logits = {1000.0, 1000.0};

Erwartete Ausgabe:
std::vector<double> expected = {0.5, 0.5};
Eingabe:
double[] logits = new double[] {1000.0, 1000.0};

Erwartete Ausgabe:
double[] expected = new double[] {0.5, 0.5};

Erläuterung

Computing e^1000 directly overflows. Subtracting the maximum gives [0, 0], and e⁰ / (e⁰ + e⁰) = 0.5 for both.

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

Subtract the largest logit before exponentiating so the largest transformed score becomes zero and overflow is avoided.

Hinweis 2

Compute each exponent, sum them once, then divide every exponent by that total to get probabilities.

Hinweis 3

Make sure the returned values stay in the same order as the input and still sum to one after normalization.

Erwartete Komplexität

O(n) time, O(n) extra space

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

Noch keine bestandene Lösung. Löse sie und hol dir Platz eins.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.