Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'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

like image 785
DaveTheAl Avatar asked Nov 08 '22 09:11

DaveTheAl


1 Answers

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))

like image 150
Guru Bhandari Avatar answered Nov 14 '22 22:11

Guru Bhandari