python 用于机器学习的 ZCA 白化

ZCA whitening in python for Machine learning

我正在训练 1000 张 28x28 大小的图像。但在训练之前,我通过参考 How to implement ZCA Whitening? Python.

对我的数据进行 ZCA 白化

由于我有1000张大小为28x28的数据图片,展平后变成了1000x784。 但是如下代码所示,X是否是我的1000x784的图像数据集?

如果是这样,则说明ZCAMatrix大小为1000x1000。 在这种情况下,对于预测,我有一个大小为 28x28 的图像,或者我们可以说,1x784.So 的大小将 ZCAMatrix 与图像相乘没有意义。

所以我想,X是图像数据集的转置。我对吗? 如果我是对的,那么 ZCAMatrix 的大小是 784x784.

现在如何计算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] matrix
ZCAMatrix = zca_whitening_matrix(X) # get ZCAMatrix
ZCAMatrix # [5 x 5] matrix
xZCAMatrix = np.dot(ZCAMatrix, X) # project X onto the ZCAMatrix
xZCAMatrix # [5 x 3] matrix

我从可用的 Keras 代码中获得了参考 here

很明显,在我的例子中,协方差矩阵将给出 784x784 矩阵,在该矩阵上执行 奇异值分解 。它给出了 3 个矩阵,用于计算 principal_components,而 principal_components 用于查找白化的 ZCA数据。

现在我的问题是

how should I calculate the ZCA whitened image, whether I should use np.dot(ZCAMatrix, transpose_of_image_to_be_predict) or np.dot(image_to_be_predict, ZCAMatrix)? Suggestion would be greatly appreciate.

为此,我从 here 获得了参考资料。

这里需要用到np.dot(image_to_be_predict, ZCAMatrix)计算ZCA白化后的图像