TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Study Plan
Evaluate a Dense Layer
Neural Networks
Medium

Evaluate a dense layer whose weight rows and bias entries correspond to neurons in output order.

a=Wx+b\mathbf{a}=W\mathbf{x}+\mathbf{b}a=Wx+b

Here x is inputs with length n, W is weight_matrix with shape (m, n), b is biases with length m, and a contains the m preactivations. When nonlinear is true, apply tanh to each preactivation; otherwise return the preactivations. Either width may be zero.

Return a one-dimensional PyTorch tensor of shape (m,) in weight-row order, preserving the common input dtype and device. A one-neuron result remains a vector; zero neurons produce an empty vector.

Loading visualization...

Examples

Input: inputs = [1,2,-1], weight_matrix = [[1,0,1],[0,2,0]], biases = [0.5,-0.5], nonlinear = false

Output: [0.5, 3.5]

Explanation: The first row gives 0.5 after bias, and the second gives 3.5.

Input: inputs = [2,1], weight_matrix = [[1,-1]], biases = [0], nonlinear = true

Output: [0.761594]

Input: inputs = [3,-2], weight_matrix = [[0,0],[0,0]], biases = [0,1], nonlinear = false

Output: [0.0, 1.0]

Hint 1

torch.mv computes a matrix-vector product.

Hint 2

torch.tanh applies independently to every tensor entry.

Hint 3

Avoid squeeze: a one-neuron layer still returns a vector.

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 has shape (n,), weight_matrix has shape (m, n), and biases has shape (m,).
  • m and n are nonnegative integers; nonlinear is a Python boolean.
  • All tensors are finite and share one floating-point dtype and device; tensors may be noncontiguous.
  • Inputs produce finite outputs.
Try Similar Problems
Build a Scalar Neuron ModuleMediumCollect 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: array

You must run your code first.

Evaluate a Dense Layer

Neural Networks
Medium

Evaluate a dense layer whose weight rows and bias entries correspond to neurons in output order.

a=Wx+b\mathbf{a}=W\mathbf{x}+\mathbf{b}a=Wx+b

Here x is inputs with length n, W is weight_matrix with shape (m, n), b is biases with length m, and a contains the m preactivations. When nonlinear is true, apply tanh to each preactivation; otherwise return the preactivations. Either width may be zero.

Return a one-dimensional PyTorch tensor of shape (m,) in weight-row order, preserving the common input dtype and device. A one-neuron result remains a vector; zero neurons produce an empty vector.

Loading visualization...

Examples

Input: inputs = [1,2,-1], weight_matrix = [[1,0,1],[0,2,0]], biases = [0.5,-0.5], nonlinear = false

Output: [0.5, 3.5]

Explanation: The first row gives 0.5 after bias, and the second gives 3.5.

Input: inputs = [2,1], weight_matrix = [[1,-1]], biases = [0], nonlinear = true

Output: [0.761594]

Input: inputs = [3,-2], weight_matrix = [[0,0],[0,0]], biases = [0,1], nonlinear = false

Output: [0.0, 1.0]

Hint 1

torch.mv computes a matrix-vector product.

Hint 2

torch.tanh applies independently to every tensor entry.

Hint 3

Avoid squeeze: a one-neuron layer still returns a vector.

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 has shape (n,), weight_matrix has shape (m, n), and biases has shape (m,).
  • m and n are nonnegative integers; nonlinear is a Python boolean.
  • All tensors are finite and share one floating-point dtype and device; tensors may be noncontiguous.
  • Inputs produce finite outputs.
Try Similar Problems
Build a Scalar Neuron ModuleMediumCollect 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: array

You must run your code first.