TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Study Plan
Evaluate a Multi-Layer Perceptron
Neural Networks
Hard

Evaluate a multilayer perceptron using the supplied weight matrices and bias vectors in forward order. Apply tanh after every layer, including the final layer.

hℓ=tanh⁡(Wℓhℓ−1+bℓ)\mathbf{h}_{\ell}=\tanh(W_{\ell}\mathbf{h}_{\ell-1}+\mathbf{b}_{\ell})hℓ​=tanh(Wℓ​hℓ−1​+bℓ​)

Here h is a layer's output, the initial h is inputs, W is its weight matrix, b is its bias vector, and the layer index runs through the supplied lists. Each weight row represents one output neuron.

Return a tuple containing the final output tensor and a Python list of every layer's output tensor in forward order. All output tensors are one-dimensional and preserve the common input dtype and device. The list includes the final output.

Loading visualization...

Examples

Input: inputs = [1,2], weights = [[[1,0],[0,1]]], biases = [[0,0]]

Output: ([0.761594, 0.964028], [[0.761594, 0.964028]])

Explanation: The identity weight matrix leaves the inputs unchanged before tanh; the trace contains this single layer’s output.

Input: inputs = [1], weights = [[[1],[-1]],[[1,1]]], biases = [[0,0],[0]]

Output: ([0.0], [[0.761594, -0.761594], [0.0]])

Input: inputs = [10], weights = [[[2]]], biases = [[0]]

Output: ([1.0], [[1.0]])

Hint 1

torch.mv computes a matrix-vector product.

Hint 2

zip pairs each layer’s weight matrix with its bias vector.

Hint 3

list.append can retain each layer output without flattening it.

Requirements

  • Use PyTorch without modifying inputs, weights, or biases.
  • Preserve the shared float16, float32, or float64 dtype and device; examples and custom Run inputs use float64.

Constraints

  • inputs is a one-dimensional tensor.
  • weights and biases are nonempty Python lists of equal length.
  • Each weight matrix has shape (output width, input width), and its bias vector has the output width.
  • Every output width is positive, and adjacent layer widths match.
  • All tensors contain finite values and share one floating-point dtype and device; noncontiguous tensors are valid.
  • All layer outputs are finite.
Try Similar Problems
Evaluate a Dense LayerMediumBuild a Scalar Neuron ModuleMediumCollect Module Parameters RecursivelyMediumTrain a Tiny Micrograd MLPHardRun Reverse-Mode AutodiffHardGradient-Check an Autodiff GraphHardBackpropagate Through a Scalar NeuronMediumTopologically Sort a Computation DAGMediumVision Transformer Patch EmbeddingHardLSTM Cell from ScratchHardEarly StoppingHardBackpropagation Through a Computation GraphHard

Accepts: array

Accepts: array

Accepts: array

You must run your code first.

Evaluate a Multi-Layer Perceptron

Neural Networks
Hard

Evaluate a multilayer perceptron using the supplied weight matrices and bias vectors in forward order. Apply tanh after every layer, including the final layer.

hℓ=tanh⁡(Wℓhℓ−1+bℓ)\mathbf{h}_{\ell}=\tanh(W_{\ell}\mathbf{h}_{\ell-1}+\mathbf{b}_{\ell})hℓ​=tanh(Wℓ​hℓ−1​+bℓ​)

Here h is a layer's output, the initial h is inputs, W is its weight matrix, b is its bias vector, and the layer index runs through the supplied lists. Each weight row represents one output neuron.

Return a tuple containing the final output tensor and a Python list of every layer's output tensor in forward order. All output tensors are one-dimensional and preserve the common input dtype and device. The list includes the final output.

Loading visualization...

Examples

Input: inputs = [1,2], weights = [[[1,0],[0,1]]], biases = [[0,0]]

Output: ([0.761594, 0.964028], [[0.761594, 0.964028]])

Explanation: The identity weight matrix leaves the inputs unchanged before tanh; the trace contains this single layer’s output.

Input: inputs = [1], weights = [[[1],[-1]],[[1,1]]], biases = [[0,0],[0]]

Output: ([0.0], [[0.761594, -0.761594], [0.0]])

Input: inputs = [10], weights = [[[2]]], biases = [[0]]

Output: ([1.0], [[1.0]])

Hint 1

torch.mv computes a matrix-vector product.

Hint 2

zip pairs each layer’s weight matrix with its bias vector.

Hint 3

list.append can retain each layer output without flattening it.

Requirements

  • Use PyTorch without modifying inputs, weights, or biases.
  • Preserve the shared float16, float32, or float64 dtype and device; examples and custom Run inputs use float64.

Constraints

  • inputs is a one-dimensional tensor.
  • weights and biases are nonempty Python lists of equal length.
  • Each weight matrix has shape (output width, input width), and its bias vector has the output width.
  • Every output width is positive, and adjacent layer widths match.
  • All tensors contain finite values and share one floating-point dtype and device; noncontiguous tensors are valid.
  • All layer outputs are finite.
Try Similar Problems
Evaluate a Dense LayerMediumBuild a Scalar Neuron ModuleMediumCollect Module Parameters RecursivelyMediumTrain a Tiny Micrograd MLPHardRun Reverse-Mode AutodiffHardGradient-Check an Autodiff GraphHardBackpropagate Through a Scalar NeuronMediumTopologically Sort a Computation DAGMediumVision Transformer Patch EmbeddingHardLSTM Cell from ScratchHardEarly StoppingHardBackpropagation Through a Computation GraphHard

Accepts: array

Accepts: array

Accepts: array

You must run your code first.