Write Python code to implement neural network backpropagation
Filed under TCS
Free read 3 of 3Compute weighted sums and apply activation functions (e.g., Sigmoid) to get predictions.
The question
Implement the backpropagation algorithm for a simple neural network in Python.
Constraints
- Use standard Python libraries only
- Implement forward pass and gradient calculation
- Assume a simple 2-layer network
Approach
Compute weighted sums and apply activation functions (e.g., Sigmoid) to get predictions.
Calculate the difference between predicted and actual values (Loss function).
Compute gradients of the loss with respect to weights using the chain rule.
Tips
- What they are testing: Verify gradients against numerical differentiation.
- Common mistake: Forgetting the chain rule or incorrectly applying the derivative of the activation function.
Related TCS questions
Collected from public candidate posts and coding forums. Updated Nov 2026.