Skip to content

Commit affbb03

Browse files
committed
Fixing possible log of zero error
1 parent 074671c commit affbb03

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

code/network2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ class CrossEntropyCost:
4343
@staticmethod
4444
def fn(a, y):
4545
"""Return the cost associated with an output ``a`` and desired output
46-
``y``.
46+
``y``. Note that the np.nan_to_num ensures that if the output
47+
from the network is exactly right, then 0.0 will be returned,
48+
rather than nan.
4749
4850
"""
49-
return np.sum(-y*np.log(a)-(1-y)*np.log(1-a))
51+
return np.nan_to_num(np.sum(-y*np.log(a)-(1-y)*np.log(1-a)))
5052

5153
@staticmethod
5254
def delta(z, a, y):

0 commit comments

Comments
 (0)