简体   繁体   中英

'TypeError: an integer is required' when initializing random weight matrix in numpy with numpy.random.randn()

So I am using numpy to build a neural net from matrices, and I have the following code for initialization:

for i in xrange(self.num_layers-1):
    self.params['W%d' % i] = np.random.randn(input_dim, hidden_dims) * weight_scale
    self.params['b%d' % i] = np.zeros(hidden_dims)

All variables are predefined;

type(input_dim) == integer
type(hidden_dims) == integer
type(self.num_layers) == integer
weight_scale == 1e-3

However, when I deploy this, I get the following error:

Traceback (most recent call last):
  File "...", line 201, in __init__
self.params['W%d' % i] = np.random.randn(input_dim, hidden_dims) * weight_scale
  File "mtrand.pyx", line 1680, in mtrand.RandomState.randn (numpy/random/mtrand/mtrand.c:17783)
  File "mtrand.pyx", line 1810, in mtrand.RandomState.standard_normal (numpy/random/mtrand/mtrand.c:18250)
  File "mtrand.pyx", line 163, in mtrand.cont0_array (numpy/random/mtrand/mtrand.c:2196)
TypeError: an integer is required

I have tried searching for this error but cannot get any relevant matches. Any idea what could have gone wrong? I have also tried using np.random.normal(scale=weigh_tscale, size=(input_dim, hidden_dims)) and I have received the same

'TypeError: an integer is required'

Thank you in advance for any clues!


Update: This was using python2, not 3

You have misspelled range to xrange . This will solve your problem.

for i in range(self.num_layers-1):
    self.params['W%d' % i] = np.random.randn(input_dim, hidden_dims) * weight_scale
    self.params['b%d' % i] = np.zeros(hidden_dims)

Otherwise, np.random.randn(input_dim, hidden_dims) should not be the metrics size in double braces like np.random.randn((input_dim, hidden_dims))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM