コード例 #1
0
ファイル: test_gd.py プロジェクト: philiplessner/FunctionalML
    def test_sumsq(self):
        def f(x_i):
            return sum(x_ij**2 for x_ij in x_i)

        def df(x_i):
            return [2 * x_ij for x_ij in x_i]
        
        x0 = [5., 4.]
        tol = 1.e-6
        a = until_within_tol((f(e) for e in gradient_descent(df, x0)), tolerance=tol)
        b = list(a)
        self.assertLessEqual(abs(b[-2] - b[-1]), tol)
コード例 #2
0
    def test_sumsq(self):
        def f(x_i):
            return sum(x_ij**2 for x_ij in x_i)

        def df(x_i):
            return [2 * x_ij for x_ij in x_i]

        x0 = [5., 4.]
        tol = 1.e-6
        a = until_within_tol((f(e) for e in gradient_descent(df, x0)),
                             tolerance=tol)
        b = list(a)
        self.assertLessEqual(abs(b[-2] - b[-1]), tol)
コード例 #3
0
ファイル: expxy.py プロジェクト: philiplessner/FunctionalML
        
    
def g(x):
    """
    f(x, y) = -exp(-x^3 / 3 + x - y^2) has min at (1,0), saddle point at (-1,0)
    """
    return -math.exp(x[0]**3/-3 + x[0] - x[1]**2)
        

def dg(x):
    """just the gradient"""
    return ((1 - x[0]**2) * g(x), -2 * x[1] * g(x))
    
    
tol = 1.e-6
b = until_within_tol((g(e) for e in gradient_descent(dg, random_point())),
                     tolerance=tol)
print(list(b))

alphas = [1., 0.3, 0.1, 0.03, 0.01, 0.003, 0.001]
niter = 100

plot_lrates(g, dg, random_point(), alphas, niter)

#x0 = random_point()
x0 = [-0.2, 0.5]
result = list(take(50, ((g(e), e) for e in gradient_descent(dg, x0)) ))
xs = ['x' + unicode(i) for i in xrange(len(x0))]
table = pylsytable2(['y'] + xs)
table.add_data('y', list(pluck(0, result)), '{:.2e}')
for i, x in enumerate(xs):
    table.add_data(x, list(pluck(i,pluck(1, result))), '{:.2e}')
コード例 #4
0

def g(x):
    """
    f(x, y) = -exp(-x^3 / 3 + x - y^2) has min at (1,0), saddle point at (-1,0)
    """
    return -math.exp(x[0]**3 / -3 + x[0] - x[1]**2)


def dg(x):
    """just the gradient"""
    return ((1 - x[0]**2) * g(x), -2 * x[1] * g(x))


tol = 1.e-6
b = until_within_tol((g(e) for e in gradient_descent(dg, random_point())),
                     tolerance=tol)
print(list(b))

alphas = [1., 0.3, 0.1, 0.03, 0.01, 0.003, 0.001]
niter = 100

plot_lrates(g, dg, random_point(), alphas, niter)

#x0 = random_point()
x0 = [-0.2, 0.5]
result = list(take(50, ((g(e), e) for e in gradient_descent(dg, x0))))
xs = ['x' + unicode(i) for i in xrange(len(x0))]
table = pylsytable2(['y'] + xs)
table.add_data('y', list(pluck(0, result)), '{:.2e}')
for i, x in enumerate(xs):
    table.add_data(x, list(pluck(i, pluck(1, result))), '{:.2e}')