机器学习中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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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