Mun*_*uno 5 python numpy machine-learning
基于 Coursera 机器学习课程,我正在尝试在 python 中实现神经网络的成本函数。有一个与此类似的问题- 有一个可接受的答案 - 但该答案中的代码是用八度写的。不要偷懒,我已经尝试将答案的相关概念适应我的案例,据我所知,我正在正确实现该功能。然而,我输出的成本与预期成本不同,所以我做错了。
这是一个可重现的小示例:
以下链接指向一个.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)
Run Code Online (Sandbox Code Playgroud)
实际上,cost
应该是 0.287629,cost + newCost
应该是 0.383770。
这是上面问题中贴出的代价函数,供参考:
问题是您使用了错误的类标签。计算成本函数时,您需要使用真实值或真实的类标签。
我不确定你的 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)
Run Code Online (Sandbox Code Playgroud)
这输出:
0.287629165161
0.383769859091
Run Code Online (Sandbox Code Playgroud)
编辑:修复了整数除法错误,regCost *= lam/(2*m)
该错误将 regCost 归零。