Based on a Coursera machine learning course, I am trying to implement a cost function for a neural network in python. There is a question similar to this one with the accepted answer, but the code in these answers is written in an octave. In order not to be lazy, I tried to adapt the corresponding concepts of the answer to my case, and, as far as I can tell, I correctly implement this function. However, the cost of I output is different from the expected value, so I'm doing something wrong.
Here is a small reproducible example:
The following link leads to a file .npzthat can be downloaded (as shown below) to obtain the corresponding data. Rename the file "arrays.npz", please, if you use it.
http://www.filedropper.com/arrays_1
if __name__ == "__main__":
with np.load("arrays.npz") as data:
thrLayer = data['thrLayer']
thetaO = data['thetaO']
thetaT = data['thetaT']
Ynew = data['Ynew']
X = data['X']
Y = data['Y']
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)
In fact, it costshould be 0.287629, and it cost + newCostshould be 0.383770.
This is the cost function posted in the question above for reference:

source
share