[英]ZCA whitening in python for Machine learning
I am training 1000 images of 28x28 size. 我正在训练28x28尺寸的1000张图像。 But before training, I am performing ZCA whitening on my data by taking the reference from How to implement ZCA Whitening?
但是在培训之前,我将参考如何实施ZCA Whitening的参考对数据执行ZCA Whitening。 Python .
Python 。
Since I have 1000 data images of size 28x28, after flattening, it becomes 1000x784. 由于我有1000个尺寸为28x28的数据图像,因此在展平后,它将变为1000x784。 But as given in the code below, whether X is my image dataset of 1000x784?
但是,如下面的代码所示,X是否是我的1000x784图像数据集?
If it is so, then it means the ZCAMatrix size is 1000x1000. 如果是这样,则表示ZCAMatrix大小为1000x1000。 In this case, for prediction I have a image of size 28x28, or we can say, size of 1x784.So it doesn't make sense to multiply ZCAMatrix to the image.
在这种情况下,为了进行预测,我的图像尺寸为28x28,或者可以说尺寸为1x784,因此将ZCAMatrix乘以该图像是没有意义的。
So I think, X is the transpose of image data set. 所以我认为X是图像数据集的转置。 Am I right?
我对吗? If I am right, then the size of ZCAMatrix is 784x784.
如果我是对的,则ZCAMatrix的大小为784x784。
Now 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)
? 现在,无论我应该使用
np.dot(ZCAMatrix, transpose_of_image_to_be_predict)
还是np.dot(image_to_be_predict, ZCAMatrix)
如何计算ZCA增白图像? Suggestion would be greatly appreciate. 建议将不胜感激。
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
And an example of the usage: 以及用法示例:
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
I got the reference from the Keras code available here . 我从此处提供的Keras代码获得了参考。
It is very clear that in my case the co-variance matrix will give 784x784 matrix, on which Singular Value Decomposition is performed. 很明显,在我的情况下,协方差矩阵将给出784x784矩阵,在该矩阵上执行奇异值分解 。 It gives 3 matrix that is used to calculate the principal_components , and that principal_components is used to find the ZCA whitened data.
它给出了3个矩阵,用于计算principal_components ,并且principal_components用于查找ZCA增白的数据。
Now my question was 现在我的问题是
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)?
应该使用np.dot(ZCAMatrix,transpose_of_image_to_be_predict)还是np.dot(image_to_be_predict,ZCAMatrix),如何计算ZCA增白图像? Suggestion would be greatly appreciate.
建议将不胜感激。
For this I got the reference from here . 为此,我从这里得到了参考。
Here I need to use np.dot(image_to_be_predict, ZCAMatrix)
to calculate the ZCA whitened image. 在这里,我需要使用
np.dot(image_to_be_predict, ZCAMatrix)
计算ZCA增白图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.