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"])
def _print_stats(key2stats, t_total): rows = [] for (key, (count,t)) in key2stats.iteritems(): rows.append([str(key), count, t, t/t_total]) rows = sorted(rows, key=lambda row: row[2], reverse=True) cumsum = 0 for row in rows: cumsum += row[3] row.append(cumsum) from thirdparty.tabulate import tabulate print tabulate(rows, headers=["Instruction","Count","Time","Frac","Frac cumsum"])
def test_scalars(): np.random.seed(0) 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}[cgt.get_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}[cgt.get_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"])
def print_table(matrix): table = [] headers = sorted(list(reduce( lambda x, y: x | y, [set(matrix[c].keys()) for c in matrix]))) for klass, counter in sorted(matrix.items()): row = [klass] row.extend(counter[header] for header in headers) row.append(sum(counter.values())) table.append(row) last_row = ['Outcomes'] last_row.extend([sum(c[header] for c in matrix.values()) for header in headers]) last_row.append(sum(last_row[1:])) table.append(last_row) headers.append('Samples') print tabulate(table, headers=headers, tablefmt="rst")
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"])
def test_affine(): np.random.seed(0) 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(flatfancysli0, 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(fancysli0, M23, nr.randn(5,3)) check_affine(fancysli0a, M23, nr.randn(5,3)) check_affine(fancysli1, M23, nr.randn(2,5)) # 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"])