DPGMM.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:megamix 作者: 14thibea 项目源码 文件源码
def _step_M(self,points,log_resp):
        """
        In this step the algorithm updates the values of the parameters (means, covariances,
        alpha, beta, nu).

        Parameters
        ----------
        points : an array (n_points,dim)

        log_resp: an array (n_points,n_components)
            an array containing the logarithm of the responsibilities.

        """

        n_points,dim = points.shape

        resp = np.exp(log_resp)

        # Convenient statistics
        N = np.sum(resp,axis=0) + 10*np.finfo(resp.dtype).eps            #Array (n_components,)
        X_barre = 1/N[:,np.newaxis] * np.dot(resp.T,points)              #Array (n_components,dim)
        S = _full_covariance_matrices(points,X_barre,N,resp,self.reg_covar,self.n_jobs)

        #Parameters update
        self.alpha = np.asarray([1.0 + N,
                                  self.alpha_0 + np.hstack((np.cumsum(N[::-1])[-2::-1], 0))]).T
        self.alpha += np.asarray([-self.pypcoeff * np.ones(self.n_components),
                                      self.pypcoeff * np.arange(self.n_components)]).T
        self.beta = self.beta_0 + N
        self.nu = self.nu_0 + N

        # Weights update
        for i in range(self.n_components):
            if i==0:
                self.log_weights[i] = psi(self.alpha[i][0]) - psi(np.sum(self.alpha[i]))
            else:
                self.log_weights[i] = psi(self.alpha[i][0]) - psi(np.sum(self.alpha[i]))
                self.log_weights[i] += self.log_weights[i-1] + psi(self.alpha[i-1][1]) - psi(self.alpha[i-1][0])

        # Means update
        means = self.beta_0 * self._means_prior + N[:,np.newaxis] * X_barre
        self.means = means * np.reciprocal(self.beta)[:,np.newaxis]
        self.means_estimated = self.means

        # Covariance update
        for i in range(self.n_components):
            diff = X_barre[i] - self._means_prior
            product = self.beta_0 * N[i]/self.beta[i] * np.outer(diff,diff)
            self._inv_prec[i] = self._inv_prec_prior + N[i] * S[i] + product

            det_inv_prec = np.linalg.det(self._inv_prec[i])
            self._log_det_inv_prec[i] = np.log(det_inv_prec)
            self.cov[i] = self._inv_prec[i] / self.nu[i]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号