def test_apogee_cnn(self): # Apogee_CNN print("======Apogee_CNN======") neuralnet = ApogeeCNN() neuralnet.max_epochs = 1 neuralnet.callbacks = ErrorOnNaN() neuralnet.train(random_xdata, random_ydata) prediction = neuralnet.test(random_xdata) jacobian = neuralnet.jacobian(random_xdata[:10]) np.testing.assert_array_equal(prediction.shape, random_ydata.shape) np.testing.assert_array_equal(jacobian.shape, [random_xdata[:10].shape[0], random_ydata.shape[1], random_xdata.shape[1]]) neuralnet.save(name='apogee_cnn') neuralnet_loaded = load_folder("apogee_cnn") neuralnet_loaded.max_epochs = 1 neuralnet_loaded.callbacks = ErrorOnNaN() prediction_loaded = neuralnet_loaded.test(random_xdata) # Apogee_CNN is deterministic np.testing.assert_array_equal(prediction, prediction_loaded) # Fine tuning test neuralnet_loaded.train(random_xdata, random_ydata) prediction_loaded = neuralnet_loaded.test(random_xdata) # prediction should not be equal after fine-tuning self.assertRaises(AssertionError, np.testing.assert_array_equal, prediction, prediction_loaded)
def test_apogee_cnn(self): """ Test ApogeeCNN models - training, testing, evaluation - basic astroNN model method """ print("======ApogeeCNN======") # Data preparation random_xdata = np.random.normal(0, 1, (200, 1024)) random_ydata = np.random.normal(0, 1, (200, 2)) # setup model instance neuralnet = ApogeeCNN() print(neuralnet) # assert no model before training self.assertEqual(neuralnet.has_model, False) neuralnet.max_epochs = 1 # for quick result neuralnet.callbacks = ErrorOnNaN() # Raise error and fail the test if Nan neuralnet.train(random_xdata, random_ydata) # training self.assertEqual(neuralnet.uses_learning_phase, True) # Assert ApogeeCNN uses learning phase (bc of Dropout) # test basic astroNN model method neuralnet.get_weights() neuralnet.summary() output_shape = neuralnet.output_shape input_shape = neuralnet.input_shape neuralnet.get_config() neuralnet.save_weights('save_weights_test.h5') # save astroNN weight only neuralnet.plot_dense_stats() neuralnet.plot_model() prediction = neuralnet.test(random_xdata) jacobian = neuralnet.jacobian(random_xdata[:2]) hessian = neuralnet.hessian_diag(random_xdata[:2]) hessian_full_approx = neuralnet.hessian(random_xdata[:2], method='approx') hessian_full_exact = neuralnet.hessian(random_xdata[:2], method='exact') # make sure raised if data dimension not as expected self.assertRaises(ValueError, neuralnet.jacobian, np.atleast_3d(random_xdata[:3])) # make sure evaluate run in testing phase instead of learning phase # ie no Dropout which makes model deterministic self.assertEqual( np.all(neuralnet.evaluate(random_xdata, random_ydata) == neuralnet.evaluate(random_xdata, random_ydata)), True) # assert shape correct as expected np.testing.assert_array_equal(prediction.shape, random_ydata.shape) np.testing.assert_array_equal(jacobian.shape, [random_xdata[:2].shape[0], random_ydata.shape[1], random_xdata.shape[1]]) np.testing.assert_array_equal(hessian.shape, [random_xdata[:2].shape[0], random_ydata.shape[1], random_xdata.shape[1]]) # hessian approx and exact result should have the same shape np.testing.assert_array_equal(hessian_full_approx.shape, hessian_full_exact.shape) # save weight and model again neuralnet.save(name='apogee_cnn') neuralnet.save_weights('save_weights_test.h5') # load the model again neuralnet_loaded = load_folder("apogee_cnn") neuralnet_loaded.plot_dense_stats() # assert has model without training because this is a trained model self.assertEqual(neuralnet_loaded.has_model, True) # fine tune test prediction_loaded = neuralnet_loaded.test(random_xdata) # ApogeeCNN is deterministic check again np.testing.assert_array_equal(prediction, prediction_loaded) # Fine tuning test neuralnet_loaded.max_epochs = 1 neuralnet_loaded.callbacks = ErrorOnNaN() neuralnet_loaded.train(random_xdata, random_ydata) prediction_loaded = neuralnet_loaded.test(random_xdata) # prediction should not be equal after fine-tuning self.assertRaises(AssertionError, np.testing.assert_array_equal, prediction, prediction_loaded)
def test_apogee_cnn(self): # Data preparation, keep the data size large (>800 data points to prevent issues) random_xdata = np.random.normal(0, 1, (200, 1024)) random_ydata = np.random.normal(0, 1, (200, 2)) # ApogeeCNN print("======ApogeeCNN======") neuralnet = ApogeeCNN() self.assertEqual(neuralnet.has_model, False) neuralnet.max_epochs = 1 neuralnet.callbacks = ErrorOnNaN() neuralnet.train(random_xdata, random_ydata) self.assertEqual(neuralnet.uses_learning_phase, True) neuralnet.get_weights() neuralnet.get_config() neuralnet.save_weights('save_weights_test.h5') neuralnet.summary() output_shape = neuralnet.output_shape input_shape = neuralnet.input_shape prediction = neuralnet.test(random_xdata) jacobian = neuralnet.jacobian(random_xdata[:3]) hessian = neuralnet.hessian_diag(random_xdata[:2]) hessian_full_approx = neuralnet.hessian(random_xdata[:2], method='approx') hessian_full_exact = neuralnet.hessian(random_xdata[:2], method='exact') self.assertRaises(ValueError, neuralnet.jacobian, np.atleast_3d(random_xdata[:3])) # make sure evaluate run in testing phase instead of learning phase # ie no Dropout which makes model deterministic self.assertEqual( np.all( neuralnet.evaluate(random_xdata, random_ydata) == neuralnet.evaluate(random_xdata, random_ydata)), True) np.testing.assert_array_equal(prediction.shape, random_ydata.shape) np.testing.assert_array_equal(jacobian.shape, [ random_xdata[:3].shape[0], random_ydata.shape[1], random_xdata.shape[1] ]) np.testing.assert_array_equal(hessian.shape, [ random_xdata[:2].shape[0], random_ydata.shape[1], random_xdata.shape[1] ]) neuralnet.save(name='apogee_cnn') neuralnet.save_weights('save_weights_test.h5') neuralnet_loaded = load_folder("apogee_cnn") self.assertEqual(neuralnet_loaded.has_model, True) neuralnet_loaded.max_epochs = 1 neuralnet_loaded.callbacks = ErrorOnNaN() prediction_loaded = neuralnet_loaded.test(random_xdata) # Apogee_CNN is deterministic np.testing.assert_array_equal(prediction, prediction_loaded) # Fine tuning test neuralnet_loaded.train(random_xdata, random_ydata) prediction_loaded = neuralnet_loaded.test(random_xdata) # prediction should not be equal after fine-tuning self.assertRaises(AssertionError, np.testing.assert_array_equal, prediction, prediction_loaded)
def test_apogee_cnn(self): """ Test ApogeeCNN models - training, testing, evaluation - basic astroNN model method """ print("======ApogeeCNN======") # setup model instance neuralnet = ApogeeCNN() print(neuralnet) # assert no model before training self.assertEqual(neuralnet.has_model, False) neuralnet.max_epochs = 5 # for quick result neuralnet.callbacks = ErrorOnNaN( ) # Raise error and fail the test if Nan neuralnet.targetname = ['logg', 'feh'] neuralnet.train(xdata, ydata) # training neuralnet.train_on_batch(xdata[:64], ydata[:64]) # single batch fine-tuning test # self.assertEqual(neuralnet.uses_learning_phase, True) # Assert ApogeeCNN uses learning phase (bc of Dropout) # test basic astroNN model method neuralnet.get_weights() neuralnet.summary() output_shape = neuralnet.output_shape input_shape = neuralnet.input_shape neuralnet.get_config() neuralnet.save_weights( 'save_weights_test.h5') # save astroNN weight only neuralnet.plot_dense_stats() neuralnet.plot_model() prediction = neuralnet.test(xdata) mape = np.median( np.abs(prediction[neuralnet.val_idx] - ydata[neuralnet.val_idx]) / ydata[neuralnet.val_idx], axis=0) self.assertEqual(np.all(0.15 > mape), True) # assert less than 15% error jacobian = neuralnet.jacobian(xdata[:5]) # assert shape correct as expected np.testing.assert_array_equal(prediction.shape, ydata.shape) np.testing.assert_array_equal( jacobian.shape, [xdata[:5].shape[0], ydata.shape[1], xdata.shape[1]]) hessian = neuralnet.hessian(xdata[:5], mean_output=True) np.testing.assert_array_equal( hessian.shape, [ydata.shape[1], xdata.shape[1], xdata.shape[1]]) # make sure raised if data dimension not as expected self.assertRaises(ValueError, neuralnet.jacobian, np.atleast_3d(xdata[:3])) # make sure evaluate run in testing phase instead of learning phase # ie no Dropout which makes model deterministic self.assertEqual( np.all( neuralnet.evaluate(xdata, ydata) == neuralnet.evaluate( xdata, ydata)), True) # save weight and model again neuralnet.save(name='apogee_cnn') neuralnet.save_weights('save_weights_test.h5') # load the model again neuralnet_loaded = load_folder("apogee_cnn") neuralnet_loaded.plot_dense_stats() # assert has model without training because this is a trained model self.assertEqual(neuralnet_loaded.has_model, True) # fine tune test prediction_loaded = neuralnet_loaded.test(xdata) # ApogeeCNN is deterministic check again np.testing.assert_array_equal(prediction, prediction_loaded) # Fine tuning test neuralnet_loaded.max_epochs = 5 neuralnet_loaded.callbacks = ErrorOnNaN() neuralnet_loaded.train(xdata, ydata) prediction_loaded = neuralnet_loaded.test(xdata[neuralnet.val_idx]) # prediction should not be equal after fine-tuning self.assertRaises(AssertionError, np.testing.assert_array_equal, prediction, prediction_loaded)