File tree Expand file tree Collapse file tree 1 file changed +10
-2
lines changed
Expand file tree Collapse file tree 1 file changed +10
-2
lines changed Original file line number Diff line number Diff 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
179187def sigmoid (z ):
180188 """The sigmoid function. Note that it checks to see whether ``z``
You can’t perform that action at this time.
0 commit comments