def cps(): P = workload.Prefix M = workload.IdentityTotal V = workload.VStack W1 = workload.Kronecker([M(50), M(100), M(7), M(4), M(2)]) W2 = workload.Kronecker([P(50), P(100), M(7), M(4), M(2)]) return V([W1]), V([W2])
def census(): W = SF1_Persons() workloads = [] for K in W.matrices: w = K.matrices Wi = workload.Kronecker([w[0], w[1], w[2], w[4]]) workloads.append(Wi) W1 = workload.VStack(workloads) M = workload.IdentityTotal workloads = [] for K in W.matrices: w = K.matrices Wi = workload.Kronecker([w[0], w[1], w[2], w[4], M(51)]) workloads.append(Wi) W2 = workload.VStack(workloads) return W1, W2
def dict2workload(workload_dict: Dict[str, AbstractLinearQuery]): """ Convert a dict of queries into HDMM Workload""" workload_list: List[workload.Kron] = [] for query in workload_dict.values(): kron_factors = [ workload.EkteloMatrix(x) for x in query.kronFactors() ] workload_list.append(workload.Kronecker(kron_factors)) return workload.VStack(workload_list)
def SmallKrons(blocks, size=5000): base = [workload.Total(W.shape[1]) for W in blocks] d = len(blocks) concat = [] for attr in powerset(range(d)): subs = [blocks[i] if i in attr else base[i] for i in range(d)] tmp = reduce(lambda x, y: x * y, [blocks[i].shape[1] for i in attr], 1) W = workload.Kronecker(subs) if tmp <= size: concat.append(W) return workload.VStack(concat)
def DimKKrons(workloads, k=1): blocks = workloads base = [workload.Total(W.shape[1]) for W in blocks] d = len(blocks) concat = [] for attr in itertools.combinations(range(d), k): subs = [blocks[i] if i in attr else base[i] for i in range(d)] W = workload.Kronecker(subs) concat.append(W) return workload.VStack(concat)
def adult(): R = workload.AllRange P = workload.Prefix M = workload.IdentityTotal I = workload.Identity T = workload.Total W1 = workload.Kronecker([M(75), M(16), M(5), M(2), M(20)]) W2 = DimKKrons([I(75), I(16), I(5), I(2), I(20)], 2) #W2 = workload.DimKMarginals((75, 16, 5, 2, 20), 2) return W1, W2
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 take_measurements(A, data): """ Efficiently take measurements from HDMM strategy and convert to a PGM-compatable form """ A = workload.union_kron_canonical(A) measurements = [] for Ai in A.matrices: w = Ai.weight proj = [ attributes[i] for i, B in enumerate(Ai.base.matrices) if type(B) != workload.Ones ] print(proj) matrix = workload.Kronecker( [B for B in Ai.base.matrices if type(B) != workload.Ones]) matrix = w * matrix.sparse_matrix() x = data.project( proj).datavector() # does Relation have this functionality? y = matrix.dot(x) + np.random.laplace( loc=0, scale=1, size=matrix.shape[0]) measurements.append((matrix, y, 1.0, proj)) return measurements
from hdmm import workload, templates import numpy as np # create a Kronecker product workload from dense matrix building blocks # 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()