-1

I'm new to using the math library in Python. The purpose of this script is to show my "working out" of Cross-Entropy error function.

I have checked my parentheses and operators and cannot see anything wrong, with my noob eyes.

The error occurs on the last line.

out = 1.099
target = 0.7
do = 3

print('CEE = -(log(' + str(out) + ') + ((1 - ' + str(target) + ') * log(1 - ' + str(out) + ')))')
print('    = -(log(' + str(out) + ') + ((' + str(round(1 - target, dp)) + ') * log(' + str(round(1 - out, dp)) + ')))')
print('    = -(' + str(round(m.log(out), dp)) + ' + ((' + str(round(1 - target, dp)) + ') * ' + str(round(m.log(1 - out), dp)) + '))')

Output & Error:

CEE = -(log(1.099) + ((1 - 0.7) * log(1 - 1.099)))
    = -(log(1.099) + ((0.3) * log(-0.099)))

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    import Perceptron
  File "/home/runner/Deep-Learning/Perceptron.py", line 125, in <module>
    print('    = -(' + str(round(m.log(out), dp)) + ' + ((' + str(round(1 - target, dp)) + ') * ' + str(round(m.log(1 - out), dp)) + '))')
ValueError: math domain error
  • If you broke up the last line into simple statements, e.g. `m.log(out); m.log(1-target)`, etc, and build up more nested calls as things pass the test, you'd very soon discover you're trying to calculate the logarithm of a negative number. – Reti43 May 14 '21 at 16:56
  • here is the issue `round(m.log(1 - out), dp) # are you sure you need a log of negative 0.09899999999999998 ?` – simpleApp May 14 '21 at 17:12
  • @simpleApp So there is a grand over all minus sign that goes in front of each print statement, which will be the next line. So negating the minus sign – ThePewCDestroyer May 14 '21 at 18:02
  • Isn't cross entropy supposed to look like -p log(q) -(1 - p) log(1 - q) where p and q are in the range [0, 1] ? That doesn't seem to match what you have there. – Robert Dodier May 14 '21 at 18:07
  • negative number to log is throwing the assertion error. so something does not look right at the math you are trying to accomplish. pls re-check. – simpleApp May 14 '21 at 18:14

1 Answers1

0

Log(), math.log() cannot accept negative numbers.