Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Python Neural Network Implementation

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

Your question is Python Neural Network Implementation. 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

Capgemini GenAI engineering workflows may require implementing lightweight model components without relying on a machine learning framework. Write a Python function that trains a fully connected neural network with one sigmoid hidden layer and one sigmoid output neuron, then returns predictions for the training samples.

Use batch gradient descent and binary cross-entropy loss. The initial weights and biases are provided, so the function must be deterministic.

Formal Specification

Implement train_network(X, y, W1, b1, W2, b2, epochs, learning_rate).

  • X is a non-empty list of n samples, each containing d floats.
  • y is a list of n binary labels, either 0 or 1.
  • W1 is a d x h matrix connecting inputs to h hidden neurons.
  • b1 is a length-h hidden bias vector.
  • W2 is a length-h vector connecting the hidden layer to the output.
  • b2 is the output bias.
  • epochs is a non-negative integer. learning_rate is positive.
  • Train for exactly epochs batch updates and return a list of n predictions, where each prediction is 1 when the final output probability is at least 0.5, otherwise 0.

Use sigmoid activation in both layers. Average gradients across the batch before updating parameters.

Constraints

  • 1 <= len(X) <= 200
  • 1 <= len(X[0]) <= 20
  • 1 <= len(b1) <= 20
  • len(W1) == len(X[0])
  • Each row of W1 has len(b1) elements
  • len(W2) == len(b1)
  • len(y) == len(X)
  • 0 <= epochs <= 2,000
  • 0 < learning_rate <= 1
  • Every label is either 0 or 1

Function Signature

def train_network(X, y, W1, b1, W2, b2, epochs, learning_rate):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output