예제 #1
0
def test_apply_on_pipeline_with_positional_argument_should_call_method_on_each_steps():
    pipeline = Pipeline([MultiplyByN(1), MultiplyByN(1)])

    pipeline.apply('set_hyperparams', hyperparams=HyperparameterSamples({'multiply_by': 2}))

    assert pipeline.get_hyperparams()['multiply_by'] == 2
    assert pipeline['MultiplyByN'].get_hyperparams()['multiply_by'] == 2
    assert pipeline['MultiplyByN1'].get_hyperparams()['multiply_by'] == 2
예제 #2
0
def test_apply_on_pipeline_with_meta_step_and_positional_argument_should_call_method_on_each_steps():
    pipeline = Pipeline([OutputTransformerWrapper(MultiplyByN(1)), MultiplyByN(1)])

    pipeline.apply('set_hyperparams', hyperparams=HyperparameterSamples({'multiply_by': 2}))

    assert pipeline.get_hyperparams()['multiply_by'] == 2
    assert pipeline['OutputTransformerWrapper'].wrapped.get_hyperparams()['multiply_by'] == 2
    assert pipeline['MultiplyByN'].get_hyperparams()['multiply_by'] == 2
예제 #3
0
def test_apply_method_on_pipeline_should_call_method_on_each_steps():
    pipeline = Pipeline([MultiplyByN(1), MultiplyByN(1)])

    pipeline.apply(lambda step: step._set_hyperparams(
        HyperparameterSamples({'multiply_by': 2})))

    assert pipeline.get_hyperparams()['multiply_by'] == 2
    assert pipeline['MultiplyByN'].get_hyperparams()['multiply_by'] == 2
    assert pipeline['MultiplyByN1'].get_hyperparams()['multiply_by'] == 2
예제 #4
0
def test_feature_union_should_apply_to_self_and_sub_steps():
    p = Pipeline(
        [FeatureUnion([
            Identity(),
            Identity(),
        ], joiner=NumpyTranspose())])

    p.apply(lambda step: step._set_hyperparams(
        HyperparameterSamples({'applied': True})))

    assert p.hyperparams['applied']
    assert p['FeatureUnion'].hyperparams['applied']
    assert p['FeatureUnion'][0].hyperparams['applied']
    assert p['FeatureUnion'][1].hyperparams['applied']
    assert p['FeatureUnion'][2].hyperparams['applied']
예제 #5
0
def test_apply_method_on_pipeline_with_meta_step_should_call_method_on_each_steps(
):
    pipeline = Pipeline(
        [OutputTransformerWrapper(MultiplyByN(1)),
         MultiplyByN(1)])

    pipeline.apply(lambda step: step._set_hyperparams(
        HyperparameterSamples({'multiply_by': 2})))

    assert pipeline.get_hyperparams()['multiply_by'] == 2
    assert pipeline['OutputTransformerWrapper'].get_hyperparams(
    )['multiply_by'] == 2
    assert pipeline['OutputTransformerWrapper'].wrapped.get_hyperparams(
    )['multiply_by'] == 2
    assert pipeline['MultiplyByN'].get_hyperparams()['multiply_by'] == 2
예제 #6
0
def main():
    p = Pipeline([
        IdentityWithRvs().set_hyperparams_space(
            HyperparameterSpace({'a': randint(low=2, high=5)})),
        IdentityWithRvs().set_hyperparams_space(
            HyperparameterSpace({'b': randint(low=100, high=400)}))
    ])

    samples: HyperparameterSamples = p.apply(rvs)
    print('p.apply(rvs) ==>')
    print(json.dumps(samples, indent=4))

    # or equivalently:

    samples: HyperparameterSamples = p.apply('_rvs')
    print('p.apply(\'_rvs\') ==>')
    print(json.dumps(samples, indent=4))
예제 #7
0
def test_has_children_mixin_apply_should_apply_method_to_recursive_childrends(
):
    p = Pipeline([
        ('a', Identity()),
        ('b', Identity()),
        Pipeline([('c', Identity()), ('d', Identity())]),
    ])

    p.apply('_set_hyperparams',
            ra=None,
            hyperparams=HyperparameterSamples({
                'Pipeline__c__hp': 3,
                'Pipeline__d__hp': 4
            }))

    assert p['Pipeline']['c'].hyperparams.to_flat_dict()['hp'] == 3
    assert p['Pipeline']['d'].hyperparams.to_flat_dict()['hp'] == 4
예제 #8
0
def test_has_children_mixin_apply_should_apply_method_to_direct_childrends():
    p = Pipeline([
        ('a', Identity()),
        ('b', Identity()),
        Pipeline([('c', Identity()), ('d', Identity())]),
    ])

    p.apply('_set_hyperparams',
            ra=None,
            hyperparams=HyperparameterSamples({
                'a__hp': 0,
                'b__hp': 1,
                'Pipeline__hp': 2
            }))

    assert p['a'].hyperparams.to_flat_as_dict_primitive()['hp'] == 0
    assert p['b'].hyperparams.to_flat_as_dict_primitive()['hp'] == 1
    assert p['Pipeline'].hyperparams.to_flat_as_dict_primitive()['hp'] == 2
예제 #9
0
def test_has_children_mixin_apply_should_return_recursive_dict_to_direct_childrends(
):
    p = Pipeline([
        ('a', Identity().set_hyperparams(HyperparameterSamples({'hp': 0}))),
        ('b', Identity().set_hyperparams(HyperparameterSamples({'hp': 1})))
    ])

    results = p.apply('_get_hyperparams', ra=None)

    assert results.to_flat_as_dict_primitive()['a__hp'] == 0
    assert results.to_flat_as_dict_primitive()['b__hp'] == 1
예제 #10
0
def test_apply_on_pipeline_with_meta_step_and_positional_argument():
    pipeline = Pipeline(
        [OutputTransformerWrapper(MultiplyByN(1)),
         MultiplyByN(1)])

    pipeline.apply('_set_hyperparams',
                   hyperparams=HyperparameterSamples({
                       'multiply_by':
                       2,
                       'OutputTransformerWrapper__multiply_by':
                       3,
                       'OutputTransformerWrapper__MultiplyByN__multiply_by':
                       4,
                       'MultiplyByN__multiply_by':
                       5
                   }))

    assert pipeline.get_hyperparams()['multiply_by'] == 2
    assert pipeline['OutputTransformerWrapper'].get_hyperparams(
    )['multiply_by'] == 3
    assert pipeline['OutputTransformerWrapper'].wrapped.get_hyperparams(
    )['multiply_by'] == 4
    assert pipeline['MultiplyByN'].get_hyperparams()['multiply_by'] == 5
예제 #11
0
def test_has_children_mixin_apply_should_return_recursive_dict_to_recursive_childrends(
):
    p = Pipeline([
        Pipeline([
            ('c', Identity().set_hyperparams(HyperparameterSamples({'hp':
                                                                    3}))),
            ('d', Identity().set_hyperparams(HyperparameterSamples({'hp': 4})))
        ]).set_hyperparams(HyperparameterSamples({'hp': 2})),
    ])

    results = p.apply('_get_hyperparams', ra=None)

    assert results['Pipeline__hp'] == 2
    assert results['Pipeline__c__hp'] == 3
    assert results['Pipeline__d__hp'] == 4
예제 #12
0
def main(chosen_device):
    exercice_number = 1
    print('exercice {}\n=================='.format(exercice_number))

    data_inputs, expected_outputs = generate_data(
        # See: https://github.com/guillaume-chevalier/seq2seq-signal-prediction/blob/master/datasets.py
        exercice_number=exercice_number,
        n_samples=None,
        window_size_past=None,
        window_size_future=None)

    print('data_inputs shape: {} => (n_samples, window_size_past, input_dim)'.
          format(data_inputs.shape))
    print(
        'expected_outputs shape: {} => (n_samples, window_size_future, output_dim)'
        .format(expected_outputs.shape))

    sequence_length = data_inputs.shape[1]
    input_dim = data_inputs.shape[2]
    output_dim = expected_outputs.shape[2]

    batch_size = 100
    epochs = 3
    validation_size = 0.15
    max_plotted_validation_predictions = 10

    seq2seq_pipeline_hyperparams = HyperparameterSamples({
        'hidden_dim':
        100,
        'layers_stacked_count':
        2,
        'lambda_loss_amount':
        0.0003,
        'learning_rate':
        0.006,
        'window_size_future':
        sequence_length,
        'output_dim':
        output_dim,
        'input_dim':
        input_dim
    })

    pipeline = Pipeline([
        MiniBatchSequentialPipeline(
            [
                ForEachDataInput(MeanStdNormalizer()),
                ToNumpy(),
                PlotPredictionsWrapper(
                    Tensorflow2ModelStep(
                        # See: https://github.com/Neuraxio/Neuraxle-TensorFlow
                        create_model=create_model,
                        create_loss=create_loss,
                        create_optimizer=create_optimizer,
                        expected_outputs_dtype=tf.dtypes.float32,
                        data_inputs_dtype=tf.dtypes.float32,
                        device_name=chosen_device,
                        print_loss=True).set_hyperparams(
                            seq2seq_pipeline_hyperparams))
            ],
            batch_size=batch_size),
    ]).set_name('SignalPrediction')

    trainer = Trainer(
        epochs=epochs,
        validation_splitter=ValidationSplitter(test_size=validation_size),
        scoring_callback=ScoringCallback(
            metric_function=metric_3d_to_2d_wrapper(mean_squared_error),
            higher_score_is_better=False))

    trial: Trial = trainer.train(pipeline=pipeline,
                                 data_inputs=data_inputs,
                                 expected_outputs=expected_outputs)

    plot_metrics(
        metric_name='mse',
        train_values=trial.validation_splits[0].metrics_results['main']
        ['train_values'],
        validation_values=trial.validation_splits[0].metrics_results['main']
        ['validation_values'],
        exercice_number=exercice_number)

    # Get trained pipeline
    pipeline = trial.get_trained_pipeline(split_number=0)

    # Get validation set with trainer.validation_split_function.split function.
    _, _, data_inputs_validation, expected_outputs_validation = trainer.validation_split_function.split(
        data_inputs=data_inputs, expected_outputs=expected_outputs)

    # Enable the plotting feature inside the PlotPredictionsWrapper wrapper step.
    pipeline.apply('toggle_plotting')
    pipeline.apply(method='set_max_plotted_predictions',
                   max_plotted_predictions=max_plotted_validation_predictions)

    # Transform the trained pipeline to plot predictions
    pipeline.transform_data_container(
        DataContainer(data_inputs=data_inputs_validation[0],
                      expected_outputs=expected_outputs_validation[0]))