Exemplo n.º 1
0
    def standard_init(self, data):
        """ Standard Initial Estimation for *W* and *sigma*.

        each *W* raw is set to the average over the data plus WGN of mean zero
        and var *sigma*/4. *sigma* is set to the variance of the data around 
        the computed mean. *pi* is set to 1./H . Returns a dict with the 
        estimated parameter set with entries "W", "pi" and "sigma".
        """
        comm = self.comm
        H = self.H
        my_y = data['y']
        my_N, D = my_y.shape

        assert D == self.D

        # Calculate averarge W
        W_mean = parallel.allmean(my_y, axis=0, comm=comm)               # shape: (D, )

        # Calculate data variance
        sigma_sq = parallel.allmean((my_y-W_mean)**2, axis=0, comm=comm) # shape: (D, )
        sigma_init = np.sqrt(sigma_sq).sum() / D                         # scalar

        # Initial W
        noise = sigma_init/4.
        W_init = W_mean[:,None] + np.random.normal(scale=noise, size=[D, H])    # shape: (H, D)`

        #Create and set Model Parameters, W columns have the same average!
        model_params = {'W'     : W_init }

        if 'pies' in self.to_learn:
            model_params['pies'] = np.ones(H) * 1./H

        return model_params
Exemplo n.º 2
0
    def standard_init(self, my_data):
        """ Standard Initial of the model parameters.

        """
        comm = self.comm

        temp_params = CAModel.standard_init(self, my_data)
        model_params = {}
        model_params['W'] = temp_params['W'].copy()

        # Initial pi
        pi = comm.bcast(np.random.rand(self.H)) * 0.95
        pi[pi < 0.05] = 0.05

        model_params['pi'] = pi

        my_y = my_data['y']

        # Calculate averarge W
        W_mean = parallel.allmean(my_y, axis=0, comm=comm)  # shape: (D, )

        # Calculate data variance
        sigma_sq_sq = parallel.allmean((my_y - W_mean)**2, axis=0,
                                       comm=comm)  # shape: (D, )

        # Calculate sigma_sq
        if self.sigma_sq_type == 'full':
            model_params['sigma_sq'] = np.diag(
                np.diag(sigma_sq_sq)) + (0.001 * np.eye(self.D))
        elif self.sigma_sq_type == 'diagonal':
            model_params['sigma_sq'] = sigma_sq_sq + 0.001
        else:
            model_params['sigma_sq'] = np.mean(sigma_sq_sq) + 0.001

        if 'mu' in self.to_learn:
            mu = comm.bcast(np.random.normal(0, 1, [self.H]))
        else:
            mu = np.zeros(self.H)

        model_params['mu'] = mu

        if 'psi_sq' in self.to_learn:
            psi_sq_diag = comm.bcast(np.random.rand(self.H)) * 2
            psi_sq_diag[psi_sq_diag < 0.05] = 0.05
            psi_sq = np.diag(psi_sq_diag)
        else:
            psi_sq = np.eye(self.H)

        model_params['psi_sq'] = psi_sq

        model_params = comm.bcast(model_params)

        return model_params
Exemplo n.º 3
0
    def standard_init(self, data):
        """Standard onitial estimation for model parameters.
        
        This implementation each "W" raw is set to the average over the data plus white Gaussian noise of mean zero
        and standard deviation "sigma"/4. sigma is set to the variance of the data around
        the computed mean. "pi" is set to 1./H . Returns a dict with the
        estimated parameter set with entries "W", "pi" and "sigma".
        
        Parameters
        ----------
        data : dict
            dataset dictionary. Contains a ndarray of size number of samples x number of features under data['y']
        
        Returns
        -------
        dict
            a dictionary containing the model parameters
        """
        comm = self.comm
        H = self.H
        my_y = data['y']
        my_N, D = my_y.shape

        assert D == self.D

        # Calculate averarge W
        W_mean = parallel.allmean(my_y, axis=0, comm=comm)  # shape: (D, )

        # Calculate data variance
        # import ipdb;ipdb.set_trace()  ######### Break Point ###########

        sigma_sq = parallel.allmean((my_y - W_mean)**2, axis=0,
                                    comm=comm)  # shape: (D, )
        sigma_init = np.sqrt(sigma_sq).sum() / D  # scalar

        # Initial W
        noise = sigma_init / 4.
        # noise = sigma_init * 4.
        # shape: (H, D)`
        # W_init = W_mean[:, None] + np.random.laplace(scale=noise, size=[D, H])
        W_init = W_mean[:, None] + np.random.normal(scale=noise, size=[D, H])

        sparsity = 1. - (1. / H)
        pi_init = np.random.rand(self.K - 1)
        pi_init = (1 - sparsity) * pi_init / pi_init.sum()
        pi_init = np.insert(pi_init, self._K_0, sparsity)

        # Create and set Model Parameters, W columns have the same average!
        model_params = {'W': W_init, 'pi': pi_init, 'sigma': sigma_init}

        return model_params
Exemplo n.º 4
0
    def standard_init(self, data):
        """ Standard onitial estimation for model parameters.

        This implementation

        *W* and *sigma*.



        each *W* raw is set to the average over the data plus WGN of mean zero
        and var *sigma*/4. *sigma* is set to the variance of the data around
        the computed mean. *pi* is set to 1./H . Returns a dict with the
        estimated parameter set with entries "W", "pi" and "sigma".
        """
        comm = self.comm
        H = self.H
        my_y = data['y']
        my_N, D = my_y.shape

        assert D == self.D

        # Calculate averarge W
        W_mean = parallel.allmean(my_y, axis=0, comm=comm)     # shape: (D, )

        # Calculate data variance
        # import ipdb;ipdb.set_trace()  ######### Break Point ###########

        sigma_sq = parallel.allmean((my_y - W_mean)**2, axis=0, comm=comm)  # shape: (D, )
        sigma_init = np.sqrt(sigma_sq).sum()/D                         # scalar

        # Initial W
        noise = sigma_init / 4.
        # noise = sigma_init * 4.
        # shape: (H, D)`
        # W_init = W_mean[:, None] + np.random.laplace(scale=noise, size=[D, H])
        W_init = W_mean[:, None] + np.random.normal(scale=noise, size=[D, H])

        sparsity = 1. - (1./H) 
        pi_init = np.random.rand(self.K - 1)
        pi_init = (1 - sparsity) * pi_init / pi_init.sum()
        pi_init = np.insert(pi_init, self.K_0, sparsity)

        # Create and set Model Parameters, W columns have the same average!
        model_params = {
            'W': W_init,
            'pi': pi_init,
            'sigma': sigma_init
        }

        return model_params
Exemplo n.º 5
0
    def test_allmean(self):
        D = 10
        my_a = np.ones(D)

        mean = parallel.allmean(my_a)
        self.assertAlmostEqual(mean, 1.0)