Skip to content

Commit

Permalink
OTH: The code seems to work on my machine, but it's still in an indec…
Browse files Browse the repository at this point in the history
…ent state
  • Loading branch information
pberkes committed Aug 17, 2010
1 parent bd06224 commit d007c33
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
19 changes: 9 additions & 10 deletions mdp/nodes/sparse_pca_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def _check_roundoff(t, dtype):
'\nerrors. See CovarianceMatrix docstring for more'
' information.' % (t, dtype.name))
warnings.warn(wr, mdp.MDPWarning)

class SparseCovarianceMatrix(object):
"""This class stores an empirical covariance matrix that can be updated
incrementally. A call to the 'fix' method returns the current state of
Expand Down Expand Up @@ -196,9 +195,6 @@ def _check_output(self, y):
error_str = ("y has dimension %d"
", should be 0<y<=%d" % (y.shape[1], self.output_dim))
raise NodeException(error_str)

def _get_supported_dtypes(self):
return ['float32', 'float64']

def get_explained_variance(self):
"""Return the fraction of the original variance that can be
Expand Down Expand Up @@ -248,11 +244,12 @@ def _stop_training(self, debug=False):
self.cov_mtx and self.dcov_mtx to be examined.
"""
# request the covariance matrix and clean up
self.cov_mtx, avg, self.tlen = self._cov_mtx.fix()
self.cov_mtx, self.avg, self.tlen = self._cov_mtx.fix()
del self._cov_mtx

# range for the eigenvalues
rng = self._adjust_output_dim()
#rng = self._adjust_output_dim()
rng = self.output_dim

# if we have more variables then observations we are bound to fail here
# suggest to use the NIPALSNode instead.
Expand All @@ -271,26 +268,28 @@ def _stop_training(self, debug=False):
# compute the eigenvectors of the covariance matrix (inplace)
# (eigenvalues sorted in ascending order)
try:
d, v = mdp.numx.sparse.svd(self.cov_mtx)
self._symeig(self.cov_mtx, k=rng, overwrite=(not debug))
print rng
d, v = mdp.numx.sparse.linalg.eigen_symmetric(self.cov_mtx, k=rng)
#self._symeig(self.cov_mtx, k=rng, overwrite=(not debug))
# if reduce=False and svd=False. we should check for
# negative eigenvalues and fail
if not (self.reduce or self.svd or (self.desired_variance is
if not (self.reduce or (self.desired_variance is
not None)):
if d.min() < 0:
raise NodeException("Got negative eigenvalues: "
"%s.\n"
"You may either set output_dim to be"
" smaller, or set reduce=True and/or "
"svd=True" % str(d))
except SymeigException, exception:
except mdp.utils.SymeigException as exception:
err = str(exception)+("\nCovariance matrix may be singular."
"Try setting svd=True.")
raise NodeException(err)

# delete covariance matrix if no exception occurred
if not debug:
del self.cov_mtx
del self.avg

# sort by descending order
d = numx.take(d, range(d.shape[0]-1, -1, -1))
Expand Down
31 changes: 22 additions & 9 deletions mdp/test/test_sparse_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,32 @@
#import scipy
#from numpy.random import rand

# create pca node
pcanode1 = mdp.nodes.SparsePCANode()
x = mdp.numx.sparse.lil_matrix((12, 11))
for i in range(11):
#x[i,i] = float(i)
#x[i,i] = float(i)
x[0,i] = float(i)+1.
x[i+1,i] = -float(i)-1.

#d, v = numx.sparse.linalg.eigen_symmetric(x, k=10)





# create random sparse matrix
x = mdp.numx.sparse.lil_matrix((1000, 1000))
x[0, :100] = mdp.numx.rand(100)
x[1, 100:200] = x[0, :100]
x.setdiag(mdp.numx.rand(1000))
x = x.tocsr() # convert it to CSR
#x = mdp.numx.sparse.lil_matrix((1000, 1000))
#[0, :100] = mdp.numx.rand(100)
#x[1, 100:200] = x[0, :100]
#x.setdiag(mdp.numx.rand(1000))
#x = x.tocsr() # convert it to CSR


v, u, w = numx.sparse.linalg.eigen_symmetric(x)
# create pca node
pcanode1 = mdp.nodes.SparsePCANode(output_dim=10)

pcanode1.train(x)
pcanode1.stop_training()
pcanode1.stop_training(debug=True)

#print pcanode1.d, d
#assert all(pcanode1.d == d)

0 comments on commit d007c33

Please sign in to comment.