繁体   English   中英

适用于机器学习的python中的ZCA美白

[英]ZCA whitening in python for Machine learning

我正在训练28x28尺寸的1000张图像。 但是在培训之前,我将参考如何实施ZCA Whitening的参考对数据执行ZCA Whitening。 Python

由于我有1000个尺寸为28x28的数据图像,因此在展平后,它将变为1000x784。 但是,如下面的代码所示,X是否是我的1000x784图像数据集?

如果是这样,则表示ZCAMatrix大小为1000x1000。 在这种情况下,为了进行预测,我的图像尺寸为28x28,或者可以说尺寸为1x784,因此将ZCAMatrix乘以该图像是没有意义的。

所以我认为X是图像数据集的转置。 我对吗? 如果我是对的,则ZCAMatrix的大小为784x784。

现在,无论我应该使用np.dot(ZCAMatrix, transpose_of_image_to_be_predict)还是np.dot(image_to_be_predict, ZCAMatrix)如何计算ZCA增白图像? 建议将不胜感激。

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代码获得了参考。

很明显,在我的情况下,协方差矩阵将给出784x784矩阵,在该矩阵上执行奇异值分解 它给出了3个矩阵,用于计算principal_components ,并且principal_components用于查找ZCA增白的数据。

现在我的问题是

应该使用np.dot(ZCAMatrix,transpose_of_image_to_be_predict)还是np.dot(image_to_be_predict,ZCAMatrix),如何计算ZCA增白图像? 建议将不胜感激。

为此,我从这里得到了参考。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM