【发布时间】:2021-08-04 20:14:25
【问题描述】:
我不熟悉在 Python 中使用 math 库。这个脚本的目的是展示我对Cross-Entropy 错误函数的“解决”。
我检查了我的括号和运算符,用我的菜鸟的眼睛看不出有什么问题。
错误发生在最后一行。
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)) + '))')
输出和错误:
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
【问题讨论】:
-
如果你把最后一行分解成简单的语句,例如
m.log(out); m.log(1-target)等,并在事情通过测试时建立更多嵌套调用,您很快就会发现您正在尝试计算负数的对数。 -
这里是问题
round(m.log(1 - out), dp) # are you sure you need a log of negative 0.09899999999999998 ? -
@simpleApp 所以在每个打印语句的前面都有一个减号,这将是下一行。所以否定减号
-
交叉熵不应该看起来像 -p log(q) -(1 - p) log(1 - q) 其中 p 和 q 在 [0, 1] 范围内吗?这似乎与您所拥有的不符。
-
要记录的负数会引发断言错误。所以在你试图完成的数学运算中有些东西看起来不正确。请重新检查。
标签: python math printing cross-entropy