Alle Herausforderungen
Mittel 0 gelöst 0% Erfolgsquote

Forward Propagation in a Two-Layer Network

Run inputs through a hidden ReLU layer and a sigmoid output layer.

Das lernst du

  • Chain a hidden layer and an output layer
  • Feed one layer's output into the next
  • Run a full network prediction

Problem

Übersetzung anzeigen

A small neural network has a hidden layer and an output layer. Data flows forward through them:

  1. Hidden layer: h_j = ReLU(inputs · hidden_weights[j] + hidden_biases[j]), where ReLU(z) = max(0, z)
  2. Output layer: y_k = sigmoid(h · output_weights[k] + output_biases[k]), where sigmoid(z) = 1 / (1 + e^(-z))

Implement forward_pass(inputs, hidden_weights, hidden_biases, output_weights, output_biases).

Input

inputs: The n input values.

hidden_weights, hidden_biases: The hidden layer, one row and one bias per hidden neuron.

output_weights, output_biases: The output layer, one row and one bias per output; each row has one weight per hidden neuron.

Output

Return the list of network outputs y.

Einschränkungen

1 ≤ inputs, hidden neurons, outputs ≤ 50

-10 ≤ each value ≤ 10

Beispiele

Beispiel 1
Eingabe:
inputs = [1.0, 0.5]
hidden_weights = [
    [1.0, -2.0],
    [0.5, 1.0]
]
hidden_biases = [0.0, 0.5]
output_weights = [
    [1.0, -1.0]
]
output_biases = [0.0]

Erwartete Ausgabe:
expected = [0.182426]
Eingabe:
std::vector<double> inputs = {1.0, 0.5};
std::vector<std::vector<double>> hidden_weights = {
    {1.0, -2.0},
    {0.5, 1.0}
};
std::vector<double> hidden_biases = {0.0, 0.5};
std::vector<std::vector<double>> output_weights = {
    {1.0, -1.0}
};
std::vector<double> output_biases = {0.0};

Erwartete Ausgabe:
std::vector<double> expected = {0.182426};
Eingabe:
double[] inputs = new double[] {1.0, 0.5};
double[][] hidden_weights = new double[][] {
    new double[] {1.0, -2.0},
    new double[] {0.5, 1.0}
};
double[] hidden_biases = new double[] {0.0, 0.5};
double[][] output_weights = new double[][] {
    new double[] {1.0, -1.0}
};
double[] output_biases = new double[] {0.0};

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

Erläuterung

Hidden: ReLU(1×1 + 0.5×(-2) + 0) = ReLU(0) = 0 and ReLU(1×0.5 + 0.5×1 + 0.5) = 1.5. Output: sigmoid(0×1 + 1.5×(-1) + 0) = sigmoid(-1.5) ≈ 0.182426.

Beispiel 2
Eingabe:
inputs = [2.0]
hidden_weights = [
    [1.0],
    [-1.0]
]
hidden_biases = [0.0, 0.0]
output_weights = [
    [0.5, 0.5],
    [-0.5, 2.0]
]
output_biases = [-1.0, 0.0]

Erwartete Ausgabe:
expected = [0.5, 0.268941]
Eingabe:
std::vector<double> inputs = {2.0};
std::vector<std::vector<double>> hidden_weights = {
    {1.0},
    {-1.0}
};
std::vector<double> hidden_biases = {0.0, 0.0};
std::vector<std::vector<double>> output_weights = {
    {0.5, 0.5},
    {-0.5, 2.0}
};
std::vector<double> output_biases = {-1.0, 0.0};

Erwartete Ausgabe:
std::vector<double> expected = {0.5, 0.268941};
Eingabe:
double[] inputs = new double[] {2.0};
double[][] hidden_weights = new double[][] {
    new double[] {1.0},
    new double[] {-1.0}
};
double[] hidden_biases = new double[] {0.0, 0.0};
double[][] output_weights = new double[][] {
    new double[] {0.5, 0.5},
    new double[] {-0.5, 2.0}
};
double[] output_biases = new double[] {-1.0, 0.0};

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

Erläuterung

Hidden: ReLU(2) = 2 and ReLU(-2) = 0. Output 1: sigmoid(2×0.5 + 0 - 1) = sigmoid(0) = 0.5. Output 2: sigmoid(2×(-0.5) + 0) = sigmoid(-1) ≈ 0.268941.

So gehst du vor

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

Hinweis 1

First compute all hidden activations from the inputs, then use those hidden outputs for the final layer.

Hinweis 2

Each layer is a batch of dot products plus biases, with ReLU in the hidden layer and sigmoid at the output.

Hinweis 3

Do not mix original inputs into the second layer; it only sees the hidden layer outputs.

Erwartete Komplexität

O(hn + oh) time, O(h + o) 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.