Example #1
0
def test_enforce_int_delta_threshold_le_output_dim():
    x = numpy.random.normal(size=(300, 15))
    # No automatic stop_training
    n = iGSFANode(output_dim=5, reconstruct_with_sfa=False, slow_feature_scaling_method=None, delta_threshold=6)
    n.train(x, train_mode="regular")
    n.train(x**3, train_mode="regular")
    with pytest.raises(Exception):
        n.stop_training()
    # Automatic stop_training
    n = iGSFANode(output_dim=5, reconstruct_with_sfa=True, slow_feature_scaling_method=None, delta_threshold=6)
    with pytest.raises(Exception):
        n.train(x, train_mode="regular")
Example #2
0
def test_no_automatic_stop_training():
    """ Test that verifies that iGSFA does not call stop training when when multiple-train is used
    """
    x = numpy.random.normal(size=(300, 15))
    n = iGSFANode(output_dim=5, reconstruct_with_sfa=False, slow_feature_scaling_method=None)
    n.train(x, train_mode="regular")
    n.train(x, train_mode="regular")
    n.stop_training()

    n = iGSFANode(output_dim=5, reconstruct_with_sfa=False, slow_feature_scaling_method="data_dependent")
    n.train(x, train_mode="regular")
    n.train(x, train_mode="regular")
    n.stop_training()
Example #3
0
def test_slow_feature_scaling_methods():
    """ Test that executes each feature scaling method and verifies that (most of them) only change the
    scale of the slow features in the slow part but do not mix them.
    """
    x = numpy.random.normal(size=(300, 15))

    all_slow_feature_scaling_methods = ["QR_decomposition", "sensitivity_based", None, "data_dependent",
                                        "data_dependent2"]
    num_slow_feature_scaling_methods = len(all_slow_feature_scaling_methods)
    output_features = []
    for slow_feature_scaling_method in all_slow_feature_scaling_methods:
        n = iGSFANode(output_dim=15, reconstruct_with_sfa=True, slow_feature_scaling_method=slow_feature_scaling_method)
        n.train(x, train_mode="regular")
        if n.is_training():
            n.stop_training()
        output_features.append(n.execute(x))


    size_slow_part = n.sfa_node.output_dim
    print("size_slow_part:", size_slow_part)
    for i in range(num_slow_feature_scaling_methods):
        output_features[i] = output_features[i][:,:size_slow_part]
    first_sample_y_data_dependent = output_features[num_slow_feature_scaling_methods-1][0]
    for i in range(1, len(all_slow_feature_scaling_methods)-1):
        print("checking feature equivalence between", all_slow_feature_scaling_methods[i], "and",
              all_slow_feature_scaling_methods[num_slow_feature_scaling_methods-1])
        first_sample_y_i = output_features[i][0]
        y = output_features[i] * first_sample_y_data_dependent / first_sample_y_i
        assert (y - output_features[num_slow_feature_scaling_methods-1]) == pytest.approx(0.0)
Example #4
0
def test_automatic_stop_training():
    """ Test that verifies that iGSFA automatically calls stop training when trained on single batch mode
    """
    x = numpy.random.normal(size=(300, 15))

    n = iGSFANode(output_dim=15, reconstruct_with_sfa=True, slow_feature_scaling_method=None)
    n.train(x, train_mode="regular")
    with pytest.raises(mdp.TrainingFinishedException):
        n.train(x, train_mode="regular")

    n = iGSFANode(output_dim=15, reconstruct_with_sfa=True, slow_feature_scaling_method="data_dependent")
    n.train(x, train_mode="regular")
    with pytest.raises(mdp.TrainingFinishedException):
        n.train(x, train_mode="regular")

    n = iGSFANode(output_dim=15, reconstruct_with_sfa=True, slow_feature_scaling_method="sensitivity_based")
    n.train(x, train_mode="regular")
    with pytest.raises(mdp.TrainingFinishedException):
        n.train(x, train_mode="regular")

    n = iGSFANode(output_dim=15, reconstruct_with_sfa=True, slow_feature_scaling_method="QR_decomposition")
    n.train(x, train_mode="regular")
    with pytest.raises(mdp.TrainingFinishedException):
        n.train(x, train_mode="regular")
Example #5
0
def test_equivalence_GSFA_PCA_for_DT_0():
    """ Test of iGSFA and PCA when delta_threshold is smaller than 0.0
    """
    x = numpy.random.normal(size=(300, 15))

    n = iGSFANode(output_dim=5, slow_feature_scaling_method=None, delta_threshold=0.0)
    n.train(x, train_mode="regular")
    # n.stop_training() has been automatically called

    y = n.execute(x)
    deltas_igsfa = comp_delta(y)

    n2 = mdp.nodes.PCANode(output_dim=5)
    n2.train(x)
    n2.stop_training()

    y2 = n2.execute(x)
    deltas_pca = comp_delta(y2)
    assert (deltas_igsfa - deltas_pca) == pytest.approx(0.0)