Skip to content

Commit e1257b2

Browse files
committed
Fixing up the function to find the minimal cross entropy
1 parent 1682825 commit e1257b2

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

code/backprop2.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,16 @@ def minimal_cross_entropy(training_data):
173173
Return the minimal output cross entropy for training data
174174
consisting of a list of values ``(x, y)``. This is just
175175
-sum (y ln(y) + (1-y) ln(1-y))."""
176-
y = np.array([yj for (_, yj) in training_data])
177-
return np.sum(-y*np.log(y)-(1-y)*np.log(1-y))
176+
def rescale(y):
177+
"""
178+
Rescale the vectors very slightly to ensure there are no
179+
problems with taking logarithms of 0. This might cause a
180+
problem if there are a huge number of features (10**7 & up),
181+
in which case this code will need to be modified."""
182+
return 10**(-9)+(1-2*10**(-9))*y
183+
rescaled_training_data = [rescale(y) for (_, y) in training_data]
184+
return np.sum(-np.sum(y*np.log(y)+(1-y)*np.log(1-y))
185+
for y in rescaled_training_data)
178186

179187
def sigmoid(z):
180188
"""The sigmoid function. Note that it checks to see whether ``z``

0 commit comments

Comments
 (0)