def test_loss_mse(self): # =============MSE/MAE============= # y_pred = tf.constant([[2., 3., 4.], [2., 3., 7.]]) y_pred_2 = tf.constant([[2., 9., 4.], [2., 0., 7.]]) y_true = tf.constant([[2., MAGIC_NUMBER, 4.], [2., MAGIC_NUMBER, 4.]]) npt.assert_almost_equal(mean_absolute_error(y_true, y_pred).numpy(), [0., 3. / 2.]) npt.assert_almost_equal(mean_squared_error(y_true, y_pred).numpy(), [0., 9. / 2]) # make sure neural network prediction won't matter for magic number term npt.assert_almost_equal(mean_absolute_error(y_true, y_pred).numpy(), mean_absolute_error(y_true, y_pred_2).numpy()) npt.assert_almost_equal(mean_squared_error(y_true, y_pred).numpy(), mean_squared_error(y_true, y_pred_2).numpy())
def test_loss_func(self): # make sure custom reduce_var works var_array = [1, 2, 3, 4, 5] self.assertEqual(reduce_var(tf.Variable(var_array)).eval(session=get_session()), np.var(var_array)) # =============Magic correction term============= # y_true = tf.Variable([[2., MAGIC_NUMBER, MAGIC_NUMBER], [2., MAGIC_NUMBER, 4.]]) npt.assert_array_equal(magic_correction_term(y_true).eval(session=get_session()), [3., 1.5]) # =============MSE/MAE============= # y_pred = tf.Variable([[2., 3., 4.], [2., 3., 7.]]) y_pred_2 = tf.Variable([[2., 9., 4.], [2., 0., 7.]]) y_true = tf.Variable([[2., MAGIC_NUMBER, 4.], [2., MAGIC_NUMBER, 4.]]) npt.assert_almost_equal(mean_absolute_error(y_true, y_pred).eval(session=get_session()), [0., 3. / 2.]) npt.assert_almost_equal(mean_squared_error(y_true, y_pred).eval(session=get_session()), [0., 9. / 2]) # make sure neural network prediction won't matter for magic number term npt.assert_almost_equal(mean_absolute_error(y_true, y_pred).eval(session=get_session()), mean_absolute_error(y_true, y_pred_2).eval(session=get_session())) npt.assert_almost_equal(mean_squared_error(y_true, y_pred).eval(session=get_session()), mean_squared_error(y_true, y_pred_2).eval(session=get_session())) # =============Mean Error============= # y_pred = tf.Variable([[1., 3., 4.], [2., 3., 7.]]) y_true = tf.Variable([[2., MAGIC_NUMBER, 3.], [2., MAGIC_NUMBER, 7.]]) npt.assert_almost_equal(mean_error(y_true, y_pred).eval(session=get_session()), [0., 0.]) # =============Accuracy============= # y_pred = tf.Variable([[1., 0., 0.], [1., 0., 0.]]) y_true = tf.Variable([[1., MAGIC_NUMBER, 1.], [0., MAGIC_NUMBER, 1.]]) npt.assert_array_equal(categorical_accuracy(y_true, y_pred).eval(session=get_session()), [1., 0.]) npt.assert_almost_equal(binary_accuracy(from_logits=False)(y_true, y_pred).eval(session=get_session()), [1. / 2., 0.]) # =============Abs Percentage Accuracy============= # y_pred = tf.Variable([[1., 0., 0.], [1., 0., 0.]]) y_pred_2 = tf.Variable([[1., 9., 0.], [1., -1., 0.]]) y_true = tf.Variable([[1., MAGIC_NUMBER, 1.], [1., MAGIC_NUMBER, 1.]]) npt.assert_array_almost_equal(mean_absolute_percentage_error(y_true, y_pred).eval(session=get_session()), [50., 50.], decimal=3) # make sure neural network prediction won't matter for magic number term npt.assert_array_almost_equal(mean_absolute_percentage_error(y_true, y_pred).eval(session=get_session()), mean_absolute_percentage_error(y_true, y_pred_2).eval(session=get_session()), decimal=3) # =============Percentage Accuracy============= # y_pred = tf.Variable([[1., 0., 0.], [1., 0., 0.]]) y_pred_2 = tf.Variable([[1., 9., 0.], [1., -1., 0.]]) y_true = tf.Variable([[1., MAGIC_NUMBER, 1.], [1., MAGIC_NUMBER, 1.]]) npt.assert_array_almost_equal(mean_percentage_error(y_true, y_pred).eval(session=get_session()), [50., 50.], decimal=3) # make sure neural network prediction won't matter for magic number term npt.assert_array_almost_equal(mean_percentage_error(y_true, y_pred).eval(session=get_session()), mean_percentage_error(y_true, y_pred_2).eval(session=get_session()), decimal=3) # =============Mean Squared Log Error============= # y_pred = tf.Variable([[1., 0., 0.], [1., 0., 0.]]) y_pred_2 = tf.Variable([[1., 9., 0.], [1., -1., 0.]]) y_true = tf.Variable([[1., MAGIC_NUMBER, 1.], [1., MAGIC_NUMBER, 1.]]) npt.assert_array_almost_equal(mean_squared_logarithmic_error(y_true, y_pred).eval(session=get_session()), [0.24, 0.24], decimal=3) # make sure neural network prediction won't matter for magic number term npt.assert_array_almost_equal(mean_squared_logarithmic_error(y_true, y_pred).eval(session=get_session()), mean_squared_logarithmic_error(y_true, y_pred_2).eval(session=get_session()), decimal=3) # =============Zeros Loss============= # y_pred = tf.Variable([[1., 0., 0.], [5., -9., 2.]]) y_true = tf.Variable([[1., MAGIC_NUMBER, 1.], [1., MAGIC_NUMBER, 1.]]) npt.assert_array_almost_equal(zeros_loss(y_true, y_pred).eval(session=get_session()), [0., 0.])