Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown * in numpy.random function [duplicate]

Tags:

python

numpy

I was studying this link and this is the code .

 U1 = np.random.rand(*H1.shape) < p # first dropout mask

Why does it fail when I try to do this?

import numpy
numpy.random.rand(*1) < 2 

I understand that the rand() function takes in a number which is why I am confused that the code is supposed to work.

like image 427
aceminer Avatar asked Oct 16 '25 04:10

aceminer


1 Answers

The * unpacks a tuple into multiple input arguments. The code is creating a random matrix the same shape as H1 using the shape attribute (which is a tuple) as the dimension inputs to np.random.rand.

You can do this with any tuple

np.random.rand(*(2,3))     # The same as np.random.rand(2,3)
# Creates a 2 x 3 array

You are trying to unpack an integer which is going to fail.

like image 153
Suever Avatar answered Oct 17 '25 18:10

Suever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!