基于机器学习课程,我试图在python中实现神经网络的成本函数。有一个question与此类似 - 有一个接受的答案 - 但答案中的代码是用八度写的。不要懒惰,我试图根据我的情况调整答案的相关概念,据我所知,我正确地实现了这个功能。然而,我输出的成本与预期成本不同,所以我做错了。
这是一个可重复的小例子:
以下链接指向.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。
这是上述问题中公布的成本函数,供参考:
答案 0 :(得分:2)
问题是您使用的是错误的类标签。计算成本函数时,您需要使用基础事实或真正的类标签。
我不确定你的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归零。
答案 1 :(得分:0)
您可以尝试此实现
import scipy.io
mat=scipy.io.loadmat('ex4data1.mat')
X=mat['X']
y=mat['y']
theta=scipy.io.loadmat('ex4weights.mat')
theta1=theta['Theta1']
theta2=theta['Theta2']
theta=[theta1,theta2]
new=np.zeros((10,len(y)))
for i in range(len(y)):
new[y[i]-1,i]=1
y=new
def sigmoid(x):
return 1/(1+np.exp(-x))
def reg_cost(theta,X,y,lambda1):
current=X
for i in range(len(theta)):
a= np.append(np.ones((len(current),1)),current,axis=1)
z=np.matmul(a,theta[i].T)
z=sigmoid(z)
current=z
htheta=current
ans=np.sum(np.multiply(np.log(htheta),(y).T)) +
np.sum(np.multiply(np.log(1-htheta),(1-y).T))
ans=-ans/len(X)
for i in range(len(theta)):
new=theta[i][:,1:]
newsum=np.sum(np.multiply(new,new))
ans+=newsum*(lambda1)/(2*len(X))
return ans
print(reg_cost(theta,X,y,1))
输出
0.3837698590909236