I am currently looking into an example implementation of Random Search from Machine Learning Refined and have trouble understanding the following code snippet where w is an input vector and num_samples is number of random directions to sample:
# construct set of random unit directions
directions = np.random.randn(num_samples,np.size(w))
norms = np.sqrt(np.sum(directions*directions,axis = 1))[:,np.newaxis]
directions = directions/norms
What I do understand is that we sample random vectors in the size of w, which we then need to normalize in order to get unit directions. So we calculate |V| = sqrt(x*x + y*y + z*z)
for all directions or V/|V| = (x/|V|, y/|V|, z/|V|)
respectively.
Could someone explain to me in simple terms what happens in line 3, specifically why we sum over axis=1 and what the [:,np.newaxis]
does? I have a vague grasp on what this is supposed to do, but some intuition would help a lot.