Beispiel #1
0
 def test_lstm_forecaster_shape_error(self):
     train_data, val_data, test_data = create_data()
     forecaster = LSTMForecaster(past_seq_len=24,
                                 input_feature_num=2,
                                 output_feature_num=1,
                                 loss="mae",
                                 lr=0.01)
     with pytest.raises(AssertionError):
         forecaster.fit(train_data, epochs=2)
Beispiel #2
0
 def test_lstm_forecaster_fit_loader(self):
     train_loader, _, _ = create_data(loader=True)
     forecaster = LSTMForecaster(past_seq_len=24,
                                 input_feature_num=2,
                                 output_feature_num=2,
                                 hidden_dim=[32, 16],
                                 layer_num=2,
                                 dropout=[0.1, 0.2],
                                 loss="mae",
                                 lr=0.01)
     train_loss = forecaster.fit(train_loader, epochs=2)
Beispiel #3
0
 def test_lstm_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,
                                 hidden_dim=[32, 16],
                                 layer_num=2,
                                 dropout=[0.1, 0.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)
Beispiel #4
0
 def test_lstm_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)
Beispiel #5
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])
Beispiel #6
0
    def test_lstm_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 bigdl.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()
Beispiel #7
0
 def test_lstm_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)
Beispiel #8
0
 def test_lstm_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)
         with pytest.raises(RuntimeError):
             forecaster.build_onnx(sess_options=1)
         sess_options = onnxruntime.SessionOptions()
         sess_options.intra_op_num_threads = 1
         forecaster.build_onnx(sess_options=sess_options)
         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
Beispiel #9
0
    def test_lstm_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()