I'm implementing a computation using numpy zeros and numpy.random.randn
W1 = np.random.randn(n_h, n_x) * .01
b1 = np.zeros((n_h, 1))
I'm not sure why random.randn() can accept two integers while zeros() needs a tuple. Is there a good reason for that?
Cheers, JChen.
randn generates samples from the normal distribution, while numpy. random. rand from a uniform distribution (in the range [0,1)).
NumPy random. randn() function in Python is used to return random values from the normal distribution in a specified shape. This function creates an array of the given shape and it fills with random samples from the normal standard distribution.
random. random() is one of the function for doing random sampling in numpy. It returns an array of specified shape and fills it with random floats in the half-open interval [0.0, 1.0).
The numpy random seed is a numerical value that generates a new set or repeats pseudo-random numbers. The value in the numpy random seed saves the state of randomness. If we call the seed function using value 1 multiple times, the computer displays the same random numbers.
Most likely it's just a matter of history. numpy
results from the merger of several prior packages, and has a long development. Some quirks get cleaned up, others left as is.
randn(d0, d1, ..., dn)
zeros(shape, dtype=float, order='C')
randn
has this note:
This is a convenience function. If you want an interface that takes a tuple as the first argument, use
numpy.random.standard_normal
instead.
standard_normal(size=None)
With *
it is easy to pass a tuple to randn
:
np.random.randn(*(1,2,3))
np.zeros
takes a couple of keyword arguments. randn
does not. You can define a Python function with a (*args, **kwargs)
signature. But accepting a tuple, especially one with a common usage as shape
, fits better. But that's a matter of opinion.
np.random.rand
and np.random.random_sample
are another such pair. Most likely rand
and randn
are the older versions, and standard_normal
and random_sample
are newer ones designed to conform to the more common tuple
style.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With