Skip to content
Snippets Groups Projects
Commit 3ee2ed54 authored by Alex Spies's avatar Alex Spies
Browse files

add public test files for students on colab

parent 15478281
No related branches found
No related tags found
No related merge requests found
OK_FORMAT = True
test = { 'name': '1. Function 1 Minima Check',
'points': 2,
'suites': [ { 'cases': [ { 'code': '>>> # Check with minimum\n>>> f1_check_minimum(B, a, b)\nTrue',
'failure_message': 'F1 Minimum Check (with minimum) Test Failed',
'hidden': False,
'locked': False,
'points': 0,
'success_message': 'F1 Minimum Check (with minimum) Test Passed'}],
'scored': True,
'setup': '',
'teardown': '',
'type': 'doctest'}]}
OK_FORMAT = True
test = { 'name': '2.a Method of Finite Differences',
'points': 4,
'suites': [ { 'cases': [ { 'code': '>>> B = np.array([[4, -2], [-2, 4]])\n'
'>>> a = np.array([[0], [1]])\n'
'>>> b = np.array([[-2], [1]])\n'
'>>> def f1(x):\n'
'... return float(x.T @ B @ x - x.T @ x + a.T @ x - b.T @ x)\n'
'>>> dummy_input = np.array([[2.5], [3.5]])\n'
'>>> output = grad_fd(f1, dummy_input)\n'
'>>> \n'
'>>> target_output = np.array([[3.00003, 11.00003]])\n'
'>>> np.isclose(output, target_output, atol=1e-3).all()\n'
'True',
'failure_message': 'Finite Differences on f1 Test Failed',
'hidden': False,
'locked': False,
'points': 1,
'success_message': 'Finite Differences on f1 Test Passed'},
{ 'code': '>>> B = np.array([[4, -2], [-2, 4]])\n'
'>>> a = np.array([[0], [1]])\n'
'>>> b = np.array([[-2], [1]])\n'
'>>> def f2(x):\n'
'... return float(np.cos((x - b).T @ (x - b)) + (x - a).T @ B @ (x - a))\n'
'>>> dummy_input = np.array([[2.5], [3.5]])\n'
'>>> output = grad_fd(f2, dummy_input)\n'
'>>> \n'
'>>> target_output = np.array([[1.18572957, 5.10321673]])\n'
'>>> np.isclose(output, target_output, atol=1e-3).all()\n'
'True',
'failure_message': 'Finite Differences on f2 Test Failed',
'hidden': False,
'locked': False,
'points': 0,
'success_message': 'Finite Differences on f2 Test Passed'}],
'scored': True,
'setup': '',
'teardown': '',
'type': 'doctest'}]}
OK_FORMAT = True
test = {'name': 'Gradient Descent Initialization', 'points': 2, 'suites': [{'cases': [], 'scored': True, 'setup': '', 'teardown': '', 'type': 'doctest'}]}
OK_FORMAT = True
test = { 'name': 'Gradient Descent',
'points': 4,
'suites': [ { 'cases': [ { 'code': '>>> c = np.array([[4], [5]])\n'
'>>> dummy_fn = lambda x: float(c.T @ x)\n'
'>>> dummy_fn_grad = lambda x: c.T\n'
'>>> dummy_start_x, dummy_start_y = 1.0, 2.0\n'
'>>> \n'
'>>> trajectory, minimum_loc, minimum_value = gradient_descent(dummy_fn, dummy_fn_grad, start_x=dummy_start_x, start_y=dummy_start_y, lr=0.01, '
'n_steps=5, silent=True)\n'
'>>> \n'
'>>> target_trajectory = [np.array([[1], [2]]), np.array([[0.96], [1.95]]), np.array([[0.92], [1.9]]),\n'
'... np.array([[0.88], [1.85]]), np.array([[0.84], [1.8]]), np.array([[0.8], [1.75]])]\n'
'>>> \n'
'>>> np.array([np.isclose(target.flatten(), output.flatten(), atol=1e-3) for target, output in zip(target_trajectory, trajectory)]).all()\n'
'True',
'failure_message': 'Gradient Descent Trajectory Test Failed',
'hidden': False,
'locked': False,
'points': 0,
'success_message': 'Gradient Descent Trajectory Test Passed'},
{ 'code': '>>> c = np.array([[4], [5]])\n'
'>>> dummy_fn = lambda x: float(c.T @ x)\n'
'>>> dummy_fn_grad = lambda x: c.T\n'
'>>> dummy_start_x, dummy_start_y = 1.0, 2.0\n'
'>>> \n'
'>>> trajectory, minimum_loc, minimum_value = gradient_descent(dummy_fn, dummy_fn_grad, start_x=dummy_start_x, start_y=dummy_start_y, lr=0.01, '
'n_steps=5, silent=True)\n'
'>>> target_minimum_loc = np.array([[0.8], [1.75]])\n'
'>>> np.isclose(target_minimum_loc, minimum_loc, atol=1e-3).all()\n'
'True',
'failure_message': 'Gradient Descent Minimum Location Test Failed',
'hidden': False,
'locked': False,
'points': 0,
'success_message': 'Gradient Descent Minimum Location Test Passed'},
{ 'code': '>>> c = np.array([[4], [5]])\n'
'>>> dummy_fn = lambda x: float(c.T @ x)\n'
'>>> dummy_fn_grad = lambda x: c.T\n'
'>>> dummy_start_x, dummy_start_y = 1.0, 2.0\n'
'>>> \n'
'>>> trajectory, minimum_loc, minimum_value = gradient_descent(dummy_fn, dummy_fn_grad, start_x=dummy_start_x, start_y=dummy_start_y, lr=0.01, '
'n_steps=5, silent=True)\n'
'>>> target_minimum_value = 11.949999999999998\n'
'>>> np.isclose(target_minimum_value, minimum_value, atol=1e-3)\n'
'True',
'failure_message': 'Gradient Descent Minimum Value Test Failed',
'hidden': False,
'locked': False,
'points': 0,
'success_message': 'Gradient Descent Minimum Value Test Passed'}],
'scored': True,
'setup': '',
'teardown': '',
'type': 'doctest'}]}
OK_FORMAT = True
test = { 'name': 'Q2.b.i Gradients of the Functions - f1',
'points': 1,
'suites': [ { 'cases': [ { 'code': '>>> B = np.array([[4, -2], [-2, 4]])\n'
'>>> a = np.array([[0], [1]])\n'
'>>> b = np.array([[-2], [1]])\n'
'>>> dummy_input = np.array([[2.5], [3.5]])\n'
'>>> output = f1_grad_exact(dummy_input)\n'
'>>> \n'
'>>> target_output = np.array([[3.0, 11.0]])\n'
'>>> np.isclose(output, target_output, atol=1e-3).all()\n'
'True',
'failure_message': 'Exact Gradients of f1 Test Failed',
'hidden': False,
'locked': False,
'points': 0,
'success_message': 'Exact Gradients of f1 Test Passed'}],
'scored': True,
'setup': '',
'teardown': '',
'type': 'doctest'}]}
OK_FORMAT = True
test = { 'name': 'Q2.b.ii Gradients of the Functions - f2',
'points': 1,
'suites': [ { 'cases': [ { 'code': '>>> B = np.array([[4, -2], [-2, 4]])\n'
'>>> a = np.array([[0], [1]])\n'
'>>> b = np.array([[-2], [1]])\n'
'>>> dummy_input = np.array([[2.5], [3.5]])\n'
'>>> output = f2_grad_exact(dummy_input)\n'
'>>> \n'
'>>> target_output = np.array([[1.1858, 5.1032]])\n'
'>>> np.isclose(output, target_output, atol=1e-3).all()\n'
'True',
'failure_message': 'Exact Gradients of f2 Test Failed',
'hidden': False,
'locked': False,
'points': 0,
'success_message': 'Exact Gradients of f2 Test Passed'}],
'scored': True,
'setup': '',
'teardown': '',
'type': 'doctest'}]}
OK_FORMAT = True
test = { 'name': 'Q2.b.iii Gradients of the Functions - f3',
'points': 2,
'suites': [ { 'cases': [ { 'code': '>>> B = np.array([[4, -2], [-2, 4]])\n'
'>>> a = np.array([[0], [1]])\n'
'>>> b = np.array([[-2], [1]])\n'
'>>> dummy_input = np.array([[2.5], [3.5]])\n'
'>>> output = f3_grad_exact(dummy_input)\n'
'>>> \n'
'>>> target_output = np.array([[0.02699379, 0.03779876]])\n'
'>>> np.isclose(output, target_output, atol=1e-3).all()\n'
'True',
'failure_message': 'Exact Gradients of f2 Test Failed',
'hidden': False,
'locked': False,
'points': 0,
'success_message': 'Exact Gradients of f2 Test Passed'}],
'scored': True,
'setup': '',
'teardown': '',
'type': 'doctest'}]}
OK_FORMAT = True
test = { 'name': 'Q4.a Squared Distance Function',
'points': 1,
'suites': [ { 'cases': [ { 'code': '>>> p = np.random.rand(4)\n>>> q = np.random.rand(4)\n>>> x = np.random.rand(4)\n>>> () == sq_dist_fwd(p, q, x).shape\nTrue',
'failure_message': 'Squared Distance Function Output Shape Test Failed',
'hidden': False,
'locked': False,
'points': 0,
'success_message': 'Squared Distance Function Output Shape Test Passed'}],
'scored': True,
'setup': '',
'teardown': '',
'type': 'doctest'}]}
OK_FORMAT = True
test = { 'name': 'Q4.c Squared Distance Function Gradients',
'points': 2,
'suites': [ { 'cases': [ { 'code': '>>> p = np.random.rand(4)\n>>> q = np.random.rand(4)\n>>> x = np.random.rand(4)\n>>> (4,) == sq_dist_grad(p,q,x).shape \nTrue',
'failure_message': 'Squared Distance Gradient Function Shape Test Failed',
'hidden': False,
'locked': False,
'points': 0,
'success_message': 'Squared Distance Gradient Function Shape Test Passed'},
{ 'code': '>>> #! think I prefer to test for us of "grad" from autograd, rather than do memory test\n'
'>>> # 1. this is more robust ; 2. we can them provide demo of the memory use as proof of utility \n'
'>>> # """ # BEGIN TEST CONFIG\n'
'>>> # name: test_sq_dist_grad_memory\n'
'>>> # points: 2\n'
'>>> # hidden: true \n'
">>> # success_message: 'Squared Distance Gradient Memory Test Passed'\n"
">>> # failure_message: 'Squared Distance Gradient Memory Test Failed'\n"
'>>> # """ # END TEST CONFIG\n'
'>>> # %%capture result\n'
'>>> # %load_ext memory_profiler\n'
'>>> # a,b,x = np.random.randn(100), np.random.randn(100), np.random.randn(100)\n'
'>>> # %memit manual_grad_vec(a,b,x)\n'
'>>> # %%capture result2\n'
'>>> # %memit grad(sq_dist_fwd, 2)(a,b,x) \n'
">>> # get_mem_usage = lambda x: float(str(x)[12:str(x).find('MiB')])\n"
'>>> # 3*get_mem_usage(result) < get_mem_usage(result2)\n',
'hidden': False,
'locked': False}],
'scored': True,
'setup': '',
'teardown': '',
'type': 'doctest'}]}
OK_FORMAT = True
test = { 'name': 'Q4.d Squared Distance Autodiff',
'points': 2,
'suites': [ { 'cases': [ { 'code': '>>> our_grad = grad(sq_dist, 0)(z) # This is calling our custom gradient function\n'
'>>> their_grad = grad(sq_dist_fwd, 2)(p,q,z) # This is uses slow gradient primitives\n'
'>>> np.allclose(our_grad, their_grad, atol=1e-3)\n'
'True',
'failure_message': 'Squared Distance Autodiff Value Test Failed',
'hidden': False,
'locked': False,
'points': 2,
'success_message': 'Squared Distance Autodiff Value Test Passed'}],
'scored': True,
'setup': '',
'teardown': '',
'type': 'doctest'}]}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment