Tất cả thử thách
Dễ 1 đã giải Tỷ lệ chấp nhận 100.0%

Single Artificial Neuron

Compute the output of one neuron: weighted sum of the inputs plus a bias, passed through sigmoid.

Bạn sẽ học được gì

  • Combine inputs, weights and a bias
  • Pass the sum through an activation
  • See how a neuron makes a prediction

Đề bài

Xem bản dịch

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.

Ràng buộc

1 ≤ length of inputs = length of weights ≤ 100

-10 ≤ each input, weight and bias ≤ 10

Ví dụ

Ví dụ 1
Đầu vào:
inputs = [1.0, 2.0, 3.0]
weights = [0.2, 0.4, 0.6]
bias = 0.5

Kết quả mong đợi:
expected = 0.964429
Đầu vào:
std::vector<double> inputs = {1.0, 2.0, 3.0};
std::vector<double> weights = {0.2, 0.4, 0.6};
double bias = 0.5;

Kết quả mong đợi:
double expected = 0.964429;
Đầu vào:
double[] inputs = new double[] {1.0, 2.0, 3.0};
double[] weights = new double[] {0.2, 0.4, 0.6};
double bias = 0.5;

Kết quả mong đợi:
double expected = 0.964429;

Giải thích

z = 1×0.2 + 2×0.4 + 3×0.6 + 0.5 = 3.3, and sigmoid(3.3) ≈ 0.964429.

Ví dụ 2
Đầu vào:
inputs = [0.5, -1.0]
weights = [2.0, 1.0]
bias = 0.0

Kết quả mong đợi:
expected = 0.5
Đầu vào:
std::vector<double> inputs = {0.5, -1.0};
std::vector<double> weights = {2.0, 1.0};
double bias = 0.0;

Kết quả mong đợi:
double expected = 0.5;
Đầu vào:
double[] inputs = new double[] {0.5, -1.0};
double[] weights = new double[] {2.0, 1.0};
double bias = 0.0;

Kết quả mong đợi:
double expected = 0.5;

Giải thích

z = 0.5×2 + (-1)×1 + 0 = 0, and sigmoid(0) = 0.5: the neuron is exactly undecided.

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

This is just a weighted sum of the inputs plus a bias, followed by one sigmoid.

Gợi ý 2

Pair each input with its corresponding weight and accumulate the products before adding the bias.

Gợi ý 3

Watch that the final output is passed through sigmoid, not returned as the raw sum.

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 FSFilippo Sanguineti C++ 3,185 ms - 4/5 Xem code

Học hỏi từ người khác

Giải pháp cộng đồng

Cảnh báo: Có 1 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem mã của người khác và đánh giá AI. Đăng nhập và giải thử thách để tham gia thảo luận.