Skip to content
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

Merged
merged 4 commits into from
Jan 10, 2018

Conversation

mihirparadkar
Copy link
Collaborator

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

import numpy as np
import numpy.random as npr
import numpy.linalg as linalg

def princomps_old(flat_x):
    sigma = np.dot(flat_x.T, flat_x) / flat_x.shape[0]
    u, s, _ = linalg.svd(sigma)
    principal_components = np.dot(np.dot(u, np.diag(1. / np.sqrt(s + 1e-5))), u.T)
    return principal_components
 
def princomps_new(flat_x):
    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)))
    principal_components = (vt.T / np.sqrt(s_expand**2 + 1e-5)).dot(vt)
    return principal_components

x = npr.randn(64, 784) #A batch of MNIST data
flat_x = x - x.mean(axis=0)
print(np.allclose(princomps_old(flat_x), princomps_new(flat_x))) #True

fchollet
fchollet previously approved these changes Jan 9, 2018
Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

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]
Copy link
Member

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

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: spaces around **

@fchollet fchollet merged commit 32aa192 into keras-team:master Jan 10, 2018
angeloskath added a commit to angeloskath/keras that referenced this pull request Jan 26, 2018
fchollet pushed a commit that referenced this pull request Jan 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants