fv.py 文件源码

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

项目:feature-aggregation 作者: paschalidoud 项目源码 文件源码
def fit(self, X, y=None):
        """Learn a fisher vector encoding.

        Fit a gaussian mixture model to the data using n_gaussians with
        diagonal covariance matrices.

        Parameters
        ----------
        X : array_like or list
            The local features to train on. They must be either nd arrays or
            a list of nd arrays.
        """
        X, _ = self._reshape_local_features(X)

        if self.n_pca_components != 1:
            # train PCA
            self.pca_model = PCA(n_components=int(X.shape[-1]*self.n_pca_components))
            self.pca_model.fit(X)

            # apply PCA and reduce dimensionality
            X = self.pca_model.transform(X)

        # consider changing the initialization parameters
        gmm = GaussianMixture(
            n_components=self.n_gaussians,
            max_iter=self.max_iter,
            covariance_type='diag',
            verbose=self.verbose
        )
        gmm.fit(X)

        # save the results of the gmm
        self.weights = gmm.weights_
        self.means = gmm.means_
        self.covariances = gmm.covariances_

        # precompute some values for encoding
        D = X[0].size
        self.inverted_covariances = (1./self.covariances)
        self.inverted_sqrt_covariances = np.sqrt(1./self.covariances)
        self.normalization_factor = np.hstack([
            np.repeat(1.0/np.sqrt(self.weights), D),
            np.repeat(1.0/np.sqrt(2*self.weights), D)
        ])

        return self
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号