In [1]:
%pylab inline
In [3]:
import random
In [37]:
def multiply(a, b):
return a * b
def add(a, b):
return a + b
def circut(a, b, c):
d = add(a, b)
return multiply(d, c)
In [27]:
def random_max(fn, a, b, niter=100):
tweak_amount = 0.01
best_out = None
best_a = a
best_b = b
values = []
for i in xrange(niter):
na = a + tweak_amount + (random.random() * 2 - 1)
nb = b + tweak_amount + (random.random() * 2 - 1)
out = fn(na, nb)
values.append(out)
if best_out is None or out > best_out:
best_out = out
best_a = na
best_b = nb
return values, best_out, best_a, best_b
In [31]:
values, best, a, b = random_max(multiply, -2, 3)
plot(values)
Out[31]:
In [32]:
def agradient_max(fn, a, b, niter=100):
tweak_amount = 0.01
best_out = None
best_a = a
best_b = b
values = []
for i in xrange(niter):
ga = b
gb = a
a += tweak_amount * ga
b += tweak_amount * gb
out = fn(a, b)
values.append(out)
if best_out is None or out > best_out:
best_out = out
best_a = a
best_b = b
return values, best_out, best_a, best_b
In [34]:
values, best, a, b = agradient_max(multiply, -2, 3)
plot(values)
Out[34]:
In [39]:
f = circut(-2, 5, -4)
print f
In [ ]: