-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improvements to numerical stability and performance for ZCA whitening #9025
Conversation
…it call to diag for eigenvalues
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
keras/preprocessing/image.py
Outdated
sigma = np.dot(flat_x.T, flat_x) / flat_x.shape[0] | ||
u, s, _ = linalg.svd(sigma) | ||
self.principal_components = np.dot(np.dot(u, np.diag(1. / np.sqrt(s + self.zca_epsilon))), u.T) | ||
n_examples = flat_x.shape[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style: use num_examples
for consistency
keras/preprocessing/image.py
Outdated
n_examples = flat_x.shape[0] | ||
u, s, vt = linalg.svd(flat_x / np.sqrt(n_examples)) | ||
s_expand = np.hstack((s, np.zeros(vt.shape[0] - n_examples, dtype=flat_x.dtype))) | ||
self.principal_components = (vt.T / np.sqrt(s_expand**2 + self.zca_epsilon)).dot(vt) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style: spaces around **
This PR simplifies ZCA whitening by not forming the covariance matrix or the full diagonal matrix. Instead it uses the SVD of the data itself and broadcasting division, which is mathematically equivalent but faster and more numerically stable.
Tested numerically by the following