Beispiel #1
0
def test_deprecations():

    with pytest.warns(FutureWarning, match="deprecated in 0.22"):
        gaussian_random_matrix(10, 100)

    with pytest.warns(FutureWarning, match="deprecated in 0.22"):
        sparse_random_matrix(10, 100)
 def __init__(self, n_features=784, n_components=20, random_state=None):
     super().__init__()
     self.n_features = n_features
     self.n_components = n_components
     self.decode = torch.Tensor(
         gaussian_random_matrix(self.n_components,
                                self.n_features,
                                random_state=random_state)).float()
Beispiel #3
0
def generate_random_projection_weights(original_dim, projected_dim):
  random_projection_matrix = None
  random_projection_matrix = gaussian_random_matrix(
      n_components=projected_dim, n_features=original_dim).T
  print("A Gaussian random weight matrix was creates with shape of {}".format(random_projection_matrix.shape))
  print('Storing random projection matrix to disk...')
  with open('random_projection_matrix', 'wb') as handle:
    pickle.dump(random_projection_matrix, handle, protocol=pickle.HIGHEST_PROTOCOL)
        
  return random_projection_matrix
def test_gaussian_random_matrix():
    """Check some statical properties of Gaussian random matrix"""
    # Check that the random matrix follow the proper distribution.
    # Let's say that each element of a_{ij} of A is taken from
    #   a_ij ~ N(0.0, 1 / n_components).
    #
    n_components = 100
    n_features = 1000
    A = gaussian_random_matrix(n_components, n_features, random_state=0)

    assert_array_almost_equal(0.0, np.mean(A), 2)
    assert_array_almost_equal(np.var(A, ddof=1), 1 / n_components, 1)
def test_gaussian_random_matrix():
    # Check some statical properties of Gaussian random matrix
    # Check that the random matrix follow the proper distribution.
    # Let's say that each element of a_{ij} of A is taken from
    #   a_ij ~ N(0.0, 1 / n_components).
    #
    n_components = 100
    n_features = 1000
    A = gaussian_random_matrix(n_components, n_features, random_state=0)

    assert_array_almost_equal(0.0, np.mean(A), 2)
    assert_array_almost_equal(np.var(A, ddof=1), 1 / n_components, 1)
Beispiel #6
0
 def create_estimator_no_params(self):
     if self.estimator_name == 'K-means':
         self.estimator = cluster.KMeans(n_jobs=self.n_jobs)
     elif self.estimator_name == 'EM':
         self.estimator = mixture.GaussianMixture()
     elif self.estimator_name == 'PCA':
         self.estimator = decomposition.PCA()
     elif self.estimator_name == 'ICA':
         self.estimator = decomposition.FastICA()
     elif self.estimator_name == 'Random_Projection':
         self.estimator = random_projection.gaussian_random_matrix()
     elif self.estimator_name == 'Dictionary_Learning':
         self.estimator = decomposition.DictionaryLearning()
Beispiel #7
0
    def __init__(self, patch_size, nc, n_components, stride=1):
        super(RandomProjection, self).__init__()

        randmat = random_projection.gaussian_random_matrix(
            n_components, nc * patch_size * patch_size)
        self.main = nn.Conv2d(nc,
                              n_components,
                              patch_size,
                              stride=stride,
                              padding=0,
                              bias=False)
        randmat = randmat.reshape(*self.main.weight.shape).astype(np.float32)
        self.main.weight = nn.Parameter(torch.from_numpy(randmat))

        for p in self.parameters():
            p.requires_grad = False
Beispiel #8
0
def generate_random_projection_weights(original_dim, projected_dim, output_dir):
  """Generates a Gaussian random projection weights matrix."""

  random_projection_matrix = None
  if projected_dim and original_dim > projected_dim:
    random_projection_matrix = gaussian_random_matrix(
        n_components=projected_dim, n_features=original_dim).T
    print('A Gaussian random weight matrix was creates with shape of {}'.format(
        random_projection_matrix.shape))
    print('Storing random projection matrix to disk...')
    output_file_path = os.path.join(output_dir, _RANDOM_PROJECTION_FILENAME)
    with open(output_file_path, 'wb') as handle:
      pickle.dump(
          random_projection_matrix, handle, protocol=pickle.HIGHEST_PROTOCOL)
    print('Random projection matrix saved to disk.')

  return random_projection_matrix