TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Study Plan
Collect Module Parameters Recursively
Neural Networks
Medium

Collect the scalar parameters of a multilayer perceptron from the supplied weight matrices and bias vectors. Traverse layers in list order, then neurons in weight-row order. For each neuron, include its weights in column order followed immediately by its bias.

Return a tuple containing a Python list of zero-dimensional PyTorch tensor views and a Python integer equal to the list's length. Every view must refer to its original parameter entry, preserving its dtype and device; copied tensors and Python numbers are not valid substitutes.

Loading visualization...

Examples

Input: weights = [[[1,2],[3,4]]], biases = [[5,6]]

Output: ([1.0, 2.0, 5.0, 3.0, 4.0, 6.0], 6)

Explanation: Each neuron contributes its two weight entries followed by its bias, for six scalar views.

Input: weights = [[[1,-1],[2,-2]],[[3,4]]], biases = [[0.5,-0.5],[1]]

Output: ([1.0, -1.0, 0.5, 2.0, -2.0, -0.5, 3.0, 4.0, 1.0], 9)

Input: weights = [[[-2.5]]], biases = [[0.25]]

Output: ([-2.5, 0.25], 2)

Hint 1

Integer tensor indexing produces a scalar view.

Hint 2

zip pairs matching weight matrices and bias vectors.

Hint 3

len gives the number of collected entries.

Requirements

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

Constraints

  • weights and biases are nonempty Python lists of equal length.
  • Each weight matrix has shape (output width, input width), with both widths positive; each bias vector matches its output width.
  • Adjacent layer widths match.
  • All tensors contain finite values and share one floating-point dtype and device; noncontiguous tensors are valid.
Try Similar Problems
Evaluate a Dense LayerMediumBuild a Scalar Neuron ModuleMediumEvaluate 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

You must run your code first.

Collect Module Parameters Recursively

Neural Networks
Medium

Collect the scalar parameters of a multilayer perceptron from the supplied weight matrices and bias vectors. Traverse layers in list order, then neurons in weight-row order. For each neuron, include its weights in column order followed immediately by its bias.

Return a tuple containing a Python list of zero-dimensional PyTorch tensor views and a Python integer equal to the list's length. Every view must refer to its original parameter entry, preserving its dtype and device; copied tensors and Python numbers are not valid substitutes.

Loading visualization...

Examples

Input: weights = [[[1,2],[3,4]]], biases = [[5,6]]

Output: ([1.0, 2.0, 5.0, 3.0, 4.0, 6.0], 6)

Explanation: Each neuron contributes its two weight entries followed by its bias, for six scalar views.

Input: weights = [[[1,-1],[2,-2]],[[3,4]]], biases = [[0.5,-0.5],[1]]

Output: ([1.0, -1.0, 0.5, 2.0, -2.0, -0.5, 3.0, 4.0, 1.0], 9)

Input: weights = [[[-2.5]]], biases = [[0.25]]

Output: ([-2.5, 0.25], 2)

Hint 1

Integer tensor indexing produces a scalar view.

Hint 2

zip pairs matching weight matrices and bias vectors.

Hint 3

len gives the number of collected entries.

Requirements

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

Constraints

  • weights and biases are nonempty Python lists of equal length.
  • Each weight matrix has shape (output width, input width), with both widths positive; each bias vector matches its output width.
  • Adjacent layer widths match.
  • All tensors contain finite values and share one floating-point dtype and device; noncontiguous tensors are valid.
Try Similar Problems
Evaluate a Dense LayerMediumBuild a Scalar Neuron ModuleMediumEvaluate 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

You must run your code first.