2017-02-13 65 views
2

我有這樣的代碼Python的 - 值過多解壓

N, D = x.shape 
running_mean = bn_param.get('running_mean', np.zeros(D, dtype=x.dtype)) 
running_var = bn_param.get('running_var', np.zeros(D, dtype=x.dtype)) 

和它說:ValueError異常:值過多解壓

我知道x具有50x3x32x32尺寸 我希望把在N變量中的50,我需要把3x32x32放在D變量中。 我怎麼能這樣做?謝謝。

回答

4

x.shape有2倍以上的值,不變量來解包(多任務)的數量相匹配:

>>> shape = (50, 3, 32, 32) 
>>> N, D = shape 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ValueError: too many values to unpack 

您可以使用索引,切片運營商獲得你想要的東西:

>>> shape = (50, 3, 32, 32) 
>>> N, D = shape[0], shape[1:] # [0] to get 1st, [1:] to get 2nd, 3rd,.. up to end 
>>> N 
50 
>>> D 
(3, 32, 32) 

如果您使用的是Python 3.x,您可以使用Extended iterable unpacking syntax

>>> N, *D = shape 
>>> N 
50 
>>> D 
[3, 32, 32]