Beispiel #1
0
 def check_conv(precision):
     cgt.reset_config()
     cgt.set_precision(precision)
     f = cgt.function([], nn.conv2d(cgt.constant(x), cgt.constant(filt), kernelshape=(filtrows,filtcols), pad=(filtrows-1, filtcols-1)))
     out1 = f()
     # out1 = cgt.numeric_eval1(nn.conv2d(cgt.constant(x), cgt.constant(f), kersize=(filtrows,filtcols)), {})
     np.testing.assert_allclose(out, out1, atol={"single":1e-3,"double":1e-6}[precision])
Beispiel #2
0
def check_scalar_grads(precision, backend):
    cgt.reset_config()
    np.random.seed(0)
    cgt.set_precision(precision)
    cgt.core.update_config(backend=backend)
    x = cgt.scalar('x')
    y = cgt.scalar('y')
    z = cgt.scalar('z')
    vars = [x,y,z] #pylint: disable=W0622
    vals = nr.rand(len(vars))+1

    PROB2RESULT = {}

    for ((key,_), cls) in it.chain(
            it.izip(core.UNARY_INFO.items(),it.repeat(core.ElwiseUnary)),
            it.izip(core.BINARY_INFO.items(),it.repeat(core.ElwiseBinary))
            ):
        if key == "conj":
            print "skipping conj"
            continue
        utils.colorprint(utils.Color.YELLOW, "Testing %s\n"%key)
        if cls == core.ElwiseUnary:
            n_in = 1
            op = cls(key)
        else:
            n_in = 2
            op = cls(key, (True,True))
        inputvars = vars[0:n_in]
        inputvals = vals[0:n_in]
        out = core.Result(op, inputvars)
        f = cgt.function(inputvars, out)
        try:
            grads = cgt.grad(out, inputvars)
        except core.NonDifferentiable:
            print "nondiff"
            continue
        if DISPLAY:
            print "Function:"
            cgt.print_tree(out)
            print "Gradient original:"
            cgt.print_tree(grads)
            print "Gradient simplified:"
        grads_simple = core.simplify(grads)
        if DISPLAY: cgt.print_tree(grads_simple)
        gradf = cgt.function(inputvars, grads)
        eps = {"single":1e-4,"double":1e-9}[precision]
        nugrad = numeric_grad(lambda li: f(*li), inputvals,eps=eps) #pylint: disable=W0640
        cgtgrad = gradf(*inputvals)
        np.testing.assert_almost_equal(nugrad,cgtgrad,decimal={"single":3,"double":6}[precision])

        grad_count = core.count_nodes(grads_simple)
        PROB2RESULT[key] = {}
        PROB2RESULT[key]["grad"] = grad_count

    if DISPLAY:
        from thirdparty.tabulate import tabulate
        print tabulate([[key,val["grad"]] for (key,val) in PROB2RESULT.iteritems()],headers=["funcname","gradcount"])    
Beispiel #3
0
def test_multi_output():
    cgt.reset_config()
    cgt.set_precision("single")    
    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)
Beispiel #4
0
def test_multi_output():
    cgt.reset_config()
    cgt.set_precision("single")
    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)
Beispiel #5
0
 def check_conv(precision):
     cgt.reset_config()
     cgt.set_precision(precision)
     f = cgt.function([],
                      nn.conv2d(cgt.constant(x),
                                cgt.constant(filt),
                                kernelshape=(filtrows, filtcols),
                                pad=(filtrows - 1, filtcols - 1)))
     out1 = f()
     # out1 = cgt.numeric_eval1(nn.conv2d(cgt.constant(x), cgt.constant(f), kersize=(filtrows,filtcols)), {})
     np.testing.assert_allclose(out,
                                out1,
                                atol={
                                    "single": 1e-3,
                                    "double": 1e-6
                                }[precision])
Beispiel #6
0
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))
Beispiel #7
0
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))
Beispiel #8
0
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))
Beispiel #9
0
def check_affine_funcs(precision, backend):
    cgt.reset_config()
    np.random.seed(0)
    cgt.set_precision(precision)
    cgt.core.update_config(backend=backend)

    sA = np.array(nr.rand())
    sB = np.array(nr.rand())
    sC = np.array(nr.rand())
    mA = nr.randn(2, 3)
    mB = nr.randn(2, 3)
    mC = nr.randn(2, 3)

    for fn in [xplusx, _2x_plus_3x, xm1, onemx]:
        for arg in [sA, mA]:
            check_affine(fn, arg)

    check_affine(elem_mult2, mA, mB, mC)
    check_affine(elem_mult2, sA, sB, sC)
    check_affine(pyramid, sA, sB, sC)
    check_affine(pyramid, mA, mB, mC)
    check_affine(slisum1, mA)
    check_affine(slisum2, mA)
    check_affine(slisum3, mA)
    check_affine(slisum4, mA)
    check_affine(max0, mA)
    check_affine(max1, mA)
    check_affine(max2, mA)
    check_affine(fancysli0, mA)
    check_affine(sum10, mA)
    check_affine(sum01, mA)
    check_affine(repeat0, mA[0:1, :], nr.randn(7, 3))
    check_affine(repeat1, mA[:, 0:1], nr.randn(2, 7))

    M23 = mA
    M35 = nr.randn(3, 5)
    v3 = nr.randn(3)
    v13 = v3.reshape(1, 3)  #XXX
    v5 = nr.randn(5)
    v15 = v5.reshape(1, 5)  #XXX
    v3b = nr.randn(3)

    check_affine(matmat00, M23, M35)
    check_affine(matmat01, M23, M35.T)
    check_affine(matmat10, M23.T, M35)
    check_affine(matmat11, M23.T, M35.T)

    check_affine(matmat00a, M23, M35)
    check_affine(matmat01a, M23, M35.T)
    # check_affine(matmat10a, M23.T, M35)
    check_affine(matmat11a, M23.T, M35.T)

    check_affine(matvec, M23, v3)
    check_affine(vecvec, v3, v3b)
    check_affine(bcadd, M23, v13)
    check_affine(matmatplusvec, M23, M35, v15)
    check_affine(transpose, M23, nr.randn(3, 2))

    T235 = nr.randn(2, 3, 5)
    T235a = nr.randn(2, 3, 5)
    T257 = nr.randn(2, 5, 7)
    T2357 = nr.randn(2, 3, 5, 7)
    T2357a = nr.randn(2, 3, 5, 7)

    check_affine(transpose012, T235, T235a)
    check_affine(transpose021, T235, T235a.transpose(0, 2, 1))
    check_affine(transpose102, T235, T235a.transpose(1, 0, 2))
    check_affine(transpose0312, T2357, T2357a.transpose(0, 3, 1, 2))
    check_affine(transpose0231, T2357, T2357a.transpose(0, 2, 3, 1))

    check_affine(batchedmatmul, T235, T257)

    check_affine(flip0, M23, nr.randn(2, 3))
    check_affine(flip1, M23, nr.randn(2, 3))

    # check_affine(negsli0, M23, nr.randn(2,3))
    # check_affine(negsli1, M23, nr.randn(2,3))
    # check_affine(negsli01, M23, nr.randn(2,3))

    # check_affine(rfft, M35)
    check_affine(convlike, T2357, nr.randn(11, 3 * 5 * 7), nr.randn(2, 11))

    if DISPLAY:
        from thirdparty.tabulate import tabulate
        print tabulate([[key, val["fn"], val["grad"]]
                        for (key, val) in sorted(PROB2RESULT.items())],
                       headers=["funcname", "fncount", "gradcount"])
Beispiel #10
0
def check_affine_funcs(precision, backend):
    cgt.reset_config()
    np.random.seed(0)
    cgt.set_precision(precision)
    cgt.core.update_config(backend=backend)

    sA = np.array(nr.rand())
    sB = np.array(nr.rand())
    sC = np.array(nr.rand())
    mA = nr.randn(2,3)
    mB = nr.randn(2,3)
    mC = nr.randn(2,3)

    for fn in [xplusx, _2x_plus_3x, xm1, onemx]:
        for arg in [sA, mA]:
            check_affine(fn, arg)

    check_affine(elem_mult2, mA, mB, mC)
    check_affine(elem_mult2, sA, sB, sC)
    check_affine(pyramid, sA, sB, sC)
    check_affine(pyramid, mA, mB, mC)
    check_affine(slisum1, mA)
    check_affine(slisum2, mA)
    check_affine(slisum3, mA)
    check_affine(slisum4, mA)
    check_affine(max0, mA)
    check_affine(max1, mA)
    check_affine(max2, mA)
    check_affine(fancysli0, mA)
    check_affine(sum10, mA)
    check_affine(sum01, mA)
    check_affine(repeat0, mA[0:1, :], nr.randn(7,3))
    check_affine(repeat1, mA[:, 0:1], nr.randn(2,7))

    M23 = mA
    M35 = nr.randn(3,5)
    v3 = nr.randn(3)
    v13 = v3.reshape(1,3) #XXX
    v5 = nr.randn(5)
    v15 = v5.reshape(1,5) #XXX
    v3b = nr.randn(3)

    check_affine(matmat00, M23, M35)
    check_affine(matmat01, M23, M35.T)
    check_affine(matmat10, M23.T, M35)
    check_affine(matmat11, M23.T, M35.T)

    check_affine(matmat00a, M23, M35)
    check_affine(matmat01a, M23, M35.T)
    # check_affine(matmat10a, M23.T, M35)
    check_affine(matmat11a, M23.T, M35.T)

    check_affine(matvec, M23, v3)
    check_affine(vecvec, v3, v3b)
    check_affine(bcadd, M23, v13)
    check_affine(matmatplusvec, M23, M35, v15)
    check_affine(transpose, M23, nr.randn(3,2))


    T235 = nr.randn(2,3,5)
    T235a = nr.randn(2,3,5)
    T257 = nr.randn(2,5,7)
    T2357 = nr.randn(2,3,5,7)
    T2357a = nr.randn(2,3,5,7)

    check_affine(transpose012, T235, T235a)
    check_affine(transpose021, T235, T235a.transpose(0,2,1))
    check_affine(transpose102, T235, T235a.transpose(1,0,2))
    check_affine(transpose0312, T2357, T2357a.transpose(0,3,1,2))
    check_affine(transpose0231, T2357, T2357a.transpose(0,2,3,1))

    check_affine(batchedmatmul, T235, T257)

    check_affine(flip0, M23, nr.randn(2,3))
    check_affine(flip1, M23, nr.randn(2,3))

    # check_affine(negsli0, M23, nr.randn(2,3))
    # check_affine(negsli1, M23, nr.randn(2,3))
    # check_affine(negsli01, M23, nr.randn(2,3))

    # check_affine(rfft, M35)
    check_affine(convlike, T2357, nr.randn(11,3*5*7), nr.randn(2,11))


    if DISPLAY:
        from thirdparty.tabulate import tabulate
        print tabulate([[key,val["fn"],val["grad"]] for (key,val) in sorted(PROB2RESULT.items())],headers=["funcname","fncount","gradcount"])
Beispiel #11
0
def check_scalar_grads(precision, backend):
    cgt.reset_config()
    np.random.seed(0)
    cgt.set_precision(precision)
    cgt.core.update_config(backend=backend)
    x = cgt.scalar('x')
    y = cgt.scalar('y')
    z = cgt.scalar('z')
    vars = [x, y, z]  #pylint: disable=W0622
    vals = nr.rand(len(vars)) + 1

    PROB2RESULT = {}

    for ((key, _), cls) in it.chain(
            it.izip(core.UNARY_INFO.items(), it.repeat(core.ElwiseUnary)),
            it.izip(core.BINARY_INFO.items(), it.repeat(core.ElwiseBinary))):
        if key == "conj":
            print "skipping conj"
            continue
        utils.colorprint(utils.Color.YELLOW, "Testing %s\n" % key)
        if cls == core.ElwiseUnary:
            n_in = 1
            op = cls(key)
        else:
            n_in = 2
            op = cls(key, (True, True))
        inputvars = vars[0:n_in]
        inputvals = vals[0:n_in]
        out = core.Result(op, inputvars)
        f = cgt.function(inputvars, out)
        try:
            grads = cgt.grad(out, inputvars)
        except core.NonDifferentiable:
            print "nondiff"
            continue
        if DISPLAY:
            print "Function:"
            cgt.print_tree(out)
            print "Gradient original:"
            cgt.print_tree(grads)
            print "Gradient simplified:"
        grads_simple = core.simplify(grads)
        if DISPLAY: cgt.print_tree(grads_simple)
        gradf = cgt.function(inputvars, grads)
        eps = {"single": 1e-4, "double": 1e-9}[precision]
        nugrad = numeric_grad(lambda li: f(*li), inputvals, eps=eps)  #pylint: disable=W0640
        cgtgrad = gradf(*inputvals)
        np.testing.assert_almost_equal(nugrad,
                                       cgtgrad,
                                       decimal={
                                           "single": 3,
                                           "double": 6
                                       }[precision])

        grad_count = core.count_nodes(grads_simple)
        PROB2RESULT[key] = {}
        PROB2RESULT[key]["grad"] = grad_count

    if DISPLAY:
        from thirdparty.tabulate import tabulate
        print tabulate([[key, val["grad"]]
                        for (key, val) in PROB2RESULT.iteritems()],
                       headers=["funcname", "gradcount"])