Prepin
Log in

Are you able to code batch normalization using NumPy?

Approach / Explanation

The interviewer is inquiring whether you can create a function for batch normalization utilizing the NumPy library. Batch normalization is a technique designed to accelerate the learning process of deep neural networks. It works by normalizing the activations of each layer, which are organized into mini-batches, to mitigate internal covariate shift. Here’s a breakdown of the task: Comprehend the batch normalization algorithm: Essentially, it is a method to enhance the performance and stability of neural networks by normalizing inputs at each layer. Are you capable of coding this algorithm with NumPy: NumPy is a Python library that offers support for arrays and matrices, along with a variety of mathematical functions for manipulating these elements. The essential points for you to consider when responding to this question are: Are you knowledgeable about the batch normalization process in machine learning? Do you understand how to implement this process using the NumPy library in Python?

Suggested Answer

Certainly! I can execute batch normalization using Numpy. Essentially, batch normalization normalizes the inputs across each batch by applying a transformation that preserves the zero-mean and unit-variance characteristics. Below is a simple implementation of this: import numpy as np def batch_norm ( X , gamma , beta , eps = 1e-8 ) : mean = np . mean ( X , axis = 0 ) var = np . var ( X , axis = 0 ) X_norm = ( X - mean ) / np . sqrt ( var + eps ) out = gamma * X_norm + beta return out In this context, 'X' represents the input data to be normalized, 'gamma' and 'beta' are the scaling and shifting parameters, respectively, while 'eps' is a small constant introduced to ensure stability during the division process.

Alternative Answer

Indeed, implementing batch normalization in Numpy is quite simple. Below is an alternative implementation, using 'X' to represent the batch of input features: import numpy as np def batch_norm ( X , eps = 1e-8 ) : if len ( X . shape ) not in ( 2 , 4 ) : raise ValueError ( "Input array should be 2D or 4D" ) if len ( X . shape ) == 2 : mean = np . mean ( X , axis = 0 ) std_dev = np . std ( X , axis = 0 ) else : mean = np . mean ( X , axis = ( 0 , 2 , 3 ) , keepdims = True ) std_dev = np . std ( X , axis = ( 0 , 2 , 3 ) , keepdims = True ) X_normalized = ( X - mean ) / ( std_dev + eps ) return X_normalized In this example, 'eps' is included to prevent division by zero errors. The function initially verifies whether the input array is either 2D or 4D, which are standard dimensions in neural network layers (2D for fully connected layers and 4D for convolutional layers). For a 4D array, the mean and standard deviation are computed across the width and height of the feature map, while preserving the batch and feature dimensions. The normalized output retains the same shape as the input data.

Question Details

Difficulty & Category

Medium
technical

Companies

Apple

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.