基于 Coursera 机器学习类(class),我正在尝试在 Python 中实现神经网络的成本函数。有一个question类似于这个——有一个可接受的答案——但答案中的代码是用 Octave 写的。不要偷懒,我已经尝试根据我的情况调整答案的相关概念,据我所知,我正在正确实现该功能。然而,我输出的成本与预期成本不同,所以我做错了什么。
这是一个可重现的小例子:
以下链接指向一个.npz
文件,可以加载(如下所示)以获取相关数据。重命名文件“arrays.npz”
,如果你使用它。
http://www.filedropper.com/arrays_1
if __name__ == "__main__":
with np.load("arrays.npz") as data:
thrLayer = data['thrLayer'] # The final layer post activation; you
# can derive this final layer, if verification needed, using weights below
thetaO = data['thetaO'] # The weight array between layers 1 and 2
thetaT = data['thetaT'] # The weight array between layers 2 and 3
Ynew = data['Ynew'] # The output array with a 1 in position i and 0s elsewhere
#class i is the class that the data described by X[i,:] belongs to
X = data['X'] #Raw data with 1s appended to the first column
Y = data['Y'] #One dimensional column vector; entry i contains the class of entry i
import numpy as np
m = len(thrLayer)
k = thrLayer.shape[1]
cost = 0
for i in range(m):
for j in range(k):
cost += -Ynew[i,j]*np.log(thrLayer[i,j]) - (1 - Ynew[i,j])*np.log(1 - thrLayer[i,j])
print(cost)
cost /= m
'''
Regularized Cost Component
'''
regCost = 0
for i in range(len(thetaO)):
for j in range(1,len(thetaO[0])):
regCost += thetaO[i,j]**2
for i in range(len(thetaT)):
for j in range(1,len(thetaT[0])):
regCost += thetaT[i,j]**2
regCost *= lam/(2*m)
print(cost)
print(regCost)
实际上,cost
应该是 0.287629,cost + newCost
应该是 0.383770。
这是上面问题贴出来的cost function,供引用:
最佳答案
问题是您使用了错误的类标签。在计算成本函数时,您需要使用 ground truth,即真正的类标签。
我不确定你的 Ynew 阵列是什么,但它不是训练输出。因此,我更改了您的代码以使用 Y 代替 Ynew 作为类标签,并获得了正确的成本。
import numpy as np
with np.load("arrays.npz") as data:
thrLayer = data['thrLayer'] # The final layer post activation; you
# can derive this final layer, if verification needed, using weights below
thetaO = data['thetaO'] # The weight array between layers 1 and 2
thetaT = data['thetaT'] # The weight array between layers 2 and 3
Ynew = data['Ynew'] # The output array with a 1 in position i and 0s elsewhere
#class i is the class that the data described by X[i,:] belongs to
X = data['X'] #Raw data with 1s appended to the first column
Y = data['Y'] #One dimensional column vector; entry i contains the class of entry i
m = len(thrLayer)
k = thrLayer.shape[1]
cost = 0
Y_arr = np.zeros(Ynew.shape)
for i in xrange(m):
Y_arr[i,int(Y[i,0])-1] = 1
for i in range(m):
for j in range(k):
cost += -Y_arr[i,j]*np.log(thrLayer[i,j]) - (1 - Y_arr[i,j])*np.log(1 - thrLayer[i,j])
cost /= m
'''
Regularized Cost Component
'''
regCost = 0
for i in range(len(thetaO)):
for j in range(1,len(thetaO[0])):
regCost += thetaO[i,j]**2
for i in range(len(thetaT)):
for j in range(1,len(thetaT[0])):
regCost += thetaT[i,j]**2
lam=1
regCost *= lam/(2.*m)
print(cost)
print(cost + regCost)
这个输出:
0.287629165161
0.383769859091
编辑:修复了 regCost *= lam/(2*m)
的整数除法错误,该错误将 regCost 归零。
关于python - 使用 Python 实现神经网络的成本函数(第 5 周 Coursera),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38646500/