机器学习中Python的ZCA白化

我正在训练1000张28×28大小的图像。但在训练之前,我参考如何实现ZCA白化?Python对我的数据进行了ZCA白化处理。

由于我有1000张28×28大小的数据图像,经过扁平化处理后变为1000×784。但根据下面的代码,X是否是我的1000×784的图像数据集?

如果是这样的话,那么ZCAMatrix的大小应该是1000×1000。在这种情况下,用于预测的图像大小为28×28,或者可以说,尺寸为1×784。因此,将ZCAMatrix乘以图像是没有意义的。

所以我认为,X是图像数据集的转置。我的理解正确吗?如果我正确的话,那么ZCAMatrix的大小应该是784×784。

现在我应该如何计算ZCA白化后的图像,我应该使用np.dot(ZCAMatrix, transpose_of_image_to_be_predict)还是np.dot(image_to_be_predict, ZCAMatrix)?非常欢迎建议。

def zca_whitening_matrix(X):    """    Function to compute ZCA whitening matrix (aka Mahalanobis whitening).    INPUT:  X: [M x N] matrix.        Rows: Variables        Columns: Observations    OUTPUT: ZCAMatrix: [M x M] matrix    """    # Covariance matrix [column-wise variables]: Sigma = (X-mu)' * (X-mu) / N    sigma = np.cov(X, rowvar=True) # [M x M]    # Singular Value Decomposition. X = U * np.diag(S) * V    U,S,V = np.linalg.svd(sigma)        # U: [M x M] eigenvectors of sigma.        # S: [M x 1] eigenvalues of sigma.        # V: [M x M] transpose of U    # Whitening constant: prevents division by zero    epsilon = 1e-5    # ZCA Whitening matrix: U * Lambda * U'    ZCAMatrix = np.dot(U, np.dot(np.diag(1.0/np.sqrt(S + epsilon)), U.T)) # [M x M]    return ZCAMatrix

使用示例:

X = np.array([[0, 2, 2], [1, 1, 0], [2, 0, 1], [1, 3, 5], [10, 10, 10] ]) # Input: X [5 x 3] matrixZCAMatrix = zca_whitening_matrix(X) # get ZCAMatrixZCAMatrix # [5 x 5] matrixxZCAMatrix = np.dot(ZCAMatrix, X) # project X onto the ZCAMatrixxZCAMatrix # [5 x 3] matrix

回答:

我参考了可在这里找到的Keras代码。

很明显,在我的情况下,协方差矩阵将生成一个784×784的矩阵,并对其进行奇异值分解。它会生成三个矩阵,用于计算主成分,然后使用这些主成分来找到ZCA白化后的数据。

现在我的问题是

我应该如何计算ZCA白化后的图像,是使用np.dot(ZCAMatrix, transpose_of_image_to_be_predict)还是np.dot(image_to_be_predict, ZCAMatrix)?非常欢迎建议。

对此,我参考了这里的资料。

在这里,我需要使用np.dot(image_to_be_predict, ZCAMatrix)来计算ZCA白化后的图像。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注