Exemple #1
0
    def test_ICA(self):
        print ('Preprocessing -> Performing ICA test ...')
        sys.stdout.flush()
        fail = 0
        for _ in range(100):
            data, mixMat = generate_2d_mixtures(10000)
            zca = ZCA(data.shape[1])
            zca.train(data)
            data_zca = zca.project(data)
            mixMat_zca = zca.project(mixMat.T).T
            mixMat_zca /= get_norms(mixMat_zca, axis=None)
            model = ICA(2)
            model.train(data_zca, 10)
            assert numx.all(numx.abs(data_zca - model.unproject(model.project(data_zca))) < self.epsilon)

            # Check the angle between mixing matrix  vectors and resulting vectors of the projection matrix
            # Theorectially the amari distnace could be used but it seems only to be rotation and scal invariant
            # and not invariant through permutations of the colums.
            res = model.projection_matrix / get_norms(model.projection_matrix, axis=None)
            v1 = res
            v2 = res * numx.array([[-1, 1], [-1, 1]])
            v3 = res * numx.array([[1, -1], [1, -1]])
            v4 = -res
            res = numx.array([res[:, 1], res[:, 0]]).T
            v5 = res
            v6 = res * numx.array([[-1, 1], [-1, 1]])
            v7 = res * numx.array([[1, -1], [1, -1]])
            v8 = -res

            v1d = numx.max([angle_between_vectors(v1[0], mixMat_zca[0]), angle_between_vectors(v1[1], mixMat_zca[1])])
            v2d = numx.max([angle_between_vectors(v2[0], mixMat_zca[0]), angle_between_vectors(v2[1], mixMat_zca[1])])
            v3d = numx.max([angle_between_vectors(v3[0], mixMat_zca[0]), angle_between_vectors(v3[1], mixMat_zca[1])])
            v4d = numx.max([angle_between_vectors(v4[0], mixMat_zca[0]), angle_between_vectors(v4[1], mixMat_zca[1])])
            v5d = numx.max([angle_between_vectors(v5[0], mixMat_zca[0]), angle_between_vectors(v5[1], mixMat_zca[1])])
            v6d = numx.max([angle_between_vectors(v6[0], mixMat_zca[0]), angle_between_vectors(v6[1], mixMat_zca[1])])
            v7d = numx.max([angle_between_vectors(v7[0], mixMat_zca[0]), angle_between_vectors(v7[1], mixMat_zca[1])])
            v8d = numx.max([angle_between_vectors(v8[0], mixMat_zca[0]), angle_between_vectors(v8[1], mixMat_zca[1])])

            dist = numx.min([v1d, v2d, v3d, v4d, v5d, v6d, v7d, v8d])
            # biggest angle smaller than 5 degrees
            if dist > 5.0:
                fail += 1
        assert fail < 5
        print('successfully passed!')
        sys.stdout.flush()
Exemple #2
0
import pydeep.base.numpyextension as numxext
from pydeep.preprocessing import ZCA, ICA
from pydeep.misc.toyproblems import generate_2d_mixtures
import pydeep.misc.visualization as vis

# Set the random seed
# (optional, if stochastic processes are involved we get the same results)
numx.random.seed(42)

# Create 2D linear mixture, 50000 samples, mean = 0, std = 3
data, mixing_matrix = generate_2d_mixtures(num_samples=50000,
                                           mean=0.0,
                                           scale=3.0)

# Zero Phase Component Analysis (ZCA) - Whitening in original space
zca = ZCA(data.shape[1])
zca.train(data)
whitened_data = zca.project(data)

# Independent Component Analysis (ICA)
ica = ICA(whitened_data.shape[1])

ica.train(whitened_data, iterations=100, status=False)
data_ica = ica.project(whitened_data)

# print the ll on the data
print("Log-likelihood on all data: " +
      str(numx.mean(ica.log_likelihood(data=whitened_data))))

print "Amari distanca between true mixing matrix and estimated mixing matrix: " + str(
    vis.calculate_amari_distance(zca.project(mixing_matrix.T),
Exemple #3
0
 def test_ZCA(self):
     print ('Preprocessing -> Performing ZCA test ...')
     sys.stdout.flush()
     cov = numx.array([[1.0, 0.8], [0.8, 1.0]])
     mean = numx.array([1.0, -1.0])
     data = numx.random.multivariate_normal(mean, cov, 10000)
     # Eigenvectors of the Gaussian point in 45/225 and 135/315
     # degree direction and the vectors only differ in sign
     target = 1.0 / numx.sqrt(2.0)
     target = numx.ones((2, 2)) * target
     model = ZCA(data.shape[1])
     model.train(data)
     # assert numx.all(numx.abs(numx.abs(models.projection_matrix)-target) < 0.01)
     assert numx.all(numx.abs(data - model.unproject(model.project(data))) < self.epsilon)
     assert model.project(data, 1).shape[1] == 1
     assert model.unproject(model.project(data, 1), 1).shape[1] == 1
     assert model.unproject(model.project(data), 1).shape[1] == 1
     assert model.unproject(model.project(data, 1)).shape[1] == 2
     print('successfully passed!')
     sys.stdout.flush()
Exemple #4
0
import pydeep.misc.io as io
import pydeep.misc.visualization as vis

# Set the random seed
# (optional, if stochastic processes are involved we always get the same results)
numx.random.seed(42)

# Load data (download is not existing)
data = io.load_natural_image_patches('NaturalImage.mat')

# Specify image width and height for displaying
width = height = 14

# Use ZCA to whiten the data and train it
# (you could also use PCA whitened=True + unproject for visualization)
zca = ZCA(input_dim=width * height)
zca.train(data=data)

# ZCA projects the whitened data back to the original space, thus does not
# perform a dimensionality reduction but a whitening in the original space
whitened_data = zca.project(data)

# Create a ZCA node and train it (you could also use PCA whitened=True)
ica = ICA(input_dim=width * height)
ica.train(data=whitened_data,
          iterations=100,
          convergence=1.0,
          status=True)

# Show whitened images
images = vis.tile_matrix_rows(matrix=data[0:100].T,