numpy np.max()错误:TypeError:只有整数标量数组可以转换为标量索引

6l7fqoea  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(104)

我试图让np.max()函数像relu()函数一样工作,但总是得到这个错误:

>>>np.max(0, np.arange(-5, 5))

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [91], in <cell line: 1>()
----> 1 np.max(0, np.arange(-5, 5))

File <__array_function__ internals>:5, in amax(*args, **kwargs)

File ~/opt/anaconda3/envs/coherence/lib/python3.10/site-packages/numpy/core/fromnumeric.py:2754, in amax(a, axis, out, keepdims, initial, where)
   2638 @array_function_dispatch(_amax_dispatcher)
   2639 def amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,
   2640          where=np._NoValue):
   2641     """
   2642     Return the maximum of an array or maximum along an axis.
   2643 
   (...)
   2752     5
   2753     """
-> 2754     return _wrapreduction(a, np.maximum, 'max', axis, None, out,
   2755                           keepdims=keepdims, initial=initial, where=where)

File ~/opt/anaconda3/envs/coherence/lib/python3.10/site-packages/numpy/core/fromnumeric.py:86, in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     83         else:
     84             return reduction(axis=axis, out=out, **passkwargs)
---> 86 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

TypeError: only integer scalar arrays can be converted to a scalar index

字符串
我希望它输出[0,0,0,0,0,0,1,2,3,4]

voase2hg

voase2hg1#

有两个名称相似的numpy函数,它们做的事情非常不同:

  • np.max(),用来求数组沿沿着特定轴的最大值,如果你想求向量中最大的元素,你可以用这个。
  • np.maximum(),它会进行逐个元素的最大值比较,如果需要的话会进行广播。所以,如果你想实现ReLU,你可以使用这个。

如何实现ReLU:

>>> np.maximum(0, np.arange(-5, 5))
array([0, 0, 0, 0, 0, 0, 1, 2, 3, 4])

字符串

相关问题