def test_einsum(): x = cgt.tensor3() y = cgt.tensor3() sizes = {'i': 2, 'j': 3, 'k': 5, 'l': 7} xaxes = 'ijk' yaxes = 'ikl' zaxes = 'ijl' for i in xrange(10): xperm = xaxes (yperm, zperm) = permaxes = [[chars[i] for i in np.random.permutation(3)] for chars in [yaxes, zaxes]] desc = "%s,%s->%s" % tuple("".join(chars) for chars in [xperm] + permaxes) z = cgt.einsum(desc, x, y) xval = nr.randn(*(sizes[c] for c in xperm)) yval = nr.randn(*(sizes[c] for c in yperm)) np.testing.assert_allclose(cgt.numeric_eval(z, { x: xval, y: yval }), np.einsum(desc, xval, yval), atol={ "single": 1e-3, "double": 1e-6 }[cgt.get_precision()])
def test_multi_output(): for x in (cgt.scalar('x'), cgt.vector('x'), cgt.matrix('x')): for cls in (SinCos, SinCos2): y, z = core.unpack(core.Result(cls(), [x])) xnum = np.ones((3, ) * x.ndim, cgt.floatX) correct = (np.sin(xnum), np.cos(xnum)) yznum = cgt.numeric_eval([y, z], {x: xnum}) np.testing.assert_allclose(yznum, correct) f = cgt.function([x], [y, z]) np.testing.assert_allclose(f(xnum), correct)
def test_linreg(): cgt.reset_config() cgt.set_precision('double') N = 10 K = 3 Xval = np.random.randn(N, K) wval = np.random.randn(K) bval = np.random.randn() yval = np.random.randn(N) X_nk = cgt.matrix("X") y_n = cgt.vector("y") w_k = cgt.vector("w") b = cgt.scalar(name="b") ypred = cgt.dot(X_nk, w_k) + b err = cgt.sum(cgt.square(ypred - y_n)) g = cgt.grad(err, [w_k, b]) g_simple, an, _ = cgt.core.simplify_and_analyze(g) print "Loss function:" cgt.print_tree([err]) print "Gradient:" cgt.print_tree(g) print "Gradient simplified" cgt.print_tree( g_simple, nodefn=lambda node, o: o.write(" " + an["node2hash"][node][:5])) print "-------" d = {X_nk: Xval, w_k: wval, b: bval, y_n: yval} np.testing.assert_allclose(cgt.numeric_eval(err, d), np.linalg.norm(Xval.dot(wval) + bval - yval)**2) np.testing.assert_allclose(cgt.numeric_eval(g[0], d), 2 * Xval.T.dot(Xval.dot(wval) + bval - yval)) np.testing.assert_allclose(cgt.numeric_eval(g[1], d), 2 * np.sum(Xval.dot(wval) + bval - yval, 0))
def test_multi_output(): for x in (cgt.scalar('x'), cgt.vector('x'), cgt.matrix('x')): for cls in (SinCos, SinCos2): y,z = core.unpack(core.Result(cls(), [x])) xnum = np.ones((3,)*x.ndim, cgt.floatX) correct = (np.sin(xnum),np.cos(xnum)) yznum = cgt.numeric_eval([y,z], {x:xnum}) np.testing.assert_allclose(yznum, correct) f = cgt.function([x],[y,z]) np.testing.assert_allclose(f(xnum), correct)
def test_linreg(): N = 10 K = 3 Xval = np.random.randn(N,K) wval = np.random.randn(K) bval = np.random.randn() yval = np.random.randn(N) X_nk = cgt.matrix("X") y_n = cgt.vector("y") w_k = cgt.vector("w") b = cgt.scalar(name="b") ypred = cgt.dot(X_nk, w_k) + b err = cgt.sum(cgt.square(ypred - y_n)) g = cgt.grad(err, [w_k, b]) g_simple,an,_ = cgt.core.simplify_and_analyze(g) print "Loss function:" cgt.print_tree([err]) print "Gradient:" cgt.print_tree(g) print "Gradient simplified" cgt.print_tree(g_simple, nodefn=lambda node,o: o.write(" " + an["node2hash"][node][:5])) print "-------" d = {X_nk : Xval, w_k : wval, b : bval, y_n : yval} np.testing.assert_allclose(cgt.numeric_eval(err,d), np.linalg.norm(Xval.dot(wval) + bval - yval)**2, atol={"single":1e-3,"double":1e-6}[cgt.get_precision()]) np.testing.assert_allclose(cgt.numeric_eval(g[0],d), 2 * Xval.T.dot(Xval.dot(wval) + bval - yval), atol={"single":1e-3,"double":1e-6}[cgt.get_precision()]) np.testing.assert_allclose(cgt.numeric_eval(g[1],d), 2 * np.sum(Xval.dot(wval) + bval - yval, 0), atol={"single":1e-3,"double":1e-6}[cgt.get_precision()])
def test_einsum(): x = cgt.tensor3() y = cgt.tensor3() sizes = {"i": 2, "j": 3, "k": 5, "l": 7} xaxes = "ijk" yaxes = "ikl" zaxes = "ijl" for i in xrange(10): xperm = xaxes (yperm, zperm) = permaxes = [[chars[i] for i in np.random.permutation(3)] for chars in [yaxes, zaxes]] desc = "%s,%s->%s" % tuple("".join(chars) for chars in [xperm] + permaxes) z = cgt.einsum(desc, x, y) xval = nr.randn(*(sizes[c] for c in xperm)) yval = nr.randn(*(sizes[c] for c in yperm)) np.testing.assert_allclose( cgt.numeric_eval(z, {x: xval, y: yval}), np.einsum(desc, xval, yval), atol={"single": 1e-3, "double": 1e-6}[cgt.get_precision()], )
def test_einsum(): cgt.reset_config() cgt.set_precision("double") x = cgt.tensor3() y = cgt.tensor3() sizes = {'i':2,'j':3,'k':5,'l':7} xaxes = 'ijk' yaxes = 'ikl' zaxes = 'ijl' for i in xrange(10): xperm = xaxes (yperm,zperm) = permaxes = [[chars[i] for i in np.random.permutation(3)] for chars in [yaxes,zaxes]] desc = "%s,%s->%s"%tuple("".join(chars) for chars in [xperm] + permaxes) z = cgt.einsum(desc, x, y) xval = nr.randn(*(sizes[c] for c in xperm)) yval = nr.randn(*(sizes[c] for c in yperm)) np.testing.assert_allclose( cgt.numeric_eval(z, {x : xval, y : yval}), np.einsum(desc, xval, yval))
def test_stack(): x = cgt.scalar() y = cgt.scalar() z = cgt.scalar() s0 = cgt.stack([x, y, z], axis=0) assert cgt.numeric_eval(s0, {x: 1, y: 2, z: 3}).shape == (3, ) x = cgt.vector() y = cgt.vector() z = cgt.vector() v0 = cgt.stack([x, y, z], axis=0) assert cgt.numeric_eval(v0, { x: np.zeros(2), y: np.zeros(2), z: np.zeros(2) }).shape == (3, 2) v1 = cgt.stack([x, y, z], axis=1) assert cgt.numeric_eval(v1, { x: np.zeros(2), y: np.ones(2), z: np.zeros(2) }).shape == (2, 3) x = cgt.matrix() y = cgt.matrix() z = cgt.matrix() m0 = cgt.stack([x, y, z], axis=0) assert cgt.numeric_eval(m0, { x: np.zeros((2, 4)), y: np.zeros((2, 4)), z: np.zeros((2, 4)) }).shape == (3, 2, 4) m1 = cgt.stack([x, y, z], axis=1) assert cgt.numeric_eval(m1, { x: np.zeros((2, 4)), y: np.zeros((2, 4)), z: np.zeros((2, 4)) }).shape == (2, 3, 4) m2 = cgt.stack([x, y, z], axis=2) assert cgt.numeric_eval(m2, { x: np.zeros((2, 4)), y: np.zeros((2, 4)), z: np.zeros((2, 4)) }).shape == (2, 4, 3)
def test_stack(): x = cgt.scalar() y = cgt.scalar() z = cgt.scalar() s0 = cgt.stack([x, y, z], axis=0) assert cgt.numeric_eval(s0, {x: 1, y: 2, z: 3}).shape == (3,) x = cgt.vector() y = cgt.vector() z = cgt.vector() v0 = cgt.stack([x, y, z], axis=0) assert cgt.numeric_eval(v0, {x: np.zeros(2), y: np.zeros(2), z: np.zeros(2)}).shape == (3, 2) v1 = cgt.stack([x, y, z], axis=1) assert cgt.numeric_eval(v1, {x: np.zeros(2), y: np.ones(2), z: np.zeros(2)}).shape == (2, 3) x = cgt.matrix() y = cgt.matrix() z = cgt.matrix() m0 = cgt.stack([x, y, z], axis=0) assert cgt.numeric_eval(m0, {x: np.zeros((2, 4)), y: np.zeros((2, 4)), z: np.zeros((2, 4))}).shape == (3, 2, 4) m1 = cgt.stack([x, y, z], axis=1) assert cgt.numeric_eval(m1, {x: np.zeros((2, 4)), y: np.zeros((2, 4)), z: np.zeros((2, 4))}).shape == (2, 3, 4) m2 = cgt.stack([x, y, z], axis=2) assert cgt.numeric_eval(m2, {x: np.zeros((2, 4)), y: np.zeros((2, 4)), z: np.zeros((2, 4))}).shape == (2, 4, 3)