Exemple #1
0
def test_bad_n_components():
    with pytest.raises(ValueError):
        dcca = DCCA(3, 5, 5.5, [10, 10], [10, 10])
    with pytest.raises(ValueError):
        dcca = DCCA(3, 5, 0, [10, 10], [10, 10])
    with pytest.raises(ValueError):
        dcca = DCCA(10, 10, 4, [10, 2], [10, 3])
Exemple #2
0
def test_bad_layer_sizes():
    with pytest.raises(ValueError):
        layer_sizes1, layer_sizes2 = [10, 0], [10, 10]
        dcca = DCCA(3, 5, 2, layer_sizes1, layer_sizes2)
    with pytest.raises(ValueError):
        layer_sizes1, layer_sizes2 = [10, 5.4], [10, 10]
        dcca = DCCA(3, 5, 2, layer_sizes1, layer_sizes2)
    with pytest.raises(ValueError):
        layer_sizes1, layer_sizes2 = [10, 5], [0, 10]
        dcca = DCCA(3, 5, 2, layer_sizes1, layer_sizes2)
    with pytest.raises(ValueError):
        layer_sizes1, layer_sizes2 = [10, 5], [1.9, 10]
        dcca = DCCA(3, 5, 2, layer_sizes1, layer_sizes2)
    with pytest.raises(ValueError):
        layer_sizes1, layer_sizes2 = [10, 5], np.array([10, 10])
        dcca = DCCA(3, 5, 2, layer_sizes1, layer_sizes2)
    with pytest.raises(ValueError):
        layer_sizes1, layer_sizes2 = np.array([10, 10]), [10, 10]
        dcca = DCCA(3, 5, 2, layer_sizes1, layer_sizes2)
    with pytest.raises(ValueError):
        layer_sizes1, layer_sizes2 = (10, 5, 4)
        dcca = DCCA(3, 5, 2, layer_sizes1, layer_sizes2)
    with pytest.raises(ValueError):
        layer_sizes1, layer_sizes2 = [10, 5], [10, 10]
        dcca = DCCA(3, 5, 2, layer_sizes1, layer_sizes2)
Exemple #3
0
def data():
    random_seed = 10
    N = 600
    input_size1, input_size2 = 100, 100
    sin_transform = []
    np.random.seed(random_seed)
    sin_transform.append(20 * np.random.rand(N, input_size1))
    sin_transform.append(np.sin(sin_transform[0]))
    layer_sizes1 = [1024, 50]
    layer_sizes2 = [1024, 50]
    dcca_50_2 = DCCA(input_size1,
                     input_size2,
                     n_components=2,
                     layer_sizes1=[1024, 50],
                     layer_sizes2=[1024, 50])
    dcca_print = DCCA(input_size1,
                      input_size2,
                      n_components=2,
                      layer_sizes1=[1024, 50],
                      layer_sizes2=[1024, 50],
                      print_train_log_info=True)
    dcca_low_epochs = DCCA(input_size1,
                           input_size2,
                           n_components=2,
                           layer_sizes1=[1024, 80],
                           layer_sizes2=[1024, 80],
                           epoch_num=10)
    dcca_use_all = DCCA(input_size1,
                        input_size2,
                        n_components=2,
                        layer_sizes1=[1024, 80],
                        layer_sizes2=[1024, 80],
                        use_all_singular_values=True)
    dcca_1layer = DCCA(input_size1, input_size2, n_components=2)

    return {
        'N': N,
        'input_size1': input_size1,
        'input_size2': input_size2,
        'layer_sizes1': layer_sizes1,
        'layer_sizes2': layer_sizes2,
        'sin_transform': sin_transform,
        'random_seed': random_seed,
        'dcca_50_2': dcca_50_2,
        'dcca_low_epochs': dcca_low_epochs,
        'dcca_print': dcca_print,
        'dcca_use_all': dcca_use_all,
        'dcca_1layer': dcca_1layer
    }
Exemple #4
0
def test_bad_tolerance(data):
    with pytest.raises(ValueError):
        dcca = DCCA(5,
                    5,
                    2,
                    data['layer_sizes1'],
                    data['layer_sizes2'],
                    tolerance=0)
    with pytest.raises(ValueError):
        dcca = DCCA(5,
                    5,
                    2,
                    data['layer_sizes1'],
                    data['layer_sizes2'],
                    tolerance=-4)
Exemple #5
0
def test_bad_reg_par(data):
    with pytest.raises(ValueError):
        dcca = DCCA(5,
                    5,
                    2,
                    data['layer_sizes1'],
                    data['layer_sizes2'],
                    reg_par=0)
    with pytest.raises(ValueError):
        dcca = DCCA(5,
                    5,
                    2,
                    data['layer_sizes1'],
                    data['layer_sizes2'],
                    reg_par=-4)
Exemple #6
0
def test_bad_learning_rate(data):
    with pytest.raises(ValueError):
        dcca = DCCA(5,
                    5,
                    2,
                    data['layer_sizes1'],
                    data['layer_sizes2'],
                    learning_rate=0)
    with pytest.raises(ValueError):
        dcca = DCCA(5,
                    5,
                    2,
                    data['layer_sizes1'],
                    data['layer_sizes2'],
                    learning_rate=-4)
Exemple #7
0
def test_bad_batch_size(data):
    with pytest.raises(ValueError):
        dcca = DCCA(5,
                    5,
                    2,
                    data['layer_sizes1'],
                    data['layer_sizes2'],
                    batch_size=0)
    with pytest.raises(ValueError):
        dcca = DCCA(5,
                    5,
                    2,
                    data['layer_sizes1'],
                    data['layer_sizes2'],
                    batch_size=91.9)
Exemple #8
0
def test_bad_epoch_num(data):
    with pytest.raises(ValueError):
        dcca = DCCA(5,
                    5,
                    2,
                    data['layer_sizes1'],
                    data['layer_sizes2'],
                    epoch_num=0)
    with pytest.raises(ValueError):
        dcca = DCCA(5,
                    5,
                    2,
                    data['layer_sizes1'],
                    data['layer_sizes2'],
                    epoch_num=91.9)
Exemple #9
0
def test_bad_input_size(data):
    # incorrect size
    with pytest.raises(ValueError):
        bad_data1 = []
        bad_data1.append(np.random.rand(50, 10))
        bad_data1.append(np.random.rand(50, data['input_size2']))
        #dcca_50_2.fit(bad_data1)
        data['dcca_50_2'].fit(bad_data1)
    with pytest.raises(ValueError):
        bad_data1 = []
        bad_data1.append(np.random.rand(50, data['input_size1']))
        bad_data1.append(np.random.rand(50, 5))
        # dcca_50_2.fit(bad_data1)
        data['dcca_50_2'].fit(bad_data1)
    with pytest.raises(ValueError):
        dcca = DCCA(0, 5, 2, [10, 10], [10, 10])
    with pytest.raises(ValueError):
        dcca = DCCA(2, 0, 2, [10, 10], [10, 10])
    with pytest.raises(ValueError):
        dcca = DCCA(0.6, 5, 2, [10, 10], [10, 10])
    with pytest.raises(ValueError):
        dcca = DCCA(2, 10.1, 2, [10, 10], [10, 10])
Exemple #10
0
    ['Raw Views', 'CCA', 'Polynomial KCCA', 'Gaussian KCCA', 'DCCA']
transform_labels = \
    ['Linear Transform', 'Polynomial Transform', 'Sinusoidal Transform']

input_size1 = Xs_train_sets[0][0].shape[1]
input_size2 = Xs_train_sets[0][1].shape[1]
outdim_size = min(Xs_train_sets[0][0].shape[1], 2)
layer_sizes1 = [256, 256, outdim_size]
layer_sizes2 = [256, 256, outdim_size]
methods = [
    CCA(regs=0.1, n_components=2),
    KMCCA(kernel='poly', regs=0.1, kernel_params={'degree': 2, 'coef0': 0.1},
          n_components=2),
    KMCCA(kernel='rbf', regs=0.1, kernel_params={'gamma': 1/4},
          n_components=2),
    DCCA(input_size1, input_size2, outdim_size, layer_sizes1, layer_sizes2,
         epoch_num=400)
]

fig, axes = plt.subplots(3 * 2, 5 * 2, figsize=(22, 12))
sns.set_context('notebook')

for r, transform in enumerate(transforms):
    axs = axes[2 * r:2 * r + 2, :2]
    for i, ax in enumerate(axs.flatten()):
        dim2 = int(i / 2)
        dim1 = i % 2
        ax.scatter(
            Xs_test_sets[r][0][:, dim1],
            Xs_test_sets[r][1][:, dim2],
            cmap=cmap,
            c=labels,
Exemple #11
0
###############################################################################
# Fit DCCA model to uncover latent distribution
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# The output dimensionality is still 4.

# Define parameters and layers for deep model
features1 = Xs_train[0].shape[1]  # Feature sizes
features2 = Xs_train[1].shape[1]
layers1 = [256, 256, 4]  # nodes in each hidden layer and the output size
layers2 = [256, 256, 4]

dcca = DCCA(input_size1=features1,
            input_size2=features2,
            n_components=4,
            layer_sizes1=layers1,
            layer_sizes2=layers2,
            epoch_num=500)
dcca.fit(Xs_train)
Xs_transformed = dcca.transform(Xs_test)

###############################################################################
# Visualize the transformed data
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# We can see that it has uncovered the latent correlation between views.

crossviews_plot(Xs_transformed,
                labels=y_test,
                title='Transformed Testing Data View 1 vs. View 2 '
                '(Polynomial Transform + noise)',