我已经在Python中实现了交叉熵及其渐变,但是我不确定它是否正确。我也想寻求一个好的解决方案来避免np.log(0)。我的实现是针对神经网络
yEst = np.array([1, 6, 3, 5]).T # output of a softmax function in last layer
y = np.array([0, 6, 3, 0]).T
def crossEntropy(y, yEst):
return - np.sum(y*np.log(yEst),axis =0)
def crossEntropyDerivative(y, yEst):
m = y.shape[0]
c = np.sum(yEst - y, axis = 0)
return c/m
如何避免log(0)? 渐变正确吗?