問題
An artificial neuron multiplies each input by a weight, adds a bias, and passes the result through an activation function:
z = x₁×w₁ + x₂×w₂ + … + xₙ×wₙ + bias
output = sigmoid(z) = 1 / (1 + e^(-z))
Implement neuron_output(inputs, weights, bias).
Input
inputs: The input values.
weights: One weight per input (same length as inputs).
bias: The neuron's bias.
Output
Return the neuron's output, a decimal number between 0 and 1.
制約
1 ≤ length of inputs = length of weights ≤ 100
-10 ≤ each input, weight and bias ≤ 10
例
入力:
bias = 0.5
inputs = [1.0, 2.0, 3.0]
weights = [0.2, 0.4, 0.6]
期待される出力:
expected = 0.964429
入力:
double bias = 0.5;
std::vector<double> inputs = {1.0, 2.0, 3.0};
std::vector<double> weights = {0.2, 0.4, 0.6};
期待される出力:
double expected = 0.964429;
入力:
double bias = 0.5;
double[] inputs = new double[] {1.0, 2.0, 3.0};
double[] weights = new double[] {0.2, 0.4, 0.6};
期待される出力:
double expected = 0.964429;
解説
z = 1×0.2 + 2×0.4 + 3×0.6 + 0.5 = 3.3, and sigmoid(3.3) ≈ 0.964429.
入力:
bias = 0.0
inputs = [0.5, -1.0]
weights = [2.0, 1.0]
期待される出力:
expected = 0.5
入力:
double bias = 0.0;
std::vector<double> inputs = {0.5, -1.0};
std::vector<double> weights = {2.0, 1.0};
期待される出力:
double expected = 0.5;
入力:
double bias = 0.0;
double[] inputs = new double[] {0.5, -1.0};
double[] weights = new double[] {2.0, 1.0};
期待される出力:
double expected = 0.5;
解説
z = 0.5×2 + (-1)×1 + 0 = 0, and sigmoid(0) = 0.5: the neuron is exactly undecided.
解き方のヒント
まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。
ヒント 1
This is just a weighted sum of the inputs plus a bias, followed by one sigmoid.
ヒント 2
Pair each input with its corresponding weight and accumulate the products before adding the bias.
ヒント 3
Watch that the final output is passed through sigmoid, not returned as the raw sum.
目安の計算量
O(n) time, O(1) extra space
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。