Python Tensorflow测试失败
我正在测试TF,当我测试加法时,我得到:Python Tensorflow测试失败,python,tensorflow,Python,Tensorflow,我正在测试TF,当我测试加法时,我得到: E AssertionError: E Arrays are not equal E E (mismatch 100.0%) E x: array(4.099999904632568, dtype=float32) E y: array(4.1) 如果我将测试从3.1更改为3.0,它会工作吗?有什么建议吗 代码 import tensorflow as tf import
E AssertionError:
E Arrays are not equal
E
E (mismatch 100.0%)
E x: array(4.099999904632568, dtype=float32)
E y: array(4.1)
如果我将测试从3.1更改为3.0,它会工作吗?有什么建议吗
代码
import tensorflow as tf
import unittest
def sum_op(param1, param2):
return tf.add(param1, param2)
with tf.Graph().as_default():
result = sum_op(tf.constant(1.0), tf.Variable(2.0))
with tf.Session() as sess:
tf.global_variables_initializer().run()
print sess.run(result)
class AddTest(tf.test.TestCase):
def testAdd(self):
with self.test_session() as sess:
self.assertAllEqual(sum_op(1.0, 3.1).eval(), 4.1)
suite = unittest.TestLoader().loadTestsFromTestCase(AddTest)
unittest.TextTestRunner(verbosity=2).run(suite)
一般来说,用浮点运算测试精确相等容易出错。你可能想读书
检查浮点代码时应该使用公差,例如numpy的
isclose
我使用了AssertalPostequal和delta选项