def test_marginals(self): # full rank case W = workload.Range2D(4) temp = templates.Marginals((4, 4)) temp._set_workload(W) x0 = self.prng.rand(4) func = lambda p: temp._loss_and_grad(p)[0] grad = lambda p: temp._loss_and_grad(p)[1] err = check_grad(func, grad, x0) print(err) #self.assertTrue(err <= 1e-5) # low rank case P = workload.Prefix(4) T = workload.Total(4) W1 = workload.Kronecker([P, T]) W2 = workload.Kronecker([T, P]) W = workload.VStack([W1, W2]) temp = templates.Marginals((4, 4)) temp._set_workload(W) x0 = np.array([1, 1, 1, 0.0]) func = lambda p: temp._loss_and_grad(p)[0] grad = lambda p: temp._loss_and_grad(p)[1] f, g = func(x0), grad(x0) g2 = np.zeros(4) for i in range(4): x0[i] -= 0.00001 f1 = func(x0) x0[i] += 0.00002 f2 = func(x0) x0[i] -= 0.00001 g2[i] = (f2 - f1) / 0.00002 print(g) print(g2) np.testing.assert_allclose(g, g2, atol=1e-5)
def example3(): """ Optimize Union-of-Kronecker product workload using kronecker parameterization and marginals parameterization """ print('Example 3') sub_workloads1 = [workload.Prefix(64) for _ in range(4)] sub_workloads2 = [workload.AllRange(64) for _ in range(4)] W1 = workload.Kronecker(sub_workloads1) W2 = workload.Kronecker(sub_workloads2) W = workload.VStack([W1, W2]) K = templates.KronPIdentity([4]*4, [64]*4) K.optimize(W) print(error.expected_error(W, K.strategy())) M = templates.Marginals([64]*4) M.optimize(W) print(error.expected_error(W, M.strategy())) identity = workload.Kronecker([workload.Identity(64) for _ in range(4)]) print(error.expected_error(W, identity))
def setUp(self): self.prng = np.random.RandomState(0) self.domain = (2,3,4) I = lambda n: workload.Identity(n) T = lambda n: workload.Total(n) P = lambda n: workload.Prefix(n) R = lambda n: matrix.EkteloMatrix(self.prng.rand(n,n)) W1 = workload.Kronecker([I(2), T(3), P(4)]) W2 = workload.Kronecker([T(2), T(3), I(4)]) W3 = workload.Kronecker([I(2), I(3), T(4)]) self.W = workload.VStack([W1, W2, W3]) # three representations of Identity matrix self.A1 = I(2*3*4) self.A2 = workload.Kronecker([I(2),I(3),I(4)]) self.A3 = workload.Marginals.fromtuples(self.domain, {(0,1,2) : 1.0 }) self.A4 = workload.Marginals(self.domain, self.prng.rand(8)) self.A5 = workload.Kronecker([R(2), R(3), R(4)])
# this is a 2d example: domain = (10, 25) # densely represented sub-workloads in each of the dimensions identity1 = workload.EkteloMatrix(np.eye(10)) identity2 = workload.EkteloMatrix(np.eye(25)) total = workload.EkteloMatrix(np.ones((1, 10))) prefix = workload.EkteloMatrix(np.tril(np.ones((25, 25)))) # form the kron products in each dimension W1 = workload.Kronecker([identity1, identity2]) W2 = workload.Kronecker([total, prefix]) # form the union of krons W = workload.VStack([W1, W2]) # find a Kronecker product strategy by optimizing the workload ps = [2, 2] # parameter for P-Identity strategies template = templates.KronPIdentity(ps, domain) # run optimization template.optimize(W) # get the sparse, explicit representation of the optimized strategy A = template.strategy().sparse_matrix().tocsr() # Round for Geometric Mechanism (skip this if using Laplace Mechanism) A = np.round(A * 1000) / 1000.0 # Extract diagonal and non-diagonal portion of strategy