Exemple #1
0
def choose_one_step_single_step_chosen_transform():
    a_callback = TapeCallbackFunction()
    b_callback = TapeCallbackFunction()
    c_callback = TapeCallbackFunction()
    d_callback = TapeCallbackFunction()

    return NeuraxleTestCase(pipeline=Pipeline([
        ChooseOneStepOf([
            ('a',
             FitTransformCallbackStep(a_callback,
                                      c_callback,
                                      transform_function=lambda di: di * 2)),
            ('b',
             FitTransformCallbackStep(b_callback,
                                      d_callback,
                                      transform_function=lambda di: di * 2))
        ]),
    ]),
                            callbacks=[
                                a_callback, c_callback, b_callback, d_callback
                            ],
                            expected_callbacks_data=[DATA_INPUTS, [], [], []],
                            hyperparams={
                                'ChooseOneOrManyStepsOf__choice': 'a'
                            },
                            expected_processed_outputs=np.array(
                                [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]))
Exemple #2
0
def create_test_case_fit_multiple_steps_choosen():
    a_callback = TapeCallbackFunction()
    b_callback = TapeCallbackFunction()
    c_callback = TapeCallbackFunction()
    d_callback = TapeCallbackFunction()

    return NeuraxleTestCase(
        pipeline=Pipeline([
            ChooseOneOrManyStepsOf([
                ('a', FitTransformCallbackStep(a_callback, c_callback, transform_function=lambda di: di * 2)),
                ('b', FitTransformCallbackStep(b_callback, d_callback, transform_function=lambda di: di * 2))
            ]),
        ]),
        callbacks=[a_callback, c_callback, b_callback, d_callback],
        expected_callbacks_data=[
            [],
            (DATA_INPUTS, EXPECTED_OUTPUTS),
            [],
            (DATA_INPUTS, EXPECTED_OUTPUTS)
        ],
        hyperparams={
            'ChooseOneOrManyStepsOf__a__enabled': True,
            'ChooseOneOrManyStepsOf__b__enabled': True
        },
        hyperparams_space={
            'ChooseOneOrManyStepsOf__a__enabled': Boolean(),
            'ChooseOneOrManyStepsOf__b__enabled': Boolean()
        },
        expected_processed_outputs=np.array([0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
    )
Exemple #3
0
def test_sequential_queued_pipeline_should_fit_transform_without_multiprocessing(
):
    batch_size = 10
    p = SequentialQueuedPipeline(
        [(1,
          FitTransformCallbackStep(
              transform_function=lambda di: np.array(di) * 2)),
         (1,
          FitTransformCallbackStep(
              transform_function=lambda di: np.array(di) * 2)),
         (1,
          FitTransformCallbackStep(
              transform_function=lambda di: np.array(di) * 2)),
         (1,
          FitTransformCallbackStep(
              transform_function=lambda di: np.array(di) * 2))],
        batch_size=batch_size,
        max_queue_size=5)
    queue_joiner_for_test = QueueJoinerForTest(batch_size=batch_size)
    p.steps[-1] = queue_joiner_for_test
    p.steps_as_tuple[-1] = (p.steps_as_tuple[-1][0], queue_joiner_for_test)
    p._refresh_steps()

    p, outputs = p.fit_transform(list(range(100)), list(range(100)))

    assert not p[-1].called_queue_joiner
    assert np.array_equal(outputs, EXPECTED_OUTPUTS)
Exemple #4
0
def test_queued_pipeline_saving(tmpdir):
    # Given
    p = ParallelQueuedFeatureUnion([
        ('1', FitTransformCallbackStep()),
        ('2', FitTransformCallbackStep()),
        ('3', FitTransformCallbackStep()),
        ('4', FitTransformCallbackStep()),
    ], n_workers_per_step=1, max_queue_size=10, batch_size=10)

    # When
    p, outputs = p.fit_transform(list(range(100)), list(range(100)))
    p.save(ExecutionContext(tmpdir))
    p.apply('clear_callbacks')

    # Then

    assert len(p[0].wrapped.transform_callback_function.data) == 0
    assert len(p[0].wrapped.fit_callback_function.data) == 0
    assert len(p[1].wrapped.transform_callback_function.data) == 0
    assert len(p[1].wrapped.fit_callback_function.data) == 0
    assert len(p[2].wrapped.transform_callback_function.data) == 0
    assert len(p[2].wrapped.fit_callback_function.data) == 0
    assert len(p[3].wrapped.transform_callback_function.data) == 0
    assert len(p[3].wrapped.fit_callback_function.data) == 0

    p = p.load(ExecutionContext(tmpdir))

    assert len(p[0].wrapped.transform_callback_function.data) == 10
    assert len(p[0].wrapped.fit_callback_function.data) == 10
    assert len(p[1].wrapped.transform_callback_function.data) == 10
    assert len(p[1].wrapped.fit_callback_function.data) == 10
    assert len(p[2].wrapped.transform_callback_function.data) == 10
    assert len(p[2].wrapped.fit_callback_function.data) == 10
    assert len(p[3].wrapped.transform_callback_function.data) == 10
    assert len(p[3].wrapped.fit_callback_function.data) == 10
Exemple #5
0
def create_checkpoint_test_case(tmpdir):
    tape_transform_1 = TapeCallbackFunction()
    tape_fit_1 = TapeCallbackFunction()
    tape_transform_2 = TapeCallbackFunction()
    tape_fit_2 = TapeCallbackFunction()
    pipeline = ResumablePipeline(
        [('step1', FitTransformCallbackStep(tape_transform_1, tape_fit_1)),
         ('checkpoint', DefaultCheckpoint()),
         ('step2', FitTransformCallbackStep(tape_transform_2, tape_fit_2))],
        cache_folder=tmpdir)

    return CheckpointTest(tape_transform_1, tape_fit_1, tape_transform_2,
                          tape_fit_2, pipeline)
Exemple #6
0
def test_load_full_dump_from_path(tmpdir):
    # Given
    tape_fit_callback_function = TapeCallbackFunction()
    tape_transform_callback_function = TapeCallbackFunction()
    pipeline = Pipeline(
        [('step_a', Identity()),
         ('step_b',
          OutputTransformerWrapper(
              FitTransformCallbackStep(tape_fit_callback_function,
                                       tape_transform_callback_function)))],
        cache_folder=tmpdir).set_name(PIPELINE_NAME)

    # When
    pipeline, outputs = pipeline.fit_transform(DATA_INPUTS, EXPECTED_OUTPUTS)
    pipeline.save(ExecutionContext(tmpdir), full_dump=True)

    # Then
    loaded_pipeline = ExecutionContext(tmpdir).load(
        os.path.join(PIPELINE_NAME, 'step_b'))

    assert isinstance(loaded_pipeline, OutputTransformerWrapper)
    loaded_step_b_wrapped_step = loaded_pipeline.wrapped
    assert np.array_equal(
        loaded_step_b_wrapped_step.transform_callback_function.data[0],
        EXPECTED_OUTPUTS)
    assert np.array_equal(
        loaded_step_b_wrapped_step.fit_callback_function.data[0][0],
        EXPECTED_OUTPUTS)
    assert np.array_equal(
        loaded_step_b_wrapped_step.fit_callback_function.data[0][1],
        [None] * len(EXPECTED_OUTPUTS))
def test_validation_split_wrapper_should_split_data(tmpdir):
    transform_callback = TapeCallbackFunction()
    fit_callback = TapeCallbackFunction()
    random_search = RandomSearch(
        ValidationSplitWrapper(FitTransformCallbackStep(
            transform_callback_function=transform_callback,
            fit_callback_function=fit_callback,
            transform_function=lambda di: di * 2),
                               test_size=0.1))
    data_inputs = np.random.randint(1, 100, (100, 5))
    expected_outputs = np.random.randint(1, 100, (100, 5))

    random_search, outputs = random_search.fit_transform(
        data_inputs, expected_outputs)

    assert np.array_equal(outputs, data_inputs * 2)

    # should fit on train split
    assert np.array_equal(fit_callback.data[0][0], data_inputs[0:90])
    assert np.array_equal(fit_callback.data[0][1], expected_outputs[0:90])

    # should transform on test split
    assert np.array_equal(transform_callback.data[0], data_inputs[0:90])
    assert np.array_equal(transform_callback.data[1], data_inputs[90:])

    # should transform on all data at the end
    assert np.array_equal(transform_callback.data[2], data_inputs)

    assert random_search.best_model.scores_train is not None
    assert random_search.best_model.scores_validation is not None
    assert random_search.best_model.scores_train_mean is not None
    assert random_search.best_model.scores_validation_mean is not None
    assert random_search.best_model.scores_train_std is not None
    assert random_search.best_model.scores_validation_std is not None
Exemple #8
0
def test_sequential_queued_pipeline_should_fit_without_multiprocessing():
    batch_size = 10
    p = SequentialQueuedPipeline([(1, FitTransformCallbackStep()),
                                  (1, FitTransformCallbackStep()),
                                  (1, FitTransformCallbackStep()),
                                  (1, FitTransformCallbackStep())],
                                 batch_size=batch_size,
                                 max_queue_size=5)
    queue_joiner_for_test = QueueJoinerForTest(batch_size=batch_size)
    p.steps[-1] = queue_joiner_for_test
    p.steps_as_tuple[-1] = (p.steps_as_tuple[-1][0], queue_joiner_for_test)
    p._refresh_steps()

    p = p.fit(list(range(100)), list(range(100)))

    assert not p[-1].called_queue_joiner
Exemple #9
0
def test_automl_early_stopping_callback(tmpdir):
    # TODO: fix this unit test
    # Given
    hp_repository = InMemoryHyperparamsRepository(cache_folder=str(tmpdir))
    n_epochs = 60
    auto_ml = AutoML(
        pipeline=Pipeline([
            FitTransformCallbackStep().set_name('callback'),
            MultiplyByN(2).set_hyperparams_space(
                HyperparameterSpace({'multiply_by': FixedHyperparameter(2)})),
            NumpyReshape(new_shape=(-1, 1)),
            linear_model.LinearRegression()
        ]),
        hyperparams_optimizer=RandomSearchHyperparameterSelectionStrategy(),
        validation_splitter=ValidationSplitter(0.20),
        scoring_callback=ScoringCallback(mean_squared_error,
                                         higher_score_is_better=False),
        callbacks=[
            MetricCallback('mse',
                           metric_function=mean_squared_error,
                           higher_score_is_better=False),
        ],
        n_trials=1,
        refit_trial=True,
        epochs=n_epochs,
        hyperparams_repository=hp_repository)

    # When
    data_inputs = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    expected_outputs = data_inputs * 2
    auto_ml = auto_ml.fit(data_inputs=data_inputs,
                          expected_outputs=expected_outputs)

    # Then
    p = auto_ml.get_best_model()
Exemple #10
0
def test_fit_transform_should_fit_transform_all_steps_for_each_data_inputs_expected_outputs():
    tape = TapeCallbackFunction()
    tape_fit = TapeCallbackFunction()
    p = Pipeline([
        ForEachDataInput(Pipeline([
            FitTransformCallbackStep(tape.callback, tape_fit, ["1"]),
            FitTransformCallbackStep(tape.callback, tape_fit, ["2"]),
        ]))
    ])
    data_inputs = [[0, 1], [1, 2]]
    expected_outputs = [[2, 3], [4, 5]]

    p, outputs = p.fit_transform(data_inputs, expected_outputs)

    assert tape.get_name_tape() == ["1", "2", "1", "2"]
    assert tape_fit.get_name_tape() == ["1", "2", "1", "2"]
    assert tape_fit.data == [([0, 1], [2, 3]), ([0, 1], [2, 3]), ([1, 2], [4, 5]), ([1, 2], [4, 5])]
Exemple #11
0
def test_queued_pipeline_saving(tmpdir, use_processes, use_savers):
    # Given
    p = ParallelQueuedFeatureUnion([
        ('1', 4, 10, FitTransformCallbackStep()),
        ('2', 4, 10, FitTransformCallbackStep()),
        ('3', 4, 10, FitTransformCallbackStep()),
        ('4', 4, 10, FitTransformCallbackStep()),
    ],
                                   n_workers_per_step=4,
                                   max_queue_size=10,
                                   batch_size=10,
                                   use_processes=use_processes,
                                   use_savers=use_savers).with_context(
                                       ExecutionContext(tmpdir))

    # When
    p, _ = p.fit_transform(list(range(200)), list(range(200)))
    p = p.wrapped  # clear execution context wrapper
    p.save(ExecutionContext(tmpdir))
    p.apply('clear_callbacks')

    # Then

    assert len(p[0].wrapped.transform_callback_function.data) == 0
    assert len(p[0].wrapped.fit_callback_function.data) == 0
    assert len(p[1].wrapped.transform_callback_function.data) == 0
    assert len(p[1].wrapped.fit_callback_function.data) == 0
    assert len(p[2].wrapped.transform_callback_function.data) == 0
    assert len(p[2].wrapped.fit_callback_function.data) == 0
    assert len(p[3].wrapped.transform_callback_function.data) == 0
    assert len(p[3].wrapped.fit_callback_function.data) == 0

    p = p.load(ExecutionContext(tmpdir))

    assert len(p[0].wrapped.transform_callback_function.data) == 20
    assert len(p[0].wrapped.fit_callback_function.data) == 20
    assert len(p[1].wrapped.transform_callback_function.data) == 20
    assert len(p[1].wrapped.fit_callback_function.data) == 20
    assert len(p[2].wrapped.transform_callback_function.data) == 20
    assert len(p[2].wrapped.fit_callback_function.data) == 20
    assert len(p[3].wrapped.transform_callback_function.data) == 20
    assert len(p[3].wrapped.fit_callback_function.data) == 20
Exemple #12
0
def test_choose_one_step_of_update_hyperparams():
    a_callback = TapeCallbackFunction()
    b_callback = TapeCallbackFunction()
    c_callback = TapeCallbackFunction()
    d_callback = TapeCallbackFunction()

    choose_one_step_of = ChooseOneStepOf([
        ('a',
         FitTransformCallbackStep(
             a_callback, c_callback,
             transform_function=lambda di: di * 2).set_name("step_1")),
        ('b',
         FitTransformCallbackStep(
             b_callback, d_callback,
             transform_function=lambda di: di * 2).set_name("step_1"))
    ])

    p = Pipeline([choose_one_step_of])

    p.transform(DATA_INPUTS)

    assert len(a_callback.data) == 1
    assert all(a_callback.data[0] == DATA_INPUTS)
    assert len(b_callback.data) == 0
    assert len(c_callback.data) == 0
    assert len(d_callback.data) == 0

    choose_one_step_of.update_hyperparams({'choice': 'b'})

    p.transform(DATA_INPUTS)

    assert len(a_callback.data) == 1
    assert all(a_callback.data[0] == DATA_INPUTS)
    assert len(b_callback.data) == 1
    assert all(b_callback.data[0] == DATA_INPUTS)
    assert len(c_callback.data) == 0
    assert len(d_callback.data) == 0
Exemple #13
0
def test_choose_one_step_of_set_hyperparams(method_name, args, kwargs):
    a_callback = TapeCallbackFunction()
    b_callback = TapeCallbackFunction()
    c_callback = TapeCallbackFunction()
    d_callback = TapeCallbackFunction()

    choose_one_step_of = ChooseOneStepOf([
        ('a',
         FitTransformCallbackStep(
             a_callback, c_callback,
             transform_function=lambda di: di * 2).set_name("step_1")),
        ('b',
         FitTransformCallbackStep(
             b_callback, d_callback,
             transform_function=lambda di: di * 2).set_name("step_1"))
    ])

    p = Pipeline([choose_one_step_of])

    p.transform(DATA_INPUTS)

    assert len(a_callback.data) == 1
    assert all(a_callback.data[0] == DATA_INPUTS)
    assert len(b_callback.data) == 0
    assert len(c_callback.data) == 0
    assert len(d_callback.data) == 0

    getattr(choose_one_step_of, method_name)(*args, **kwargs)

    p.transform(DATA_INPUTS)

    assert len(a_callback.data) == 1
    assert all(a_callback.data[0] == DATA_INPUTS)
    assert len(b_callback.data) == 1
    assert all(b_callback.data[0] == DATA_INPUTS)
    assert len(c_callback.data) == 0
    assert len(d_callback.data) == 0
def test_data_shuffling_should_shuffle_data_inputs_and_expected_outputs():
    callback_fit = TapeCallbackFunction()
    callback_transform = TapeCallbackFunction()
    data_shuffler = Pipeline([
        DataShuffler(seed=42, increment_seed_after_each_fit=True),
        FitTransformCallbackStep(callback_transform, callback_fit)
    ])
    data_inputs = np.array(range(10))
    expected_outputs = np.array(range(10, 20))

    outputs = data_shuffler.fit_transform(data_inputs, expected_outputs)

    assert not np.array_equal(outputs, data_inputs)
    assert not np.array_equal(callback_fit.data[0][0], data_inputs)
    assert not np.array_equal(callback_fit.data[0][1], expected_outputs)
    assert not np.array_equal(callback_transform.data, data_inputs)
Exemple #15
0
def test_validation_split_wrapper_handle_methods_should_split_data():
    transform_callback = TapeCallbackFunction()
    fit_callback = TapeCallbackFunction()
    validation_split_wrapper = ValidationSplitWrapper(
        FitTransformCallbackStep(
            transform_callback_function=transform_callback,
            fit_callback_function=fit_callback,
            transform_function=lambda di: di * 2
        ),
        test_size=0.1
    )
    data_inputs = np.random.randint(1, 100, (100, 5))
    expected_outputs = np.random.randint(1, 100, (100, 5))

    validation_split_wrapper, outputs = validation_split_wrapper.handle_fit_transform(
        DataContainer(data_inputs=data_inputs, current_ids=list(range(len(data_inputs))),
                      expected_outputs=expected_outputs),
        ExecutionContext(DEFAULT_CACHE_FOLDER)
    )

    assert np.array_equal(outputs.data_inputs, data_inputs * 2)

    # should fit on train split
    assert np.array_equal(fit_callback.data[0][0], data_inputs[0:90])
    assert np.array_equal(fit_callback.data[0][1], expected_outputs[0:90])

    # should transform on test split
    assert np.array_equal(transform_callback.data[0], data_inputs[0:90])
    assert np.array_equal(transform_callback.data[1], data_inputs[90:])

    # should transform on all data at the end
    assert np.array_equal(transform_callback.data[2], data_inputs)

    assert validation_split_wrapper.scores_train is not None
    assert validation_split_wrapper.scores_validation is not None
    assert validation_split_wrapper.scores_train_mean is not None
    assert validation_split_wrapper.scores_validation_mean is not None
    assert validation_split_wrapper.scores_train_std is not None
    assert validation_split_wrapper.scores_validation_std is not None
 def fit_transform(self, data_inputs, expected_outputs=None):
     FitTransformCallbackStep.fit_transform(self, data_inputs, expected_outputs)
     return self, np.log(data_inputs)
Exemple #17
0
def create_test_cases():
    data_inputs = np.ones((1, 1))
    expected_outputs = np.ones((1, 1))
    dc = DataContainer(data_inputs=data_inputs,
                       current_ids=range(len(data_inputs)),
                       expected_outputs=expected_outputs)

    tape = TapeCallbackFunction()
    tape_fit = TapeCallbackFunction()
    tape_without_checkpoint_test_arguments = ResumablePipelineTestCase(
        tape, data_inputs, expected_outputs,
        [("a", FitTransformCallbackStep(tape.callback, tape_fit.callback,
                                        ["1"])),
         ("b", FitTransformCallbackStep(tape.callback, tape_fit.callback,
                                        ["2"])),
         ("c", FitTransformCallbackStep(tape.callback, tape_fit.callback,
                                        ["3"]))], ["1", "2", "3"])

    tape2 = TapeCallbackFunction()
    tape2_fit = TapeCallbackFunction()
    tape_checkpoint_not_saved_test_arguments = ResumablePipelineTestCase(
        tape2, data_inputs, expected_outputs,
        [("a",
          FitTransformCallbackStep(tape2.callback, tape2_fit.callback, ["1"])),
         ("b", SomeCheckpointStep(data_container=None)),
         ("c",
          FitTransformCallbackStep(tape2.callback, tape2_fit.callback, ["2"])),
         ("d",
          FitTransformCallbackStep(tape2.callback, tape2_fit.callback, ["3"]))
         ], ["1", "2", "3"])

    tape3 = TapeCallbackFunction()
    tape3_fit = TapeCallbackFunction()
    tape_checkpoint_saved_after_first_step_test_arguments = ResumablePipelineTestCase(
        tape3, data_inputs, expected_outputs,
        [("a",
          FitTransformCallbackStep(tape3.callback, tape3_fit.callback, ["1"])),
         ("b", SomeCheckpointStep(data_container=dc)),
         ("c",
          FitTransformCallbackStep(tape3.callback, tape3_fit.callback, ["2"])),
         ("d",
          FitTransformCallbackStep(tape3.callback, tape3_fit.callback, ["3"]))
         ], ["2", "3"])

    tape4 = TapeCallbackFunction()
    tape4_fit = TapeCallbackFunction()
    tape_checkpoint_saved_after_second_step_test_arguments = ResumablePipelineTestCase(
        tape4, data_inputs, expected_outputs,
        [("a",
          FitTransformCallbackStep(tape4.callback, tape4_fit.callback, ["1"])),
         ("b",
          FitTransformCallbackStep(tape4.callback, tape4_fit.callback, ["2"])),
         ("c", SomeCheckpointStep(data_container=dc)),
         ("d",
          FitTransformCallbackStep(tape4.callback, tape4_fit.callback, ["3"]))
         ], ["3"])

    tape5 = TapeCallbackFunction()
    tape5_fit = TapeCallbackFunction()
    tape_checkpoint_saved_after_last_step_test_arguments = ResumablePipelineTestCase(
        tape5, data_inputs, expected_outputs, [
            ("a",
             FitTransformCallbackStep(tape5.callback, tape5_fit.callback,
                                      ["1"])),
            ("b",
             FitTransformCallbackStep(tape5.callback, tape5_fit.callback,
                                      ["2"])),
            ("c",
             FitTransformCallbackStep(tape5.callback, tape5_fit.callback,
                                      ["3"])),
            ("d", SomeCheckpointStep(data_container=dc)),
        ], [])

    tape6 = TapeCallbackFunction()
    tape6_fit = TapeCallbackFunction()
    tape_checkpoint_saved_inside_subpipeline_last_step = ResumablePipelineTestCase(
        tape6, data_inputs, expected_outputs, [
            ("a",
             FitTransformCallbackStep(tape6.callback, tape6_fit.callback,
                                      ["1"])),
            ResumablePipeline([
                ("b",
                 FitTransformCallbackStep(tape6.callback, tape6_fit.callback,
                                          ["2"])),
                ("d", SomeCheckpointStep(data_container=dc)),
            ]),
            ("e",
             FitTransformCallbackStep(tape6.callback, tape6_fit.callback,
                                      ["3"])),
            ("f",
             FitTransformCallbackStep(tape6.callback, tape6_fit.callback,
                                      ["4"])),
        ], ["3", "4"])

    tape7 = TapeCallbackFunction()
    tape7_fit = TapeCallbackFunction()
    tape_checkpoint_saved_inside_subpipeline_first_step = ResumablePipelineTestCase(
        tape7, data_inputs, expected_outputs, [
            ("a",
             FitTransformCallbackStep(tape7.callback, tape7_fit.callback,
                                      ["1"])),
            ResumablePipeline([
                ("d", SomeCheckpointStep(data_container=dc)),
                ("b",
                 FitTransformCallbackStep(tape7.callback, tape7_fit.callback,
                                          ["2"])),
            ]),
            ("e",
             FitTransformCallbackStep(tape7.callback, tape7_fit.callback,
                                      ["3"])),
            ("f",
             FitTransformCallbackStep(tape7.callback, tape7_fit.callback,
                                      ["4"])),
        ], ["2", "3", "4"])

    tape8 = TapeCallbackFunction()
    tape8_fit = TapeCallbackFunction()
    tape_checkpoint_saved_inside_subpipeline_step_in_the_middle = ResumablePipelineTestCase(
        tape8, data_inputs, expected_outputs, [
            ("a",
             FitTransformCallbackStep(tape8.callback, tape8_fit.callback,
                                      ["1"])),
            ResumablePipeline([
                ("b",
                 FitTransformCallbackStep(tape8.callback, tape8_fit.callback,
                                          ["2"])),
                ("d", SomeCheckpointStep(data_container=dc)),
                ("e",
                 FitTransformCallbackStep(tape8.callback, tape8_fit.callback,
                                          ["3"])),
            ]),
            ("f",
             FitTransformCallbackStep(tape8.callback, tape8_fit.callback,
                                      ["4"])),
        ], ["3", "4"])

    tape9 = TapeCallbackFunction()
    tape9_fit = TapeCallbackFunction()
    tape_checkpoint_saved_inside_subpipeline_of_subpipeline = ResumablePipelineTestCase(
        tape9, data_inputs, expected_outputs, [
            ("a",
             FitTransformCallbackStep(tape9.callback, tape9_fit.callback,
                                      ["1"])),
            ResumablePipeline([
                ("b",
                 FitTransformCallbackStep(tape9.callback, tape9_fit.callback,
                                          ["2"])),
                ResumablePipeline([
                    ("e",
                     FitTransformCallbackStep(tape9.callback,
                                              tape9_fit.callback, ["3"])),
                    ("d", SomeCheckpointStep(data_container=dc)),
                    ("f",
                     FitTransformCallbackStep(tape9.callback,
                                              tape9_fit.callback, ["4"])),
                ]),
                ("g",
                 FitTransformCallbackStep(tape9.callback, tape9_fit.callback,
                                          ["5"])),
            ]),
            ("h",
             FitTransformCallbackStep(tape9.callback, tape9_fit.callback,
                                      ["6"])),
        ], ["4", "5", "6"])

    tape10 = TapeCallbackFunction()
    tape10_fit = TapeCallbackFunction()
    tape_saved_checkpoint_after_another_saved_checkpoint = ResumablePipelineTestCase(
        tape10, data_inputs, expected_outputs,
        [("a",
          FitTransformCallbackStep(tape10.callback, tape10_fit.callback,
                                   ["1"])),
         ("b", SomeCheckpointStep(data_container=dc)),
         ("c",
          FitTransformCallbackStep(tape10.callback, tape10_fit.callback,
                                   ["2"])),
         ("b", SomeCheckpointStep(data_container=dc)),
         ("d",
          FitTransformCallbackStep(tape10.callback, tape10_fit.callback,
                                   ["3"]))], ["3"])

    tape11 = TapeCallbackFunction()
    tape11_fit = TapeCallbackFunction()
    tape_multiple_checkpoint_in_a_row = ResumablePipelineTestCase(
        tape11, data_inputs, expected_outputs,
        [("a",
          FitTransformCallbackStep(tape11.callback, tape11_fit.callback,
                                   ["1"])),
         ("joblib_1", SomeCheckpointStep(data_container=dc)),
         ("joblib_2", SomeCheckpointStep(data_container=dc)),
         ("c",
          FitTransformCallbackStep(tape11.callback, tape11_fit.callback,
                                   ["2"])),
         ("d",
          FitTransformCallbackStep(tape11.callback, tape11_fit.callback,
                                   ["3"]))], ["2", "3"])

    return [
        tape_without_checkpoint_test_arguments,
        tape_checkpoint_not_saved_test_arguments,
        tape_checkpoint_saved_after_first_step_test_arguments,
        tape_checkpoint_saved_after_second_step_test_arguments,
        tape_checkpoint_saved_after_last_step_test_arguments,
        tape_checkpoint_saved_inside_subpipeline_first_step,
        tape_checkpoint_saved_inside_subpipeline_last_step,
        tape_checkpoint_saved_inside_subpipeline_step_in_the_middle,
        tape_checkpoint_saved_inside_subpipeline_of_subpipeline,
        tape_saved_checkpoint_after_another_saved_checkpoint,
        tape_multiple_checkpoint_in_a_row
    ]
Exemple #18
0
from neuraxle.hyperparams.space import HyperparameterSpace
from neuraxle.metaopt.auto_ml import InMemoryHyperparamsRepository, AutoML, ValidationSplitter, \
    RandomSearchHyperparameterSelectionStrategy, BaseHyperparameterSelectionStrategy
from neuraxle.metaopt.callbacks import MetricCallback, ScoringCallback
from neuraxle.metaopt.tpe import TreeParzenEstimatorHyperparameterSelectionStrategy
from neuraxle.pipeline import Pipeline
from neuraxle.steps.misc import FitTransformCallbackStep
from neuraxle.steps.numpy import AddN
import os


@pytest.mark.parametrize(
    "expected_output_mult, pipeline",
    [(3.5,
      Pipeline([
          FitTransformCallbackStep().set_name('callback'),
          AddN(0.).set_hyperparams_space(
              HyperparameterSpace({
                  'add':
                  Choice(choice_list=[0, 1.5, 2, 3.5, 4, 5, 6]),
              })),
          AddN(0.).set_hyperparams_space(
              HyperparameterSpace({
                  'add':
                  Choice(choice_list=[0, 1.5, 2, 3.5, 4, 5, 6]),
              }))
      ])),
     (3.5,
      Pipeline([
          FitTransformCallbackStep().set_name('callback'),
          AddN(0.).set_hyperparams_space(
    def fit_transform(self, data_inputs, expected_outputs=None):
        FitTransformCallbackStep.fit_transform(self, data_inputs,
                                               expected_outputs)

        return self, list(np.array(data_inputs) * 2)
Exemple #20
0
from neuraxle.steps.flow import TrainOrTestOnlyWrapper
from neuraxle.steps.misc import TapeCallbackFunction, FitTransformCallbackStep
from testing.steps.neuraxle_test_case import NeuraxleTestCase

DATA_INPUTS = np.array([1])
EXPECTED_OUTPUTS = np.array([1])

tape_transform = TapeCallbackFunction()
tape_fit = TapeCallbackFunction()


@pytest.mark.parametrize('test_case', [
    NeuraxleTestCase(pipeline=Pipeline([
        TrainOrTestOnlyWrapper(
            FitTransformCallbackStep(tape_transform,
                                     tape_fit,
                                     transform_function=lambda di: di * 2))
    ]),
                     more_arguments={'set_train': True},
                     callbacks=[tape_transform, tape_fit],
                     expected_callbacks_data=[[DATA_INPUTS]],
                     data_inputs=DATA_INPUTS,
                     expected_processed_outputs=DATA_INPUTS * 2,
                     execution_mode=ExecutionMode.TRANSFORM),
    NeuraxleTestCase(pipeline=Pipeline([
        TrainOrTestOnlyWrapper(
            FitTransformCallbackStep(tape_transform,
                                     tape_fit,
                                     transform_function=lambda di: di * 2))
    ]),
                     more_arguments={'set_train': True},
Exemple #21
0
from neuraxle.base import ExecutionMode
from neuraxle.pipeline import Pipeline
from neuraxle.steps.flow import TrainOrTestOnlyWrapper, TestOnlyWrapper
from neuraxle.steps.misc import TapeCallbackFunction, FitTransformCallbackStep
from testing.steps.neuraxle_test_case import NeuraxleTestCase

DATA_INPUTS = np.array([1])
EXPECTED_OUTPUTS = np.array([1])

tape_transform = TapeCallbackFunction()
tape_fit = TapeCallbackFunction()


@pytest.mark.parametrize('test_case', [
    NeuraxleTestCase(
        pipeline=TrainOrTestOnlyWrapper(FitTransformCallbackStep(tape_transform, tape_fit, transform_function=lambda di: di * 2)),
        more_arguments={'set_train': True},
        callbacks=[tape_transform, tape_fit],
        expected_callbacks_data=[[DATA_INPUTS]],
        data_inputs=DATA_INPUTS,
        expected_processed_outputs=DATA_INPUTS * 2,
        execution_mode=ExecutionMode.TRANSFORM
    ),
    NeuraxleTestCase(
        pipeline=TrainOrTestOnlyWrapper(FitTransformCallbackStep(tape_transform, tape_fit, transform_function=lambda di: di * 2)),
        more_arguments={'set_train': True},
        callbacks=[tape_transform, tape_fit],
        data_inputs=DATA_INPUTS,
        expected_outputs=EXPECTED_OUTPUTS,
        expected_callbacks_data=[[DATA_INPUTS], [(DATA_INPUTS, EXPECTED_OUTPUTS)]],
        expected_processed_outputs=DATA_INPUTS * 2,
Exemple #22
0
from neuraxle.pipeline import Pipeline
from neuraxle.steps.data import EpochRepeater
from neuraxle.steps.misc import TapeCallbackFunction, FitTransformCallbackStep
from testing.steps.neuraxle_test_case import NeuraxleTestCase

DATA_INPUTS = np.array(range(10))
EXPECTED_OUTPUTS = np.array(range(10, 20))

callback_fit = TapeCallbackFunction()
callback_transform = TapeCallbackFunction()
EPOCHS = 2


@pytest.mark.parametrize("test_case", [
    NeuraxleTestCase(pipeline=Pipeline([
        EpochRepeater(FitTransformCallbackStep(callback_transform,
                                               callback_fit),
                      epochs=EPOCHS)
    ]),
                     callbacks=[callback_fit, callback_transform],
                     expected_callbacks_data=[[(DATA_INPUTS, EXPECTED_OUTPUTS),
                                               (DATA_INPUTS, EXPECTED_OUTPUTS)
                                               ], [DATA_INPUTS]],
                     data_inputs=DATA_INPUTS,
                     expected_outputs=EXPECTED_OUTPUTS,
                     expected_processed_outputs=DATA_INPUTS,
                     execution_mode=ExecutionMode.FIT_TRANSFORM),
    NeuraxleTestCase(pipeline=Pipeline([
        EpochRepeater(FitTransformCallbackStep(callback_transform,
                                               callback_fit),
                      epochs=EPOCHS)
    ]),