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

Dense Layer Forward Pass

Compute the outputs of a fully connected layer of neurons with a choice of activation.

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

  • Compute a full layer of neurons at once
  • Apply a chosen activation to each output
  • Organize weights as one row per neuron

โจทย์

ดูฉบับแปล

A dense layer is a group of neurons that all receive the same inputs. Neuron j has its own weights weights[j] and bias biases[j]:

z_j = inputs · weights[j] + biases[j]

Then the layer's activation is applied to each z_j:

  • "linear": output z_j unchanged
  • "relu": max(0, z_j)
  • "sigmoid": 1 / (1 + e^(-z_j))

Implement dense_forward(inputs, weights, biases, activation).

Input

inputs: The n input values.

weights: One row per neuron; each row has n weights.

biases: One bias per neuron.

activation: "linear", "relu" or "sigmoid".

Output

Return the list of outputs, one per neuron, in order.

เงื่อนไข

1 ≤ n ≤ 100, 1 ≤ number of neurons ≤ 100

-10 ≤ each value ≤ 10

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
biases = [0.0, -1.0]
inputs = [1.0, 2.0]
weights = [
    [0.5, -1.0],
    [1.5, 0.5]
]
activation = "relu"

ผลลัพธ์ที่คาดหวัง:
expected = [0.0, 1.5]
อินพุต:
std::vector<double> biases = {0.0, -1.0};
std::vector<double> inputs = {1.0, 2.0};
std::vector<std::vector<double>> weights = {
    {0.5, -1.0},
    {1.5, 0.5}
};
std::string activation = "relu";

ผลลัพธ์ที่คาดหวัง:
std::vector<double> expected = {0.0, 1.5};
อินพุต:
double[] biases = new double[] {0.0, -1.0};
double[] inputs = new double[] {1.0, 2.0};
double[][] weights = new double[][] {
    new double[] {0.5, -1.0},
    new double[] {1.5, 0.5}
};
string activation = "relu";

ผลลัพธ์ที่คาดหวัง:
double[] expected = new double[] {0.0, 1.5};

คำอธิบาย

Neuron 1: 1×0.5 + 2×(-1) + 0 = -1.5, and ReLU gives 0. Neuron 2: 1×1.5 + 2×0.5 - 1 = 1.5, which ReLU keeps. Result: [0, 1.5].

ตัวอย่างที่ 2
อินพุต:
biases = [1.0, 0.0, -1.5]
inputs = [3.0]
weights = [
    [2.0],
    [-1.0],
    [0.5]
]
activation = "linear"

ผลลัพธ์ที่คาดหวัง:
expected = [7.0, -3.0, 0.0]
อินพุต:
std::vector<double> biases = {1.0, 0.0, -1.5};
std::vector<double> inputs = {3.0};
std::vector<std::vector<double>> weights = {
    {2.0},
    {-1.0},
    {0.5}
};
std::string activation = "linear";

ผลลัพธ์ที่คาดหวัง:
std::vector<double> expected = {7.0, -3.0, 0.0};
อินพุต:
double[] biases = new double[] {1.0, 0.0, -1.5};
double[] inputs = new double[] {3.0};
double[][] weights = new double[][] {
    new double[] {2.0},
    new double[] {-1.0},
    new double[] {0.5}
};
string activation = "linear";

ผลลัพธ์ที่คาดหวัง:
double[] expected = new double[] {7.0, -3.0, 0.0};

คำอธิบาย

With a linear activation the outputs are just 3×2 + 1 = 7, 3×(-1) + 0 = -3 and 3×0.5 - 1.5 = 0.

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

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

คำใบ้ 1

Treat each neuron separately: each one has its own weight row and bias.

คำใบ้ 2

Compute every neuron's dot product with the input vector, add its bias, then apply the chosen activation.

คำใบ้ 3

Make sure the activation choice is applied after the linear step, and preserve neuron order in the output.

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

O(mn) time, O(m) extra space

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

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

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

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

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