簡體   English   中英

使用 numpy.random.randn() 在 numpy 中初始化隨機權重矩陣時出現“TypeError: an integer is required”

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

所以我使用 numpy 從矩陣構建神經網絡,我有以下初始化代碼:

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)

所有變量都是預定義的;

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

但是,當我部署它時,出現以下錯誤:

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

我已嘗試搜索此錯誤,但無法獲得任何相關匹配項。 知道出了什么問題嗎? 我也嘗試過使用 np.random.normal(scale=weigh_tscale, size=(input_dim, hidden_​​dims)) 並且我收到了相同的

'TypeError: an integer is required'

提前感謝您提供任何線索!


更新:這是使用 python2,而不是 3

您將range拼錯為xrange 這將解決您的問題。

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)

否則, np.random.randn(input_dim, hidden_dims)不應像np.random.randn((input_dim, hidden_dims))那樣是雙括號中的度量大小

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM