我想将多维数组传递到relu prime函数中。
def reluprime(x):
if x > 0:
return 1
else:
return 0..。其中x是整个数组。它回来了
ValueError:包含多个元素的数组的真值是不明确的。使用a.any()或a.all()
我在普通的relu函数中遇到了这个问题,而不是使用python函数max(),我使用了np.max(),它起了作用。但有了relu质数,两者都不起作用。我试过:
def reluprime(x):
if np.greater(x, 0):
return 1
else:
return 0..。它仍然返回相同的错误。我怎么才能解决这个问题?谢谢。
发布于 2017-07-10 21:36:48
if语句没有意义,因为它只对整个数组计算一次。如果要对数组中的每个元素使用等效的If语句,则应该执行以下操作:
def reluprime(x):
return np.where(x > 0, 1.0, 0.0)发布于 2017-07-10 21:37:44
如果向量中的条目大于0和0,则relu素数返回1,因此只需执行以下操作:
def reluprime(x):
return (x>0).astype(x.dtype)在上面的代码中,输入数组x被假定为numpy数组。例如,reluprime(np.array([-1,1,2]))返回array([0, 1, 1])。
发布于 2017-07-10 21:46:14
"relu素数“,即ReLU函数的梯度,被称为"heaviside阶跃函数”。
Numpy 1.13为此引入了一个ufunc:
def reluprime(x):
return np.heaviside(x, 0)
# second value is value at x == 0
# note that ReLU is not differentiable at x==0, so there is no right value to
# pass here我的机器上的计时结果显示,这一性能很差,表明需要做更多的工作:
In [1]: x = np.random.randn(100000)
In [2]: %timeit np.heaviside(x, 0) #mine
1.31 ms ± 58.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [3]: %timeit np.where(x > 0, 1.0, 0.0) # Jonas Adler's
658 µs ± 74.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [4]: %timeit (x>0).astype(x.dtype) # Miriam Farber's
172 µs ± 34.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)https://stackoverflow.com/questions/45021963
复制相似问题