def test_steepest_descent_armijo(): """Should trigger the line search """ initial_guess = Array([Number(2), Number(1)]) _, xstar, _, _ = optimizations.steepest_descent(rosenbrock, initial_guess, iterations=50)
def test_steepest_descent(): initial_guess = Array([Number(1.1), Number(1.1)]) _, xstar, _, _ = optimizations.steepest_descent(bowl, initial_guess, iterations=400) # print(xstar) assert xstar[0].val == pytest.approx(1, abs=1e-3) assert xstar[1].val == pytest.approx(1, abs=1e-3)
def inner_func(*args, **kwargs): # Check if args[0] has len. If so, apply the function elementwise and return an array # rather than a Number try: value = func(*args, **kwargs) deriv = deriv_func(*args, **kwargs) return Number(value, deriv) except AttributeError: vals = [func(element, *args[1:], **kwargs) for element in args[0]] derivs = [deriv_func(element, *args[1:], **kwargs) for element in args[0]] numbers = [Number(val, deriv) for val, deriv in zip(vals, derivs)] return Array(numbers)
def test_steepest_descent_scalar(): initial_guess = Number(1.1) xstar, _, _ = optimizations.steepest_descent(quadratic, initial_guess, iterations=400) print(xstar) assert xstar.val == pytest.approx(1, abs=1e-3)
def test_dot(): q = Array((Number(0), 0)) assert q.dot(q).val == 0
def test_neq(): q = Array((Number(0), 0)) w = Array((Number(0), 1)) assert q != w
def test_neq_mixed(): q = Array((Number(0), 0)) assert q != 'a' assert q != 1
def test_eq(): q = Array((Number(0), 0)) assert q == q
def test_array_converts_to_number(): q = Array((Number(0), 0)) assert q[0].val == 0 assert q[1].val == 0
def test_array_func(): q = Array((Number(0), Number(1))) w = operations.exp(q) assert w[0].val == pytest.approx(1) assert w[1].val == pytest.approx(np.exp(1))
"""Tests for the Array class """ import sys import pytest sys.path.append('..') import autodiff.operations as operations from autodiff.structures import Number from autodiff.structures import Array import numpy as np num2 = Number(2) num3 = Number(3) def test_len(): q = Array((num2, num3)) assert len(q) == 2 def test_repr(): q = Array((num2, num3)) assert repr(q) == 'Array([Number(val=2) Number(val=3)])' def test_str(): q = Array((num2, num3)) assert str(q) == '[Number(val=2) Number(val=3)]'
def test_bfgs_scalar_verbose(): initial_guess = Number(2) xstar, _, _ = optimizations.bfgs(quadratic, initial_guess, verbose=True) assert xstar.val == pytest.approx(1)
def test_bfgs_verbose(): initial_guess = Array([Number(2), Number(1)]) xstar, _, _ = optimizations.bfgs(rosenbrock, initial_guess, verbose=True) assert xstar[0].val == pytest.approx(1) assert xstar[1].val == pytest.approx(1)
def test_bfgs_scalar_correct_start(): initial_guess = Number(1) xstar, _, _ = optimizations.bfgs(quadratic, initial_guess) assert xstar.val == pytest.approx(1)
def test_bfgs_correct_start(): initial_guess = Array([Number(1), Number(1)]) xstar, _, _ = optimizations.bfgs(rosenbrock, initial_guess) assert xstar[0].val == pytest.approx(1) assert xstar[1].val == pytest.approx(1)
jacobians = [] x0 = initial_guess fxn = func(initial_guess) fpxn = fxn.jacobian(initial_guess) x1 = x0 - fxn / fpxn jacobians.append(fpxn) while fxn.val > 1e-7: x0 = x1 fxn = func(x0) fpxn = fxn.jacobian(x0) jacobians.append(fpxn) x1 = x0 - fxn / fpxn return x1, jacobians if __name__ == '__main__': x0 = Number(5) xstar, jacobians = newtons_method(func, x0) print(xstar, jacobians[-1])