我想我正在按照与帖子中提到的相同的在线教程: How to convert deep learning gradient descent equation into python
我知道我们必须计算成本和数据库,但我的问题是为什么他们在这两个方程中都将axis = 0?换句话说,我不明白轴= 0,它在这个计算中使用了什么。如果在没有轴= 0
的情况下进行计算,结果会是什么?import numpy as np
cost = -1*((np.sum(np.dot(Y,np.log(A))+np.dot((1-Y),(np.log(1-A))),axis=0))/m)
db = np.sum((A-Y),axis=0)/m
答案 0 :(得分:0)
这是一个问题类型的示例,您可以自己在解释器中尝试在相同或更短的时间内理解它,以构建此问题。
另一种方法是查看文档。在这里查阅文档总是一个好习惯。可以找到np.sum()
上的文档here
如果您仍然感到懒惰,请从文档中摘录一些:
...
axis : None or int or tuple of ints, optional
Axis or axes along which a sum is performed. The default, axis=None,
will sum all of the elements of the input array. If axis is negative it
counts from the last to the first axis.
...
文档中的一些示例:
>>> np.sum([0.5, 1.5])
2.0
>>> np.sum([[0, 1], [0, 5]])
6
>>> np.sum([[0, 1], [0, 5]], axis=0)
array([0, 6])
>>> np.sum([[0, 1], [0, 5]], axis=1)
array([1, 5])
可视化
-----> axis = 1
| [[0, 1
| [0, 5]]
v
axis = 0
答案 1 :(得分:0)
为了清楚起见:在许多深度学习框架中,所有参数都被视为张量,因此标量被简单地视为0阶张量(大小为1x1)。如果你执行np.sum()
,则展平张量并总结所有组件以产生标量(不是张量)。通过显式使用axis=1
,您可以创建第0个订单张量(在您的情况下)。我不知道你在问题中链接的代码是否需要这个,但我可以想象这在一些深度学习框架中起作用。
这是一个简单的例子,说明了我的观点:
import numpy as np
x = np.ones((1, 10))
no_ax = np.sum(x)
ax0 = np.sum(x, axis=0)
ax1 = np.sum(x, axis=1)
print(no_ax, ax0, ax1)
结果:
(10.0, array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]), array([10.]))