コード例 #1
0
 def test_relu(self):
     # make sure its the same as tensorflow
     x = np.array([-1., 2., 3., 4.])
     astroNN_x = relu(x)
     with tf.device("/cpu:0"), context.eager_mode():
         tf_x = tf.nn.relu(tf.convert_to_tensor(x))
         npt.assert_array_equal(tf_x.numpy(), astroNN_x)
コード例 #2
0
 def test_loss_magic(self):
     # =============Magic correction term============= #
     with tf.device("/cpu:0"), context.eager_mode():
         y_true = tf.constant([[2., MAGIC_NUMBER, MAGIC_NUMBER],
                               [2., MAGIC_NUMBER, 4.]])
         npt.assert_array_equal(
             magic_correction_term(y_true).numpy(), [3., 1.5])
コード例 #3
0
    def test_sigmoid(self):
        # make sure its the same as tensorflow
        x = np.array([-1., 2., 3., 4.])
        astroNN_x = sigmoid(x)
        with tf.device("/cpu:0"), context.eager_mode():
            tf_x = tf.nn.sigmoid(tf.convert_to_tensor(x))
            npt.assert_array_equal(tf_x.numpy(), astroNN_x)

        # make sure identity transform
        npt.assert_array_almost_equal(sigmoid_inv(sigmoid(x)), x)

        # for list
        # make sure its the same as tensorflow
        x = [-1., 2., 3., 4.]
        astroNN_x_list = sigmoid(x)
        npt.assert_array_equal(astroNN_x_list, astroNN_x)

        # make sure identity transform
        npt.assert_array_almost_equal(sigmoid_inv(sigmoid(x)), x)

        # for float
        # make sure its the same as tensorflow
        x = 0.
        astroNN_x = sigmoid(x)
        npt.assert_array_equal(0.5, astroNN_x)

        # make sure identity transform
        npt.assert_array_almost_equal(sigmoid_inv(sigmoid(x)), x)
コード例 #4
0
    def test_loss_mean_err(self):
        # =============Mean Error============= #
        with tf.device("/cpu:0"), context.eager_mode():
            y_pred = tf.constant([[1., 3., 4.], [2., 3., 7.]])
            y_true = tf.constant([[2., MAGIC_NUMBER, 3.],
                                  [2., MAGIC_NUMBER, 7.]])

            npt.assert_almost_equal(
                mean_error(y_true, y_pred).numpy(), [0., 0.])
コード例 #5
0
    def test_negative_log_likelihood(self):
        with tf.device("/cpu:0"), context.eager_mode():
            y_pred = tf.constant([[0.5, 0., 1.], [2., 0., -1.]])
            y_true = tf.constant([[1., MAGIC_NUMBER, 1.],
                                  [1., MAGIC_NUMBER, 0.]])

            npt.assert_array_almost_equal(nll(y_true, y_pred).numpy(),
                                          0.34657377,
                                          decimal=3)
コード例 #6
0
    def test_loss_zeros(self):
        # =============Zeros Loss============= #
        with tf.device("/cpu:0"), context.eager_mode():
            y_pred = tf.constant([[1., 0., 0.], [5., -9., 2.]])
            y_true = tf.constant([[1., MAGIC_NUMBER, 1.],
                                  [1., MAGIC_NUMBER, 1.]])

            npt.assert_array_almost_equal(
                zeros_loss(y_true, y_pred).numpy(), [0., 0.])
コード例 #7
0
    def test_loss_acurrancy(self):
        # =============Accuracy============= #
        with tf.device("/cpu:0"), context.eager_mode():
            y_pred = tf.constant([[1., 0., 0.], [1., 0., 0.]])
            y_true = tf.constant([[1., MAGIC_NUMBER, 1.],
                                  [0., MAGIC_NUMBER, 1.]])

            npt.assert_array_equal(
                categorical_accuracy(y_true, y_pred).numpy(), [1., 0.])
            npt.assert_almost_equal(
                binary_accuracy(y_true, y_pred).numpy(), [1. / 2., 0.])
コード例 #8
0
    def test_categorical_crossentropy(self):
        # Truth with Magic number is wrong
        with tf.device("/cpu:0"), context.eager_mode():
            y_pred = tf.constant([[1., 0., 1.], [2., 1., 0.]])
            y_true = tf.constant([[1., MAGIC_NUMBER, 1.],
                                  [1., MAGIC_NUMBER, 0.]])

            y_pred_softmax = tf.nn.softmax(y_pred)

            npt.assert_array_almost_equal(
                categorical_crossentropy(y_true, y_pred_softmax).numpy(),
                categorical_crossentropy(y_true, y_pred,
                                         from_logits=True).numpy(),
                decimal=3)
コード例 #9
0
    def test_loss_percentage_error(self):
        # =============Percentage Accuracy============= #
        with tf.device("/cpu:0"), context.eager_mode():
            y_pred = tf.constant([[1., 0., 0.], [1., 0., 0.]])
            y_pred_2 = tf.constant([[1., 9., 0.], [1., -1., 0.]])
            y_true = tf.constant([[1., MAGIC_NUMBER, 1.],
                                  [1., MAGIC_NUMBER, 1.]])

            npt.assert_array_almost_equal(mean_percentage_error(
                y_true, y_pred).numpy(), [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).numpy(),
                mean_percentage_error(y_true, y_pred_2).numpy(),
                decimal=3)
コード例 #10
0
    def test_loss_log_error(self):
        # =============Mean Squared Log Error============= #
        with tf.device("/cpu:0"), context.eager_mode():
            y_pred = tf.constant([[1., 0., 0.], [1., 0., 0.]])
            y_pred_2 = tf.constant([[1., 9., 0.], [1., -1., 0.]])
            y_true = tf.constant([[1., MAGIC_NUMBER, 1.],
                                  [1., MAGIC_NUMBER, 1.]])

            npt.assert_array_almost_equal(mean_squared_logarithmic_error(
                y_true, y_pred).numpy(), [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).numpy(),
                mean_squared_logarithmic_error(y_true, y_pred_2).numpy(),
                decimal=3)
コード例 #11
0
    def test_regularizator(self):
        # make sure its the same as tensorflow
        x = np.array([-1., 2., 3., 4.])
        reg = 0.2

        astroNN_x = l1(x, l1=reg)
        astroNN_x_2 = l2(x, l2=reg)

        with tf.device("/cpu:0"), context.eager_mode():
            l1_reg = tf.keras.regularizers.l1(l=reg)
            l2_reg = tf.keras.regularizers.l2(l=reg)
            tf_x = l1_reg(tf.convert_to_tensor(x))
            tf_x_2 = l2_reg(tf.convert_to_tensor(x))

            npt.assert_array_almost_equal(tf_x.numpy(), astroNN_x)
            npt.assert_array_almost_equal(tf_x_2.numpy(), astroNN_x_2)
コード例 #12
0
    def test_loss_mse(self):
        # =============MSE/MAE============= #
        with tf.device("/cpu:0"), context.eager_mode():
            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())
コード例 #13
0
    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)
コード例 #14
0
 def test_loss_func_util(self):
     with tf.device("/cpu:0"), context.eager_mode():
         # make sure custom reduce_var works
         content = [1, 2, 3, 4, 5]
         var_array = tf.constant(content)
         self.assertEqual(reduce_var(var_array).numpy(), np.var(content))