python - 使用 Python 实现神经网络的成本函数(第 5 周 Coursera)

标签 python numpy machine-learning

基于 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,供引用:


enter image description here

最佳答案

问题是您使用了错误的类标签。在计算成本函数时,您需要使用 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/

相关文章:

python - 将文件长时间保留在内存中

python - Pandas ,Numpy : How to Speed up row iteration with inner loop?

c# - 如何在 C# 中将 DataTable 转换为锯齿状数组?

python - sklearn preprocessing.scale() 函数,什么时候使用它?

machine-learning - 定义文本分类中的词汇量

python - 对 Python 字典列表进行分组

python - 如何对 df1 中与 df2 中的另一个值匹配的值求和

python - 如何启动 EC2 实例并按需运行任务

python如何替换数组(x,n)中每个x的第一个f(x)值

machine-learning - 从哪里可以找到针对非技术人员的所有人工智能算法的简要介绍