예제 #1
0
 def test_tcn_forecaster_runtime_error(self):
     train_data, val_data, test_data = create_data()
     forecaster = LSTMForecaster(past_seq_len=24,
                                 input_feature_num=2,
                                 output_feature_num=2,
                                 loss="mae",
                                 lr=0.01)
     with pytest.raises(RuntimeError):
         with tempfile.TemporaryDirectory() as tmp_dir_name:
             ckpt_name = os.path.join(tmp_dir_name, "ckpt")
             forecaster.save(ckpt_name)
     with pytest.raises(RuntimeError):
         forecaster.predict(test_data[0])
     with pytest.raises(RuntimeError):
         forecaster.evaluate(test_data)
예제 #2
0
 def test_tcn_forecaster_save_load(self):
     train_data, val_data, test_data = create_data()
     forecaster = LSTMForecaster(past_seq_len=24,
                                 input_feature_num=2,
                                 output_feature_num=2,
                                 loss="mae",
                                 lr=0.01)
     train_mse = forecaster.fit(train_data, epochs=2)
     with tempfile.TemporaryDirectory() as tmp_dir_name:
         ckpt_name = os.path.join(tmp_dir_name, "ckpt")
         test_pred_save = forecaster.predict(test_data[0])
         forecaster.save(ckpt_name)
         forecaster.load(ckpt_name)
         test_pred_load = forecaster.predict(test_data[0])
     np.testing.assert_almost_equal(test_pred_save, test_pred_load)
예제 #3
0
    def test_tcn_forecaster_xshard_input(self):
        train_data, val_data, test_data = create_data()
        print("original", train_data[0].dtype)
        init_orca_context(cores=4, memory="2g")
        from zoo.orca.data import XShards

        def transform_to_dict(data):
            return {'x': data[0], 'y': data[1]}

        def transform_to_dict_x(data):
            return {'x': data[0]}
        train_data = XShards.partition(train_data).transform_shard(transform_to_dict)
        val_data = XShards.partition(val_data).transform_shard(transform_to_dict)
        test_data = XShards.partition(test_data).transform_shard(transform_to_dict_x)
        for distributed in [True, False]:
            forecaster = LSTMForecaster(past_seq_len=24,
                                        input_feature_num=2,
                                        output_feature_num=2,
                                        loss="mae",
                                        lr=0.01,
                                        distributed=distributed)
            forecaster.fit(train_data, epochs=2)
            distributed_pred = forecaster.predict(test_data)
            distributed_eval = forecaster.evaluate(val_data)
        stop_orca_context()
예제 #4
0
 def test_tcn_forecaster_fit_eva_pred(self):
     train_data, val_data, test_data = create_data()
     forecaster = LSTMForecaster(past_seq_len=24,
                                 input_feature_num=2,
                                 output_feature_num=2,
                                 loss="mae",
                                 lr=0.01)
     train_loss = forecaster.fit(train_data, epochs=2)
     test_pred = forecaster.predict(test_data[0])
     assert test_pred.shape == test_data[1].shape
     test_mse = forecaster.evaluate(test_data)
예제 #5
0
 def test_forecast_lstm(self):
     # TODO hacking to fix a bug
     target_dim = 1
     model = LSTMForecaster(target_dim=target_dim,
                            feature_dim=self.x_train.shape[-1])
     model.fit(self.x_train,
               self.y_train,
               validation_data=(self.x_val, self.y_val),
               batch_size=8,
               distributed=False)
     assert model.evaluate(self.x_val, self.y_val)
     predict_result = model.predict(self.x_test)
     assert predict_result.shape == (self.x_test.shape[0], target_dim)
예제 #6
0
    def test_tcn_forecaster_distributed(self):
        train_data, val_data, test_data = create_data()
        init_orca_context(cores=4, memory="2g")

        forecaster = LSTMForecaster(past_seq_len=24,
                                    input_feature_num=2,
                                    output_feature_num=2,
                                    loss="mae",
                                    lr=0.01,
                                    distributed=True)

        forecaster.fit(train_data, epochs=2)
        distributed_pred = forecaster.predict(test_data[0])
        distributed_eval = forecaster.evaluate(val_data)

        model = forecaster.get_model()
        assert isinstance(model, torch.nn.Module)

        forecaster.to_local()
        local_pred = forecaster.predict(test_data[0])
        local_eval = forecaster.evaluate(val_data)

        np.testing.assert_almost_equal(distributed_pred, local_pred, decimal=5)

        try:
            import onnx
            import onnxruntime
            local_pred_onnx = forecaster.predict_with_onnx(test_data[0])
            local_eval_onnx = forecaster.evaluate_with_onnx(val_data)
            np.testing.assert_almost_equal(distributed_pred, local_pred_onnx, decimal=5)
        except ImportError:
            pass

        model = forecaster.get_model()
        assert isinstance(model, torch.nn.Module)

        stop_orca_context()
예제 #7
0
    def test_fit_score(self):
        look_back = 4

        # generate dataframe
        data = self.gen_data(feature_num=6, sample_num=100)
        # split train and test dataframes
        train_df, test_df = self.train_test_split(data,
                                                  test_num=20,
                                                  look_back=look_back)

        # roll data to generate model input
        x_train, y_train = self.roll_data(dataset=train_df,
                                          look_back=look_back,
                                          target_col_indexes=[0])
        x_test, y_test = self.roll_data(dataset=test_df,
                                        look_back=look_back,
                                        target_col_indexes=[0])

        # create model, train on train data and predict on test
        lstm_config = {"lstm_units": [32] * 2, "lr": 0.001}
        forecaster = LSTMForecaster(target_dim=1,
                                    feature_dim=x_train.shape[-1],
                                    **lstm_config)
        forecaster.fit(x=x_train,
                       y=y_train,
                       batch_size=1024,
                       epochs=50,
                       distributed=False)
        y_predict = forecaster.predict(x_test)

        # find anomaly using a manual set threshold
        td = ThresholdDetector()
        td.set_params(threshold=10)
        td.fit(y_test, y_predict)
        anomaly_scores = td.score()
        assert len(list(np.where(anomaly_scores > 0)[0])) == 0

        # if threshold is not provided, ThresholDetector can fit to the data
        ratio = 0.1
        td = ThresholdDetector()
        td.set_params(ratio=ratio)
        td.fit(y_test, y_predict)
        anomaly_scores = td.score()
        fitted_anomaly_indexes = list(np.where(anomaly_scores > 0)[0])
        assert len(fitted_anomaly_indexes) == int(ratio * y_test.shape[0])
예제 #8
0
    def test_fit_score(self):
        look_back = 4

        # generate dataframe
        data = self.gen_data(feature_num=6, sample_num=100)
        # split train and test dataframes
        train_df, test_df = self.train_test_split(data, test_num=20, look_back=look_back)

        # roll data to generate model input
        x_train, y_train = self.roll_data(dataset=train_df, look_back=look_back,
                                          target_col_indexes=[0])
        x_test, y_test = self.roll_data(dataset=test_df, look_back=look_back,
                                        target_col_indexes=[0])

        # create model, train on train data and predict on test
        y_train = np.expand_dims(y_train, 1)
        forecaster = LSTMForecaster(past_seq_len=look_back,
                                    input_feature_num=x_train.shape[-1],
                                    output_feature_num=1,
                                    hidden_dim=32,
                                    layer_num=2)
        forecaster.fit(data=(x_train, y_train), batch_size=1024, epochs=50)
        y_predict = forecaster.predict(x_test)
        y_predict = np.squeeze(y_predict, axis=1)

        # find anomaly using a manual set threshold
        td = ThresholdDetector()
        td.set_params(threshold=10)
        td.fit(y_test, y_predict)
        anomaly_scores = td.score()
        assert len(list(np.where(anomaly_scores > 0)[0])) == 0
        anomaly_indexes = td.anomaly_indexes()
        assert len(anomaly_indexes) == 0

        # if threshold is not provided, ThresholdDetector can fit to the data
        ratio = 0.1
        td = ThresholdDetector()
        td.set_params(ratio=ratio)
        td.fit(y_test, y_predict)
        fitted_anomaly_indexes = td.anomaly_indexes()
        assert len(fitted_anomaly_indexes) == int(ratio * y_test.shape[0])
예제 #9
0
 def test_tcn_forecaster_onnx_methods(self):
     train_data, val_data, test_data = create_data()
     forecaster = LSTMForecaster(past_seq_len=24,
                                 input_feature_num=2,
                                 output_feature_num=2,
                                 loss="mae",
                                 lr=0.01)
     forecaster.fit(train_data, epochs=2)
     try:
         import onnx
         import onnxruntime
         pred = forecaster.predict(test_data[0])
         pred_onnx = forecaster.predict_with_onnx(test_data[0])
         np.testing.assert_almost_equal(pred, pred_onnx, decimal=5)
         mse = forecaster.evaluate(test_data, multioutput="raw_values")
         mse_onnx = forecaster.evaluate_with_onnx(test_data,
                                                  multioutput="raw_values")
         np.testing.assert_almost_equal(mse, mse_onnx, decimal=5)
         mse = forecaster.evaluate(test_data)
         mse_onnx = forecaster.evaluate_with_onnx(test_data)
         np.testing.assert_almost_equal(mse, mse_onnx, decimal=5)
     except ImportError:
         pass