v9.1.1
Menu

Create a Simple Neural Network Class

easyFREEModelingPyTorch ModelingPytorch

Problem Statement

Create a basic neural network class by subclassing nn.Module.

Background

In PyTorch, neural networks are defined by subclassing nn.Module. You define the layers in the constructor and specify how data flows through them in the forward method.

Your Task

Write a function create_simple_network() that defines a single-layer neural network (10 inputs → 5 outputs) and returns a dictionary describing the network.

Output Format

Return a dictionary with keys: "class_name" (string), "is_module" (boolean), "output_shape" (list — shape when given a single sample of 10 features).

Example

Input
None
Output
{'class_name': 'SimpleNet', 'is_module': True, 'output_shape': [1, 5]}
Reasoning
  • Input: the function takes no arguments. Internally it builds a network and feeds it one test sample: a single row of 10 random features, i.e. a 1×101 \times 10 batch (1 sample, 10 features).
  • The math: a single fully connected layer computes y=Wx+by = Wx + b, where WW is a 5×105 \times 10 weight matrix and bb is a bias of length 5. Each output yj=∑i=110Wjixi+bjy_j = \sum_{i=1}^{10} W_{ji} x_i + b_j, so 10 inputs collapse into 5 numbers. That is 5×10+5=555 \times 10 + 5 = 55 learnable parameters.
  • Shape: the batch dimension passes through untouched, so 1×10→1×51 \times 10 \to 1 \times 5, giving output_shape = [1, 5]. A batch of 32 samples would give 32×532 \times 5.
  • class_name: the name of the class you defined, SimpleNet.
  • is_module: True, because the class inherits from PyTorch's module base class, so it is a module.
  • Notice that the actual output values are random (the input and initial weights are random); only the shape is checked.

Constraints

  • Must subclass nn.Module
  • Must call super().init() in init
  • Use nn.Linear(10, 5)

Learn this in

More from Pytorch

solution.py

Test results

Sign in to run your code against the test cases.