我使用 softmax 函数从神经网络获取输出,并在计算误差时获取最小值作为输出。
但是,如果假设 [0,0,0] 输出全部相同,则 softmax 函数的输出为 [0.33,0.33,0.33]
因此,当从中选择最小值时,
output = softmax(np.dot(hs,HO))
tarminout = np.subtract(target,output)
mine = min(tarminout)
mine = 0.5 * np.power(mine,2)
finalError += mine
由于存在多个相等的最小值,因此会出现以下错误,
Traceback (most recent call last):
File "ann.py", line 234, in modulelearn()
File "ann.py", line 97, in learnmine = min(tarminout)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
当存在超过 1 个相等的最小值时,如何通过仅选择其中一个来通过此操作?
谢谢
最佳答案
答案隐藏在上面的注释中:您的错误可能是将多维 ndarray 传递给标准 python min() 的结果,而标准 python min() 无法理解它们。
方法#1:调用 np.min 而不是 min
方法#2(不推荐):展平数组,min(tarminout.ravel())
首选方式#1,在 numpy 数组上使用 numpy 运算符
关于python - Numpy Softmax - 具有多个元素的数组的真值是不明确的。使用 a.any() 或 a.all(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38991216/