def testMarginalizeBothAxis(self): manage.config.MAXPOWER = np.array([10]) a = ExprZp([1], np.array([[1, 0, 0]])) b = ExprZp([1], np.array([[0, 1, 0]])) c = ExprZp([2], np.array([[0, 0, 1]])) expressions = np.array([[a, b, c], [a, b, c]], dtype=np.object) matrix = ExprMatrix('A', expressions, powers=np.array([1])) matrix = matrix.Marginalize(axis=None) self.assertEquals((1, 1), matrix.expressions.shape)
def testHashImmuneToConst(self): manage.config.MAXPOWER = np.array([10]) a = ExprZp( [2, 3], np.array([[1, 2, 3], [4, 5, 6]])) b = ExprZp( [4, 6], np.array([[1, 2, 3], [4, 5, 6]])) self.assertNotEquals(a, b) a = ExprMatrix("a", np.array([[a]]), np.array([1])) b = ExprMatrix("b", np.array([[b]]), np.array([1])) self.assertEquals(a, b)
def testElementwiseMultiply(self): manage.config.MAXPOWER = np.array([10, 10]) # a = x1 * x3 # b = x1 * x2^2 # c = x2^3 + x3^4 a = ExprZp([1], np.array([[1, 0, 1]])) b = ExprZp([1], np.array([[1, 2, 0]])) c = ExprZp([1, 1], np.array([[0, 3, 0], [0, 0, 4]])) expressions1 = np.array([[a, b], [c, a]]) expressions2 = np.array([[a, a], [a, a]]) # mat1: # x1x3 | x1 * x2^2 # x2^3 + x3^4 | x1x3 mat1 = ExprMatrix('M1', expressions1, powers=np.array([1, 0])) mat2 = ExprMatrix('M2', expressions2, powers=np.array([0, 1])) mat1.ElementwiseMultiply(mat2) self.assertEquals((2, 2), mat1.expressions.shape)
def testShapesAfterSumAreCorrec(self): manage.config.MAXPOWER = np.array([10]) a = ExprZp([1], np.array([[1, 0, 0]])) b = ExprZp([1], np.array([[0, 1, 0]])) expressions = np.array([[a, a, a], [b, b, b]]) matrix = ExprMatrix('M', expressions, powers=np.array([1])) # Exp matrix test self.assertEquals((2, 3), matrix.expressions.shape) m0 = copy.copy(matrix) m0 = m0.Marginalize(axis=0) self.assertEquals((1, 3), m0.expressions.shape) self.assertEquals(a + b, m0.expressions[0, 0]) self.assertEquals(a + b, m0.expressions[0, 1]) m1 = copy.copy(matrix) m1 = m1.Marginalize(axis=1) self.assertEquals((2, 1), m1.expressions.shape) self.assertEquals(a + a + a, m1.expressions[0, 0]) self.assertEquals(b + b + b, m1.expressions[1, 0])
def testAddExpressions(self): a = ExprZp([1], np.array([[1, 0, 0]])) # x1 b = ExprZp([1], np.array([[0, 0, 1]])) # x3 c = ExprZp([2], np.array([[1, 0, 1]])) # 2x1x3 d = ExprZp([2], np.array([[1, 0, 0]])) # 2x1 e = ExprZp([2], np.array([[0, 0, 1]])) # 2x3 self.assertEquals(a + a, d) self.assertEquals(b+b, e) print 'Karol D:' d.PrintDebug() print 'Karol E:' e.PrintDebug() print 'Karol D*E:' (d*e).PrintDebug() print 'Karol C+C:' (c+c).PrintDebug() self.assertEquals(d * e, c+c)
def main(): for target, labels in [targets[2]]: params = {'depth': 5, 'trials': 1, 'run_id': 1} scheduler = NgramScheduler(params) solution_tree = None for i in range(1, 16): matlab, tree, solution_tree = execute_once(scheduler, params, target, solution_tree, i) if matlab is None: break name = get_name_matlab(target, i) original = target(i).GetTargetExpression() code = "" code += original.comp[MATLAB] + "\n" code += "optimized = " expr = "" try: for w, c in matlab: if len(expr) > 0: expr += " + " expr += "%s * (%s)" % (ExprZp.ToFrac(w), c) except: break if target == RBM: expr = "2^(n + m - %d) * (%s)" % (manage.config.N + manage.config.M, expr) if target == RBMOneSide: expr = "2^(n - %d) * (%s)" % (manage.config.M, expr) if target == Sym: expr = "(%s) / 120" % expr code += expr + ";\n" code += "normalization = sum(abs(original(:)));\n" code += "assert(sum(abs(original(:) - optimized(:))) / normalization < 1e-10);" f = open("code.m", 'w') f.write(code) f.close() print "Executing matlab code: \n%s\n" % code ret = os.system( "matlab -nodesktop -nodisplay -nojvm -nosplash -r \"try run('code.m');catch exit(-1); end; exit(0) \" > /dev/null" ) if ret != 0: break os.rename("code.m", name) tree_string = "" for j, t in enumerate(tree): ProcessLine(j, str(t), get_name_tree(target, i)) generate_sup()
def testEquivalence(self): targets = [RBMOneSide, SumAmultA, RBM, SumAAT, SumAB, Sym] for target in targets: manage.config.EXPR_IMPL = ExprZp scheduler = NgramScheduler params = {'depth': 5, 'trials': 1} matlab = execute(scheduler, params, target, maxpower=4, record_it=False)[0]["matlab"] for i, m in enumerate(matlab): f = open('code.m', 'w') original = target(i + 1).GetTargetExpression() code = "" code += original.comp[MATLAB] + "\n" code += "optimized = " expr = "" for w, c in m: if len(expr) > 0: expr += " + " expr += "%s * (%s)" % (ExprZp.ToFrac(w), c) if target == RBM: expr = "2^(n + m - %d) * (%s)" % (manage.config.N + manage.config.M, expr) if target == RBMOneSide: expr = "2^(n - %d) * (%s)" % (manage.config.M, expr) code += expr + ";\n" code += "assert(sum(abs(original(:) - optimized(:))) / sum(abs(original(:))) < 1e-8);" f.write(code) f.close() print "Executing matlab code: \n%s\n" % code ret = os.system( "matlab -nodesktop -nodisplay -nojvm -nosplash -r \"try run('code.m');catch exit(-1); end; exit(0) \" > /dev/null" ) if ret == 0: print "Passed matlab verification\n" else: self.assertEquals(ret, 0) os.remove('code.m')
def testFindLinearCombinationWithMatrix(self): manage.config.MAXPOWER = np.array([10]) # mat1 mat2 target # x1 | x1x2 x1 | x1x2 3x1 | 3x1x2 # 2x3 | x2^2 x1 | x3 4x3+x1 | 2x2^2 + x3 mat1_11 = ExprZp([1], np.array([[1, 0, 0]])) mat1_12 = ExprZp([1], np.array([[1, 1, 0]])) mat1_21 = ExprZp([2], np.array([[0, 0, 1]])) mat1_22 = ExprZp([1], np.array([[0, 2, 0]])) mat2_11 = ExprZp([1], np.array([[1, 0, 0]])) mat2_12 = ExprZp([1], np.array([[1, 1, 0]])) mat2_21 = ExprZp([1], np.array([[1, 0, 0]])) mat2_22 = ExprZp([1], np.array([[0, 0, 1]])) target_11 = ExprZp([3], np.array([[1, 0, 0]])) target_12 = ExprZp([3], np.array([[1, 1, 0]])) target_21 = ExprZp([4, 1], np.array([[0, 0, 1], [1, 0, 0]])) target_22 = ExprZp([2, 1], np.array([[0, 2, 0], [0, 0, 1]])) mat1 = ExprMatrix("a", np.array([[mat1_11, mat1_12], [mat1_21, mat1_22]]), np.array([1])) mat2 = ExprMatrix("b", np.array([[mat2_11, mat2_12], [mat2_21, mat2_22]]), np.array([1])) target = ExprMatrix( "target", np.array([[target_11, target_12], [target_21, target_22]]), np.array([1])) expressions_list = [mat1, mat2] weights = ExprZp.FindLinearCombination(expressions_list, target)[0] self.assertEquals(2, len(weights)) self.assertAlmostEqual(2.0, weights[0]) self.assertAlmostEqual(1.0, weights[1])