Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Convolutional Neural Network in PyTorch

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Convolutional Neural Network in PyTorch. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Implement the inference path of a small convolutional neural network used to classify frames from a Bosch AUTODOME IP starlight 7000i camera. Do not use PyTorch convolution or pooling helpers. Implement the tensor operations directly with Python lists so that padding, stride, channel aggregation, activation, and pooling behavior are explicit.

The network has one convolutional layer, ReLU activation, non-overlapping max pooling, flattening, and a fully connected output layer. Return the index of the class with the largest logit. If logits tie, return the smallest class index.

Formal Specification

Implement cnn_predict(image, kernels, biases, dense_weights, dense_bias, stride, padding, pool_size).

  • image is a 3D list with shape [input_channels][height][width].
  • kernels is a 4D list with shape [output_channels][input_channels][kernel_size][kernel_size].
  • biases contains one value per output channel.
  • dense_weights has shape [classes][flattened_features].
  • dense_bias contains one value per class.
  • Convolution uses zero padding and the specified stride.
  • Apply ReLU before max pooling.
  • Pooling uses square windows of size pool_size and stride pool_size.
  • Return an integer class index.

Constraints

  • 1 <= input_channels, output_channels, classes <= 8
  • 1 <= height, width <= 32
  • 1 <= kernel_size <= 7 and kernel_size is odd
  • 0 <= padding <= kernel_size // 2
  • 1 <= stride <= 3
  • The convolution output dimensions are divisible by pool_size
  • All tensor shapes are valid and consistent

Function Signature

def cnn_predict(image, kernels, biases, dense_weights, dense_bias, stride, padding, pool_size):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output