def test_no_args(): adobj = AD(lambda x: x) with pytest.raises(TypeError): adobj._forward()
def test_self_m(): adobj = AD(lambda x, y: x**2 + y + y) adobj._forward(0, 0) assert adobj.m == 1
def test_set_seed_wrong_dim(): adobj = AD(lambda x: x + 5) adobj.set_seed([2, 1]) with pytest.raises(ValueError): adobj._forward(3)
def test_self_n(): adobj = AD(lambda x, y: x + y) adobj._forward(1, 2) assert adobj.n == 2
def my_sum(a): return a.sum() ad_object = AD(my_sum) # showcase reverse better than forward when inputs increase fwd_times = [] rev_times = [] ninputs = range(1, 10000, 100) for n in range(1, 10000, 100): inp = np.array([i for i in range(n)]) start_time = time.time() ad_object._forward(inp) runtime = time.time() - start_time fwd_times.append(runtime) start_time = time.time() ad_object._reverse(inp) runtime = time.time() - start_time rev_times.append(runtime) # plot results plt.plot(ninputs, fwd_times, label='forward') plt.plot(ninputs, rev_times, label='backward') plt.legend() plt.title('Comparing times under different AD modes') plt.xlabel('Number of inputs') plt.ylabel('Elapsed time in seconds')