Skip to content

Commit

Permalink
Improvements to numerical stability and performance for ZCA whitening (
Browse files Browse the repository at this point in the history
…#9025)

* Implemented zca_whitening without forming covariance matrix or explicit call to diag for eigenvalues

* Made principal_components self.principal_components

* Fixed mistake with zca_epsilon

* Style fixes
  • Loading branch information
mihirparadkar authored and fchollet committed Jan 10, 2018
1 parent 7d7060f commit 32aa192
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions keras/preprocessing/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,9 +748,10 @@ def fit(self, x,

if self.zca_whitening:
flat_x = np.reshape(x, (x.shape[0], x.shape[1] * x.shape[2] * x.shape[3]))
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)
num_examples = flat_x.shape[0]
u, s, vt = linalg.svd(flat_x / np.sqrt(num_examples))
s_expand = np.hstack((s, np.zeros(vt.shape[0] - num_examples, dtype=flat_x.dtype)))
self.principal_components = (vt.T / np.sqrt(s_expand ** 2 + self.zca_epsilon)).dot(vt)


class Iterator(Sequence):
Expand Down

0 comments on commit 32aa192

Please sign in to comment.