1

# Neural Network Weight Initialization

Unsolved
###### Neural Networks

Difficulty: 4 | Problem written by hemdan219@gmail.com
##### Problem reported in interviews at

You are given the number of nodes in input layer x_nodes, a list of the number of nodes in each of two hidden layers h_nodes, and finally the number of nodes in the output layer y_nodes.

Your task is to initialize weight matrices for the three layers depending on the nodes of previous layer and the nodes of current layer. Don't worry about initializing a bias vector. The size of the 3 weight matrices in this small neural network is given by the following formula, where $$n^{[l]}$$ is the number of nodes in layer $$l$$.

$$Size \,of \,{w^{[l]}}={({n^{[l]}},{n^{[l-1]}})}$$,

Use np.random.randn to initialize the weights.

Return a tuple containing the three weight matrices W1, W2, and W3.

Use np.random.seed(3) to initialize the random number generator to get the same expected output as our test cases.

##### Sample Input:
<class 'int'>
x_nodes: 3
<class 'list'>
 h_nodes: [7, 4]
<class 'int'>
 y_nodes: 1

##### Expected Output:
<class 'tuple'>
(array([[ 1.78862847, 0.43650985, 0.09649747], [-1.8634927 , -0.2773882 , -0.35475898], [-0.08274148, -0.62700068, -0.04381817], [-0.47721803, -1.31386475, 0.88462238], [ 0.88131804, 1.70957306, 0.05003364], [-0.40467741, -0.54535995, -1.54647732], [ 0.98236743, -1.10106763, -1.18504653]]), array([[-0.2056499 , 1.48614836, 0.23671627, -1.02378514, -0.7129932 , 0.62524497, -0.16051336], [-0.76883635, -0.23003072, 0.74505627, 1.97611078, -1.24412333, -0.62641691, -0.80376609], [-2.41908317, -0.92379202, -1.02387576, 1.12397796, -0.13191423, -1.62328545, 0.64667545], [-0.35627076, -1.74314104, -0.59664964, -0.58859438, -0.8738823 , 0.02971382, -2.24825777]]), array([[-0.26776186, 1.01318344, 0.85279784, 1.1081875 ]]))

MLPro Premium also allows you to access all our high quality MCQs which are not available on the free tier.

Not able to solve a problem? MLPro premium brings you access to solutions for all problems available on MLPro

Have an issue, the MLPro support team is available 24X7 to Premium users.

##### This is a premium feature. To access this and other such features, click on upgrade below.

uozcan12 • 5 months, 3 weeks ago

0

I did this but it did not pass all test cases. What should I do?


#!/usr/bin/python3

import numpy as np

# Please do not change the below function name and parameters
def initial_weights(x_nodes, h_nodes, y_nodes):
layers_dims = []
layers_dims.insert(0,x_nodes)
layers_dims += h_nodes
layers_dims.insert(len(layers_dims),y_nodes)

np.random.seed(3) # This seed makes sure your "random" numbers will be the as ours
parameters = {}
L = len(layers_dims) # integer representing the number of layers
for l in range(1, L):
parameters['W' + str(l)] = np.random.randn(layers_dims[l], layers_dims[l-1])

return tuple(parameters.values())