Python port of the MATLAB package manopt, for manifold optimization using Theano for automatic differentiation.
import autograd.numpy as np
from pymanopt.manifolds import Sphere
from pymanopt import Problem
from pymanopt.solvers import NelderMead
n = 1000; m = 8
A = np.random.randn(m, n)
P = np.eye(m)
solver = NelderMead(maxiter = 1000)
manifold = Sphere(m) ## spherical constraint
def cost(X): return np.sum(X)
problem = Problem(
manifold = manifold,
cost = cost,
verbosity = 1
)
import time
start = time.time()
x = solver.solve(problem)
end = time.time()
print(end - start)
Can any one please help me use Pymanopt with Tensorflow code which uses placeholders (passing input X, y through feedict)?
import tensorflow as tf
import numpy as np
from pymanopt import Problem
from pymanopt.solvers import SteepestDescent
from pymanopt.manifolds import Euclidean, Product
X = tf.placeholder( tf.float32, [ 3, 100 ] )
Y = tf.placeholder( tf.float32, [ 3, 1 ] )
w = tf.Variable(tf.zeros([3, 1]))
b = tf.Variable(tf.zeros([1, 1]))
cost = tf.reduce_mean(tf.square(Y - tf.matmul(tf.transpose(w), X) - b))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
solver = SteepestDescent()
manifold = Product([Euclidean(3, 1), Euclidean(1, 1)])
Xt = np.random.randn(3, 100).astype('float32')
Yt = (Xt[0:1, :] - 2*Xt[1:2, :] + np.random.randn(1, 100) + 5).astype('float32')
problem = Problem( manifold = manifold, cost = cost, arg = [w, b], verbosity=1 )
solution = solver.solve( problem)
print(solution)
If possible, can anyone help me work with this toy example. I want to know how to accomodate Xt and Yt which are inputs. Note that my computational graph is defined using placeholders.
Thanks :)
In [1]: import autograd.numpy as np
In [2]: from pymanopt.manifolds import Stiefel
...: from pymanopt import Problem
...: from pymanopt.solvers import SteepestDescent
...:
In [3]: # (1) Instantiate a manifold
...: manifold = Stiefel(5, 2)
...:
...: # (2) Define the cost function (here using autograd.numpy)
...: def cost(X): return np.sum(X)
...:
...: problem = Problem(manifold=manifold, cost=cost)
...:
...: # (3) Instantiate a Pymanopt solver
...: solver = SteepestDescent()
...:
...: # let Pymanopt do the rest
...: Xopt = solver.solve(problem)
...: print(Xopt)