在计算机学习课程的基础上,我尝试在python中实现神经网络的成本函数。有一个类似于这个的question --有一个被接受的答案--但是答案中的代码是用八度写的。为了不懒惰,我尝试将答案的相关概念适应于我的情况,据我所知,我正在正确地实现这个函数。然而,我输出的成本与预期成本不同,所以我做错了一些事情。
下面是一个可复制的小例子:
下面的链接将导致一个.npz文件,该文件可以加载(如下所示)以获得相关数据。如果您使用"arrays.npz"文件,请重命名它。
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。
这是上述问题中公布的费用职能,以供参考:

发布于 2016-07-29 00:28:45
问题在于您使用的是错误的类标签。在计算成本函数时,您需要使用基本真理或真正的类标签。
我不知道你的新数组是什么,但不是训练输出。因此,我更改了您的代码,使用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归零。
发布于 2018-05-14 09:27:11
您可以尝试此实现。
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)) It输出
0.3837698590909236https://stackoverflow.com/questions/38646500
复制相似问题