Single Artificial Neuron Forward Pass (z = w·x + b)

Basics
BasicsPyTorchNeuronGoogleMetaAmazon

Problem

Implement single_neuron(x, w, b) for 1D float tensors x and w (same length D) and a scalar bias tensor b, returning the scalar pre-activation z=w⊤x+bz = w^\top x + b.

Why It Matters: A single artificial neuron is the atomic unit of every perceptron, logistic regression model, and deep neural network layer.

Function Signature

import torch

def single_neuron(x: torch.Tensor, w: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
    """Compute z = w · x + b for a single artificial neuron."""

Sample Input & Output

Example 1: Standard Weighted Sum + Bias

Input:

x = torch.tensor([1.0, 2.0]), w = torch.tensor([3.0, -1.0]), b = torch.tensor(0.5)

Expected Output:

tensor(1.5)

Explanation: 1.0*3.0 + 2.0*(-1.0) + 0.5 = 3.0 - 2.0 + 0.5 = 1.5.

Example 2: Orthogonal Input & Weight

Input:

x = torch.tensor([1.0, 0.0]), w = torch.tensor([0.0, 5.0]), b = torch.tensor(-2.0)

Expected Output:

tensor(-2.0)

Explanation: The dot product is 0.0, leaving only the scalar bias -2.0.

Constraints & Edge Cases

  • Return a scalar PyTorch tensor.
  • Return a PyTorch scalar tensor (preserving autograd if inputs require gradients), not a plain Python float.
  • Use torch.dot(w, x) + b or (w * x).sum() + b.

Constraints & Evaluation Criteria

  • Return a scalar PyTorch tensor.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
x=[1,2], w=[0.5,-1], b=0.5
Expected Output / Criteria
-1.0