def test_operation_and_placeholder_gradients(): a_array = np.ones([10, 5]) b_array = np.ones([5, 10]) a_sp = sp.Variable(a_array) b_sp = sp.Variable(b_array) a = sp.Placeholder() b = sp.Placeholder() y = sp.Lazy(sp.matmul)(a, b) a.assign_value(a_sp) b.assign_value(b_sp) result = y.run() grads = sp.get_gradients(result) sp_results = [result.array, grads[a_sp], grads[b_sp]] def func(a, b): return np.matmul(a, b) y_np = func(a_array, b_array) args = [a_array, b_array] num_grads = numgrads(func, args, n=1, delta=1) num_results = [y_np, num_grads[0], num_grads[1]] for spval, numval in zip(sp_results, num_results): error = rmse(spval, numval) assert error < EPS, f"rmse = {error}"
def test_operation_and_placeholder_result(): a_array = np.ones([10, 5]) b_array = np.ones([5, 10]) a = sp.Placeholder() b = sp.Placeholder() c = sp.Lazy(sp.matmul)(a, b) d = sp.Lazy(sp.mul)(c, sp.Variable(np.array(5))) a.assign_value(sp.Variable(a_array)) b.assign_value(sp.Variable(b_array)) result = d.run() # runs the graph assert np.all(result.array - np.matmul(a_array, b_array) * 5) == 0, "invalid result"
def test_learnable_and_get_learnables(): """Test that sp.get_learnables correctly obtains sp.Variables that have been flagged by sp.learnable(). """ param_1 = sp.learnable(sp.Variable(np.array(5))) param_2 = sp.learnable(sp.Variable(np.array(10))) a = sp.Placeholder() b = sp.Placeholder() y = sp.Lazy(sp.matmul)(a, b) y = sp.Lazy(sp.mul)(y, param_1) y = sp.Lazy(sp.add)(y, param_2) params = sp.get_learnables(y) assert params == [param_1, param_2], "Did not get params."
def test_operation_and_placeholder_basic(): x_in = sp.Placeholder() x = sp.Lazy(lambda x: x ** 2)(x_in) x_in.assign_value(3) assert x.run() == 9, "Expecting a value of 9"