def test_rop_lop(): mx = matrix("mx") mv = matrix("mv") v = vector("v") y = matrix_inverse(mx).sum(axis=0) yv = aesara.gradient.Rop(y, mx, mv) rop_f = function([mx, mv], yv) sy, _ = aesara.scan( lambda i, y, x, v: (aesara.gradient.grad(y[i], x) * v).sum(), sequences=aet.arange(y.shape[0]), non_sequences=[y, mx, mv], ) scan_f = function([mx, mv], sy) rng = np.random.default_rng(utt.fetch_seed()) vx = np.asarray(rng.standard_normal((4, 4)), aesara.config.floatX) vv = np.asarray(rng.standard_normal((4, 4)), aesara.config.floatX) v1 = rop_f(vx, vv) v2 = scan_f(vx, vv) assert _allclose(v1, v2), f"ROP mismatch: {v1} {v2}" raised = False try: aesara.gradient.Rop(aesara.clone_replace(y, replace={mx: break_op(mx)}), mx, mv) except ValueError: raised = True if not raised: raise Exception( "Op did not raised an error even though the function" " is not differentiable" ) vv = np.asarray(rng.uniform(size=(4,)), aesara.config.floatX) yv = aesara.gradient.Lop(y, mx, v) lop_f = function([mx, v], yv) sy = aesara.gradient.grad((v * y).sum(), mx) scan_f = function([mx, v], sy) v1 = lop_f(vx, vv) v2 = scan_f(vx, vv) assert _allclose(v1, v2), f"LOP mismatch: {v1} {v2}"
def test_rop_lop(): mx = tensor.matrix("mx") mv = tensor.matrix("mv") v = tensor.vector("v") y = matrix_inverse(mx).sum(axis=0) yv = tensor.Rop(y, mx, mv) rop_f = function([mx, mv], yv) sy, _ = theano.scan( lambda i, y, x, v: (tensor.grad(y[i], x) * v).sum(), sequences=tensor.arange(y.shape[0]), non_sequences=[y, mx, mv], ) scan_f = function([mx, mv], sy) rng = np.random.RandomState(utt.fetch_seed()) vx = np.asarray(rng.randn(4, 4), theano.config.floatX) vv = np.asarray(rng.randn(4, 4), theano.config.floatX) v1 = rop_f(vx, vv) v2 = scan_f(vx, vv) assert _allclose(v1, v2), "ROP mismatch: %s %s" % (v1, v2) raised = False try: tensor.Rop(theano.clone(y, replace={mx: break_op(mx)}), mx, mv) except ValueError: raised = True if not raised: raise Exception(("Op did not raised an error even though the function" " is not differentiable")) vv = np.asarray(rng.uniform(size=(4, )), theano.config.floatX) yv = tensor.Lop(y, mx, v) lop_f = function([mx, v], yv) sy = tensor.grad((v * y).sum(), mx) scan_f = function([mx, v], sy) v1 = lop_f(vx, vv) v2 = scan_f(vx, vv) assert _allclose(v1, v2), "LOP mismatch: %s %s" % (v1, v2)