Skip to content

Instantly share code, notes, and snippets.

@gwding
Created December 15, 2016 02:22
Show Gist options
  • Save gwding/12342bf7f6dc5a6eb54cc6a71c64a862 to your computer and use it in GitHub Desktop.
Save gwding/12342bf7f6dc5a6eb54cc6a71c64a862 to your computer and use it in GitHub Desktop.
Use theano to do binary search over continuous monotonic increasing function
# Use theano to do binary search over continuous monotonic increasing function
import theano
import theano.tensor as T
import numpy as np
def func(x):
return T.exp(x)
magnify = 2.
xmin_init = 1e-16
xmax_init = np.inf
x_init = 1.
y_target = 100.
x = T.scalar()
y = T.scalar()
xmin = T.scalar()
xmax = T.scalar()
x_shared = theano.shared(x_init)
xmin_shared = theano.shared(xmin_init)
xmax_shared = theano.shared(xmax_init)
new_xmin = T.switch(T.lt(func(x), y), x, xmin)
new_xmax = T.switch(T.gt(func(x), y), x, xmax)
new_x = T.switch(T.isinf(new_xmax), magnify * x, (new_xmax + new_xmin) / 2.)
givens = [(x, x_shared), (xmin, xmin_shared), (xmax, xmax_shared)]
updates = [(x_shared, new_x), (xmin_shared, new_xmin), (xmax_shared, new_xmax)]
run_update = theano.function([y], func(x), givens=givens, updates=updates)
for ii in range(20):
print "y:", run_update(y_target), "x:", x_shared.get_value()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment