Search code examples
pythontensorfloweigenvector

Eigenvalue problems in TensorFlow


I want to solve an eigenvalue problem using TensorFlow. In particular, I have

e, v = tf.self_adjoint_eig(laplacian, name="eigendata")
eigenmap = v[:,1:4]

so I don't want to compute all eigenvectors.

In Matlab, I would use eigs(laplacian,4,'sm')

Looking at https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/linalg_ops.py, I see that tf.self_adjoint_eig calls gen_linalg_ops._self_adjoint_eig_v2. However, I can't find gen_linalg_ops on Github or elsewhere.

Any advice on doing such linear algebra in TensorFlow, or is it best to go with other libraries in Python?


Solution

  • MATLAB function EIG calculates all the eigenvectors. MATLAB function EIGS only calculates a selected number of the eigenvectors using precompiled https://en.wikipedia.org/wiki/ARPACK which implements https://en.wikipedia.org/wiki/Lanczos_algorithm There is no native MATLAB Lanczos code in MATLAB, most likely because the Lanczos algorithm is unavoidably unstable with respect to round-off errors, especially in single precision, making more stable implementations tricky and/or expensive.

    An alternative to EIGS function is https://www.mathworks.com/matlabcentral/fileexchange/48-lobpcg-m that implements https://en.wikipedia.org/wiki/LOBPCG natively in MATLAB.

    SciPy has an interface to ARPACK as well as the Python native implementation https://docs.scipy.org/doc/scipy-1.1.0/reference/generated/scipy.sparse.linalg.lobpcg.html

    Scikit uses ARPACK or LOBPCG for manifold spectral embedding http://scikit-learn.org/stable/modules/generated/sklearn.manifold.spectral_embedding.html and for spectral clustering http://scikit-learn.org/stable/modules/generated/sklearn.cluster.SpectralClustering.html

    TensorFlow now has a native implementation of Lanczos https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/solvers/python/ops/lanczos.py