def test_gen_model_input(self): # normal X result = data_setup.gen_model_input( data_setup.gen_dataset(self.x, "target", np.mean)["na_filled_dataset"], n_timesteps=2, ) self.assertTrue( ( result[0] == np.array([[[1, 4], [2, 5]], [[2, 5], [3, 6]]], np.float64) ).all() ) # missing ys result = data_setup.gen_model_input( data_setup.gen_dataset(self.missing_y, "target", np.nanmedian)[ "na_filled_dataset" ], n_timesteps=2, ) self.assertTrue( ( result[0] == np.array( [ [[1, 4], [2, 7.5]], [[3, 6], [4, 7]], [[4, 7], [5, 8]], [[6, 9], [7, 10]], ], np.float64, ) # result[1] == np.array([8, 10, 11, 13], np.float64) ).all() )
def test_gen_ragged_X(self): ragged_dataset = data_setup.gen_dataset(self.long_x, "target")[ "for_ragged_dataset" ] X = data_setup.gen_model_input(ragged_dataset, n_timesteps=2) result = data_setup.gen_ragged_X( X[0], [1, 2], 0, ragged_dataset, "target", fill_ragged_edges=np.nanmedian, backup_fill_method=np.nanmedian, ) self.assertTrue( ( result == np.array( [ [[1.0, 7.5], [4.0, 7.5]], [[2.0, 7.5], [4.0, 7.5]], [[3.0, 7.5], [4.0, 7.5]], [[4.0, 7.5], [4.0, 7.5]], [[5.0, 7.5], [4.0, 7.5]], [[6.0, 7.5], [4.0, 7.5]], ] ) ).all() )
def test_instantiate_model(self): model_input = data_setup.gen_model_input( data_setup.gen_dataset(self.x, "target")["na_filled_dataset"], n_timesteps=2)[0] # first tuple of the function, X result = modelling.instantiate_model(model_input, n_timesteps=2) self.assertEqual(result["mv_lstm"].n_layers, 2) self.assertEqual(str(type(result["criterion"])), "<class 'torch.nn.modules.loss.L1Loss'>") self.assertEqual(str(type(result["optimizer"])), "<class 'torch.optim.adam.Adam'>")
def test_predict(self): model_input = data_setup.gen_model_input(data_setup.gen_dataset( self.x, "target")["na_filled_dataset"], n_timesteps=2) result = modelling.instantiate_model(model_input[0], n_timesteps=2) model = result["mv_lstm"] crit = result["criterion"] opt = result["optimizer"] model_result = modelling.train_model(model_input[0], model_input[1], model, crit, opt, quiet=True) model = model_result["mv_lstm"] preds = modelling.predict(model_input[0], model) self.assertEqual(len(preds), 2)
def test_different_optimizer(self): model_input = data_setup.gen_model_input( data_setup.gen_dataset(self.x, "target")["na_filled_dataset"], n_timesteps=2)[0] # first tuple of the function, X result = modelling.instantiate_model(model_input, n_timesteps=2, optimizer=torch.optim.SGD, optimizer_parameters={ "lr": 1e-3, "weight_decay": 0.96 }) self.assertEqual(result["mv_lstm"].n_layers, 2) self.assertEqual(str(type(result["criterion"])), "<class 'torch.nn.modules.loss.L1Loss'>") self.assertEqual(str(type(result["optimizer"])), "<class 'torch.optim.sgd.SGD'>") self.assertEqual( str((result["optimizer"])).strip('\n'), "SGD (\nParameter Group 0\n dampening: 0\n lr: 0.001\n momentum: 0\n nesterov: False\n weight_decay: 0.96\n)" .strip('\n'))