Step 2:
clear;W1=rand(4,2); %assign random weights to connections between input and hidden layers
W2=rand(1,4); %assign random weights to connections between hidden and output layers
If you take a look at the picture of the network, you'll see that there are 8 connections between the input and
hidden
layer nodes, 4 per node. That's why the weight matrix is a 4 by 2.
Same for W2: four connections between the nodes of the
hidden layer and the output layer.
In assigning the random numbers why do we use the rand(4,2) and rand(1,4) format? Why not rand(2,4) and rand(4,1)? Because the number of nodes in a given network layer are represented by the number of columns of the input matrix to that layer. So if you look at X (above) you'll notice that it has two columns, each of which is associated with one input node. Also notice that the hidden layer has 4 nodes, so W2 must have 4 columns.