【问题标题】:zero-size array to reduction operation maximum which has no identity零大小数组到归约操作最大值,没有标识
【发布时间】:2017-06-10 09:37:28
【问题描述】:

这是一个可运行的 sn-p 代码,它会在我的机器上生成错误。 我有这个错误(零大小数组到没有标识的最大缩减操作)。

reader = csv.reader(open(BASE_PATH + 'labels.csv'))
# skip the header
next(reader)

X = []
y = []

for row in reader:
    label = row[2]
    if len(label) > 0 and label.find(',') == -1:
        filename = get_filename_for_index(row[1])
        img_file = cv2.imread(BASE_PATH + filename)
        if img_file is not None:
            img_file = scipy.misc.imresize(arr=img_file, size=(120, 160, 3))
            img_arr = np.asarray(img_file)
            img_arr = apply_color_mask(img_arr)
            X.append(img_arr)
            y.append(label)


X = np.asarray(X)
y = np.asarray(y)

encoder = LabelEncoder()
encoder.fit(y)
encoded_y = encoder.transform(y)

y = np_utils.to_categorical(encoded_y)


**Error**
ValueError                                Traceback (most recent call last)
<ipython-input-7-49c49b99292b> in <module>()
     26 encoded_y = encoder.transform(y)
     27 
---> 28 y = np_utils.to_categorical(encoded_y)

~/.local/lib/python3.5/site-packages/keras/utils/np_utils.py in to_categorical(y, num_classes)
     20     y = np.array(y, dtype='int').ravel()
     21     if not num_classes:
---> 22         num_classes = np.max(y) + 1
     23     n = y.shape[0]
     24     categorical = np.zeros((n, num_classes))

~/.local/lib/python3.5/site-packages/numpy/core/fromnumeric.py in amax(a, axis, out, keepdims)
   2270 
   2271     return _methods._amax(a, axis=axis,
-> 2272                           out=out, **kwargs)
   2273 
   2274 

~/.local/lib/python3.5/site-packages/numpy/core/_methods.py in _amax(a, axis, out, keepdims)
     24 # small reductions
     25 def _amax(a, axis=None, out=None, keepdims=False):
---> 26     return umr_maximum(a, axis, None, out, keepdims)
     27 
     28 def _amin(a, axis=None, out=None, keepdims=False):

值错误:零大小数组到没有标识的归约操作最大值

所以请任何人都可以帮助我。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    您可以尝试将失败的操作放在某种保护中:

    if np.any(encoded_y):
        y = np_utils.to_categorical(encoded_y)
    

    【讨论】:

      猜你喜欢
      • 2018-01-14
      • 2022-10-20
      • 2020-12-10
      • 2013-12-27
      • 2019-09-02
      • 2020-12-06
      • 2020-04-16
      • 2020-05-05
      相关资源
      最近更新 更多