TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Study Plan
Build a Scalar Neuron Module
Neural Networks
Medium

Evaluate a neuron using the supplied inputs, weights, and bias.

a=b+∑i=1nwixia=b+\sum_{i=1}^{n}w_i x_ia=b+i=1∑n​wi​xi​

Here x is inputs, w is weights, b is bias, n is the vector length, i indexes matching entries, and a is the preactivation. When nonlinear is true, the output is tanh of a; otherwise it is a. Empty vectors leave the bias as the preactivation.

Return one zero-dimensional PyTorch tensor, preserving the common input dtype and device.

Loading visualization...

Examples

Input: inputs = [1,2], weights = [0.5,-1], bias = 0.25, nonlinear = false

Output: -1.25

Explanation: The weighted sum is -1.5; adding the bias gives -1.25.

Input: inputs = [0], weights = [2], bias = 0, nonlinear = true

Output: 0.0

Input: inputs = [], weights = [], bias = 1.5, nonlinear = true

Output: 0.905148

Hint 1

torch.sum reduces an empty vector to scalar zero.

Hint 2

torch.tanh preserves a tensor’s shape, dtype, and device.

Requirements

  • Use PyTorch and the supplied parameters without modifying them.
  • Preserve the shared float16, float32, or float64 dtype; examples and custom Run inputs use float64.

Constraints

  • inputs and weights are finite one-dimensional tensors of equal length, which may be zero.
  • bias is a finite zero-dimensional tensor and nonlinear is a Python boolean.
  • All tensors share one floating-point dtype and device; vectors may be noncontiguous.
  • Inputs produce a finite output.
Try Similar Problems
Evaluate a Dense LayerMediumCollect Module Parameters RecursivelyMediumEvaluate a Multi-Layer PerceptronHardBackpropagate Through a Scalar NeuronMediumTopologically Sort a Computation DAGMediumAccumulate Shared-Path GradientsMediumTrace a Reachable Expression GraphMediumGradient-Check a Scalar NeuronMediumAutoencoder Forward PassMediumGradient AccumulationMediumDQN Loss with Target NetworkMediumWeight InitializationMedium

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Build a Scalar Neuron Module

Neural Networks
Medium

Evaluate a neuron using the supplied inputs, weights, and bias.

a=b+∑i=1nwixia=b+\sum_{i=1}^{n}w_i x_ia=b+i=1∑n​wi​xi​

Here x is inputs, w is weights, b is bias, n is the vector length, i indexes matching entries, and a is the preactivation. When nonlinear is true, the output is tanh of a; otherwise it is a. Empty vectors leave the bias as the preactivation.

Return one zero-dimensional PyTorch tensor, preserving the common input dtype and device.

Loading visualization...

Examples

Input: inputs = [1,2], weights = [0.5,-1], bias = 0.25, nonlinear = false

Output: -1.25

Explanation: The weighted sum is -1.5; adding the bias gives -1.25.

Input: inputs = [0], weights = [2], bias = 0, nonlinear = true

Output: 0.0

Input: inputs = [], weights = [], bias = 1.5, nonlinear = true

Output: 0.905148

Hint 1

torch.sum reduces an empty vector to scalar zero.

Hint 2

torch.tanh preserves a tensor’s shape, dtype, and device.

Requirements

  • Use PyTorch and the supplied parameters without modifying them.
  • Preserve the shared float16, float32, or float64 dtype; examples and custom Run inputs use float64.

Constraints

  • inputs and weights are finite one-dimensional tensors of equal length, which may be zero.
  • bias is a finite zero-dimensional tensor and nonlinear is a Python boolean.
  • All tensors share one floating-point dtype and device; vectors may be noncontiguous.
  • Inputs produce a finite output.
Try Similar Problems
Evaluate a Dense LayerMediumCollect Module Parameters RecursivelyMediumEvaluate a Multi-Layer PerceptronHardBackpropagate Through a Scalar NeuronMediumTopologically Sort a Computation DAGMediumAccumulate Shared-Path GradientsMediumTrace a Reachable Expression GraphMediumGradient-Check a Scalar NeuronMediumAutoencoder Forward PassMediumGradient AccumulationMediumDQN Loss with Target NetworkMediumWeight InitializationMedium

Accepts: array

Accepts: array

Accepts: number

You must run your code first.