def test_binary_crossentropy(self): y_pred = tf.constant([[0.5, 0.0, 1.0], [2.0, 0.0, -1.0]]) y_pred_2 = tf.constant([[0.5, 2.0, 1.0], [2.0, 2.0, -1.0]]) y_true = tf.constant([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 0.0]]) y_pred_sigmoid = tf.nn.sigmoid(y_pred) y_pred_2_sigmoid = tf.nn.sigmoid(y_pred_2) # Truth with Magic number is wrong npt.assert_array_almost_equal( binary_crossentropy(y_true, y_pred_sigmoid).numpy(), binary_crossentropy(y_true, y_pred, from_logits=True).numpy(), decimal=3, ) # make sure neural network prediction won't matter for magic number term npt.assert_array_almost_equal( binary_crossentropy(y_true, y_pred_2, from_logits=True).numpy(), binary_crossentropy(y_true, y_pred, from_logits=True).numpy(), decimal=3, ) npt.assert_array_almost_equal( binary_crossentropy(y_true, y_pred_sigmoid).numpy(), binary_crossentropy(y_true, y_pred_2_sigmoid).numpy(), decimal=3, )
def test_binary_crossentropy(self): y_pred = tf.Variable([[0.5, 0., 1.], [2., 0., -1.]]) y_pred_2 = tf.Variable([[0.5, 2., 1.], [2., 2., -1.]]) y_true = tf.Variable([[1., MAGIC_NUMBER, 1.], [1., MAGIC_NUMBER, 0.]]) y_pred_sigmoid = tf.nn.sigmoid(y_pred) y_pred_2_sigmoid = tf.nn.sigmoid(y_pred_2) # Truth with Magic number is wrong npt.assert_array_almost_equal( binary_crossentropy(y_true, y_pred_sigmoid).eval(session=get_session()), binary_crossentropy(y_true, y_pred, from_logits=True).eval(session=get_session()), decimal=3) # make sure neural network prediction won't matter for magic number term npt.assert_array_almost_equal( binary_crossentropy(y_true, y_pred_2, from_logits=True).eval(session=get_session()), binary_crossentropy(y_true, y_pred, from_logits=True).eval(session=get_session()), decimal=3) npt.assert_array_almost_equal( binary_crossentropy(y_true, y_pred_sigmoid).eval(session=get_session()), binary_crossentropy(y_true, y_pred_2_sigmoid).eval(session=get_session()), decimal=3)
def test_binary_crossentropy(self): with tf.device("/cpu:0"), context.eager_mode(): y_pred = tf.constant([[0.5, 0., 1.], [2., 0., -1.]]) y_pred_2 = tf.constant([[0.5, 2., 1.], [2., 2., -1.]]) y_true = tf.constant([[1., MAGIC_NUMBER, 1.], [1., MAGIC_NUMBER, 0.]]) y_pred_sigmoid = tf.nn.sigmoid(y_pred) y_pred_2_sigmoid = tf.nn.sigmoid(y_pred_2) # Truth with Magic number is wrong npt.assert_array_almost_equal( binary_crossentropy(y_true, y_pred_sigmoid).numpy(), binary_crossentropy(y_true, y_pred, from_logits=True).numpy(), decimal=3) # make sure neural network prediction won't matter for magic number term npt.assert_array_almost_equal( binary_crossentropy(y_true, y_pred_2, from_logits=True).numpy(), binary_crossentropy(y_true, y_pred, from_logits=True).numpy(), decimal=3) npt.assert_array_almost_equal( binary_crossentropy(y_true, y_pred_sigmoid).numpy(), binary_crossentropy(y_true, y_pred_2_sigmoid).numpy(), decimal=3)