def test_apogee_bcnn(self):
        random_xdata = np.random.normal(0, 1, (200, 7514))
        random_ydata = np.random.normal(0, 1, (200, 7))

        # ApogeeBCNN
        print("======ApogeeBCNN======")
        bneuralnet = ApogeeBCNN()
        bneuralnet.targetname = [
            'teff', 'logg', 'M', 'alpha', 'C1', 'Ti', 'Ti2'
        ]

        bneuralnet.max_epochs = 1
        bneuralnet.callbacks = ErrorOnNaN()
        bneuralnet.train(random_xdata, random_ydata)
        output_shape = bneuralnet.output_shape
        input_shape = bneuralnet.input_shape
        # prevent memory issue on Tavis CI
        bneuralnet.mc_num = 3
        prediction, prediction_err = bneuralnet.test(random_xdata)

        print(bneuralnet.evaluate(random_xdata, random_ydata))

        bneuralnet.plot_dense_stats()
        bneuralnet.plot_model()
        jacobian = bneuralnet.jacobian(random_xdata[:3], mean_output=True)

        np.testing.assert_array_equal(prediction.shape, random_ydata.shape)
        bneuralnet.save(name='apogee_bcnn')

        # just to make sure it can load it back without error
        bneuralnet_loaded = load_folder("apogee_bcnn")
        bneuralnet_loaded.plot_dense_stats()
        bneuralnet_loaded.callbacks = ErrorOnNaN()

        # prevent memory issue on Tavis CI
        bneuralnet_loaded.mc_num = 2
        pred, pred_err = bneuralnet_loaded.test(random_xdata)
        bneuralnet_loaded.aspcap_residue_plot(pred, pred, pred_err['total'])
        bneuralnet_loaded.jacobian_aspcap(jacobian)
        bneuralnet_loaded.save()

        # Fine-tuning test
        bneuralnet_loaded.max_epochs = 1
        bneuralnet_loaded.train(random_xdata, random_ydata)
        pred, pred_err = bneuralnet_loaded.test_old(random_xdata)
Exemple #2
0
    def test_apogee_bcnn(self):
        """
        Test ApogeeBCNN models
        - training, testing, evaluation
        - Apogee plotting functions
        """
        # Data preparation
        random_xdata = np.random.normal(0, 1, (200, 7514))
        random_ydata = np.random.normal(0, 1, (200, 7))

        # ApogeeBCNN
        print("======ApogeeBCNN======")
        bneuralnet = ApogeeBCNN()
        # deliberately chosen targetname to test targetname conversion too
        bneuralnet.targetname = ['teff', 'logg', 'M', 'alpha', 'C1', 'Ti', 'Ti2']

        bneuralnet.max_epochs = 1  # for quick result
        bneuralnet.callbacks = ErrorOnNaN()  # Raise error and fail the test if Nan
        bneuralnet.train(random_xdata, random_ydata)
        output_shape = bneuralnet.output_shape
        input_shape = bneuralnet.input_shape
        # prevent memory issue on Tavis CI so set mc_num=2
        bneuralnet.mc_num = 2
        prediction, prediction_err = bneuralnet.test(random_xdata)
        # assert all of them not equal becaues of MC Dropout
        self.assertEqual(
            np.all(bneuralnet.evaluate(random_xdata, random_ydata) != bneuralnet.evaluate(random_xdata, random_ydata)),
            True)
        jacobian = bneuralnet.jacobian(random_xdata[:2], mean_output=True)
        np.testing.assert_array_equal(prediction.shape, random_ydata.shape)
        bneuralnet.save(name='apogee_bcnn')

        # just to make sure it can load it back without error
        bneuralnet_loaded = load_folder("apogee_bcnn")

        # prevent memory issue on Tavis CI
        bneuralnet_loaded.mc_num = 2
        pred, pred_err = bneuralnet_loaded.test(random_xdata)
        bneuralnet_loaded.aspcap_residue_plot(pred, pred, pred_err['total'])
        bneuralnet_loaded.jacobian_aspcap(jacobian)
        bneuralnet_loaded.save()

        # Fine-tuning test
        bneuralnet_loaded.max_epochs = 1
        bneuralnet_loaded.callbacks = ErrorOnNaN()
        bneuralnet_loaded.train(random_xdata, random_ydata)
        pred, pred_err = bneuralnet_loaded.test_old(random_xdata)
    def test_apogee_bcnn(self):
        """
        Test ApogeeBCNN models
        - training, testing, evaluation
        - Apogee plotting functions
        """

        # ApogeeBCNN
        print("======ApogeeBCNN======")
        bneuralnet = ApogeeBCNN()
        # deliberately chosen targetname to test targetname conversion too
        bneuralnet.targetname = ['logg', 'feh']

        bneuralnet.max_epochs = 5  # for quick result
        bneuralnet.callbacks = ErrorOnNaN(
        )  # Raise error and fail the test if Nan
        bneuralnet.train(xdata, ydata)
        output_shape = bneuralnet.output_shape
        input_shape = bneuralnet.input_shape

        bneuralnet.mc_num = 2
        prediction, prediction_err = bneuralnet.test(xdata)
        mape = np.median(
            np.abs(prediction[bneuralnet.val_idx] - ydata[bneuralnet.val_idx])
            / ydata[bneuralnet.val_idx],
            axis=0)
        self.assertEqual(np.all(0.15 > mape),
                         True)  # assert less than 15% error
        self.assertEqual(
            np.all(0.25 > np.median(prediction_err['total'], axis=0)),
            True)  # assert entropy
        # assert all of them not equal becaues of MC Dropout
        self.assertEqual(
            np.all(
                bneuralnet.evaluate(xdata, ydata) != bneuralnet.evaluate(
                    xdata, ydata)), True)
        jacobian = bneuralnet.jacobian(xdata[:2], mean_output=True)
        np.testing.assert_array_equal(prediction.shape, ydata.shape)
        bneuralnet.save(name='apogee_bcnn')
        bneuralnet.train_on_batch(xdata[:64],
                                  ydata[:64])  # single batch fine-tuning test

        # just to make sure it can load it back without error
        bneuralnet_loaded = load_folder("apogee_bcnn")

        # prevent memory issue on Tavis CI
        bneuralnet_loaded.mc_num = 2
        pred, pred_err = bneuralnet_loaded.test(xdata)
        bneuralnet_loaded.save()

        # Fine-tuning test
        bneuralnet_loaded.max_epochs = 5
        bneuralnet_loaded.callbacks = ErrorOnNaN()
        bneuralnet_loaded.train(xdata, ydata)