gpt4 book ai didi

python - '类型错误 : an integer is required' when initializing random weight matrix in numpy with numpy. random.randn()

转载 作者:行者123 更新时间:2023-12-03 17:17:24 26 4
gpt4 key购买 nike

所以我使用 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

最佳答案

您拼错了 rangexrange .这将解决您的问题。

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

关于python - '类型错误 : an integer is required' when initializing random weight matrix in numpy with numpy. random.randn(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39351374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com