def train_kidney(): tbn = get.kidney_full() e1 = 'L' e2 = 'T' q = 'S' size = 1024 # simulate TAC = tac.TAC(tbn,(e1,e2),q,trainable=False) evidence, marginals = TAC.simulate(size,'grid') # visualize simulated data visualize.plot3D(evidence,marginals,e1,e2,q) # bn for tbn in (get.kidney_tbn(),get.kidney_bn()): # learn TAC = tac.TAC(tbn,(e1,e2),q,trainable=True) TAC.fit(evidence,marginals,loss_type='CE',metric_type='CE') predictions = TAC.evaluate(evidence) # visualize learned tac visualize.plot3D(evidence,predictions,e1,e2,q)
def __VE_vs_TAC(bn,inputs,output,evidence_size,hard_evidence=False,tbn=None): if tbn==None: tbn = bn assert not bn._for_inference assert not tbn._for_inference hinputs = inputs if hard_evidence else [] cards = tuple(bn.node(input).card for input in inputs) evidence = data.evd_random(evidence_size,cards,hard_evidence) TAC = tac.TAC(tbn,inputs,output,hard_inputs=hinputs,trainable=False) tac_posteriors = TAC.evaluate(evidence,batch_size=16) # 16 to reduce memory del TAC # no longer needed (save memory) ve_posteriors = VE.posteriors(bn,inputs,output,evidence) close = np.isclose(ve_posteriors,tac_posteriors) # array ok = np.all(close) # boolean if not ok: mismatches = np.logical_not(close) print(f'\nMismatch between VE and TAC on {tbn.name}:') print(' VE \n ',ve_posteriors[mismatches]) print(' TAC\n ',tac_posteriors[mismatches]) print('\n VE \n ',ve_posteriors) print(' TAC\n ',tac_posteriors) print('\n***Ouch!!!!\n') quit() else: print('.',end='',flush=True)
def train(size,output,data_size,testing,use_bk,tie_parameters): circuit_type = 'TAC' if testing else 'AC' u.show(f'\n===Training {circuit_type} for rectangle {output} in {size}x{size} images, use_bk {use_bk}, tie {tie_parameters}') # get training and testing data (labels are one-hot) t_evidence, t_labels = rdata.get(size,output,noisy_image_count=size,noise_count=size) v_evidence, v_labels = rdata.get(size,output,noisy_image_count=2*size,noise_count=2*size) # get model bn, inputs = rmodel.get(size,output,testing,use_bk,tie_parameters) # compile model to circuit circuit = tac.TAC(bn,inputs,output,trainable=True,profile=False) # use a random subset of the generated data t_percentage = data_size / len(t_labels) v_percentage = max(1000,data_size)/len(v_labels) # no less than 1000 t_evidence, t_labels = data.random_subset(t_evidence,t_labels,t_percentage) v_evidence, v_labels = data.random_subset(v_evidence,v_labels,v_percentage) # train AC circuit.fit(t_evidence,t_labels,loss_type='CE',metric_type='CA') # compute accuracy accuracy = circuit.metric(v_evidence,v_labels,metric_type='CA') u.show(f'\n{circuit_type} accuracy {100*accuracy:.2f}') return (100*accuracy, circuit)
def eval(f, size, digits, testing): circuit_type = 'TAC' if testing else 'AC' # get data (ground truth) evidence, marginals = ddata.get(size, digits) ecount = len(marginals) # number of examples u.echo(f, f'\n==digits {size}x{size} images: {ecount} total') # get model net, inputs, output = dmodel.get(size, digits, testing, use_bk=True, tie_parameters=False, remove_common=False) # compile model s_time = time.time() u.echo(f, f'\ncompiling {circuit_type}:', end='') AC = tac.TAC(net, inputs, output, trainable=False, profile=False) t = time.time() - s_time u.echo(f, f' {t:.1f} sec') u.echo( f, f' {circuit_type} size {AC.size:,}\n (sep) binary rank {AC.binary_rank:.1f}, rank {AC.rank}' ) # evaluate AC on evidence to get predictions u.echo(f, f'evaluating {circuit_type}:', end='', flush=True) predictions, t1, batch_size = AC.evaluate(evidence, report_time=True) u.echo(f, f' batch_size {batch_size}') u.echo(f, f' {t1:.2f} sec, {1000*t1/ecount:.1f} ms per example')
def validate(size,output,testing,elm_method='minfill',elm_wait=30): circuit_type = 'TAC' if testing else 'AC' # get data (ground truth) evidence, labels = rdata.get(size,output) u.show(f'\n===Checking {circuit_type} for rectangle {output} in {size}x{size} images: {len(labels)} total') # get model bn, inputs = rmodel.get(size,output,testing=testing,use_bk=True,tie_parameters=False) # compile model AC = tac.TAC(bn,inputs,output,trainable=False,profile=False, elm_method=elm_method,elm_wait=elm_wait) # evaluate TAC on evidence to get predictions predictions = AC.evaluate(evidence) # verify that predictions match one_hot_marginals if u.equal(predictions,labels): u.show('\n===All good!') else: u.show('***bumper!!!') quit()
def __simulate_fit(tbn,e1,e2,q): size = 1024 # simulate TAC = tac.TAC(tbn,(e1,e2),q,trainable=False) evidence, marginals = TAC.simulate(size,'grid') # visualize simulated data visualize.plot3D(evidence,marginals,e1,e2,q) # learn TAC = tac.TAC(tbn,(e1,e2),q,trainable=True) TAC.fit(evidence,marginals,loss_type='MSE',metric_type='MSE') predictions = TAC.evaluate(evidence) # visualize learned tac visualize.plot3D(evidence,predictions,e1,e2,q)
def __posterior_time(f,bn,inputs,output,bsize,min_ac,max_ac,counter): s_time = time.perf_counter() AC = tac.TAC(bn,inputs,output) t = time.perf_counter()-s_time if AC.size < min_ac*1000000 or AC.size > max_ac*1000000: return None u.echo(f,f'\n== {counter} ==\nTensor AC:',end='') u.echo(f,f' {t:.1f} sec') u.echo(f,f' size {AC.size:,}, max binary rank {AC.binary_rank:0.1f}') # get evidence cards = tuple(bn.node(input).card for input in inputs) evidence = data.evd_random(bsize,cards) # evaluate AC as tf graph with batch u.echo(f,f'(tf full) eval:',end='',flush=True) tac_posteriors, t_AC, b_AC = AC.evaluate(evidence,report_time=True) u.echo(f,f' {t_AC:.2f} sec' f'\n {1000*t_AC/bsize:.0f} ms per example, used batch size {b_AC}' f'\n {1000*t_AC/bsize/(AC.size/1000000):.0f} ms per 1M nodes (one example)') # check classical AC and numpy AC_size = AC.size AC_brank = AC.binary_rank opsgrapy = AC.ops_graph del AC # no longer needed u.echo(f,'\nScalar AC:',end='') s_time = time.perf_counter() SAC = verify.AC.ScalarAC(opsgrapy) t = time.perf_counter()-s_time u.echo(f,f' {t:.1f} sec') u.echo(f,f' size {SAC.size:,}') u.echo(f,f' {SAC.size/AC_size:.2f} scalar ac/tensor ac') def v(eval_func,type): u.echo(f,f'({type}) eval:',end='',flush=True) t_SAC, b_SAC = eval_func(evidence,tac_posteriors) u.echo(f,f' {t_SAC:.2f} sec' f'\n {1000*t_SAC/bsize:.0f} ms per example, used batch size {b_SAC}' f'\n {t_SAC/t_AC:.2f} {type}/ac ') return t_SAC, b_SAC t_numpy, b_numpy = v(SAC.verify_numpy,'numpy batch') # t_tf, b_tf = v(SAC.verify_tf,'tf batch') t_tf, b_tf = 0, 0 # t_array, b_array = v(SAC.verify_array,'array') t_array, b_array = 0, 0 return (AC_size, AC_brank, SAC.size, t_AC, t_numpy, t_tf, t_array, b_AC, b_numpy, b_tf, b_array)
def addScope(self, name, scope_type): global ScopeList, currentScope new_scope = { "name" : str(name), "parent" : ScopeList[currentScope]["name"], "table" : dict(), "scope_type" : str(scope_type), "tac" : tac.TAC(str(name),ScopeList[currentScope]["tac"].nextquad), "offset" : ScopeList[currentScope]["offset"], } ScopeList[str(name)] = new_scope #new_scope["tac"].startquad = ScopeList[currentScope]["tac"].nextquad currentScope = str(name) SymTab()
def train(size, digits, data_size, testing, use_bk, tie_parameters, remove_common=False): assert size >= 7 assert all(d in range(10) for d in digits) circuit_type = 'TAC' if testing else 'AC' u.show( f'\n===Training {circuit_type} for digits {digits} in {size}x{size} images, use_bk {use_bk}, tie {tie_parameters}' ) # get model net, inputs, output = dmodel.get(size, digits, testing, use_bk, tie_parameters, remove_common) # get data (ground truth) t_evidence, t_labels = ddata.get(size, digits, noisy_image_count=100, noise_count=size) v_evidence, v_labels = ddata.get(size, digits, noisy_image_count=200, noise_count=size) # compile model into circuit circuit = tac.TAC(net, inputs, output, trainable=True, profile=False) # get random subset of dats t_percentage = data_size / len(t_labels) v_percentage = max(1000, data_size) / len(v_labels) # no less than 1000 t_evidence, t_labels = data.random_subset(t_evidence, t_labels, t_percentage) v_evidence, v_labels = data.random_subset(v_evidence, v_labels, v_percentage) # fit circuit circuit.fit(t_evidence, t_labels, loss_type='CE', metric_type='CA') # compute accuracy accuracy = circuit.metric(v_evidence, v_labels, metric_type='CA') u.show(f'\n{circuit_type} accuracy {100*accuracy:.2f}')
def train_all(size,output,tries,data_sizes,testing,use_bk,tie_parameters,batch_size): start_time = time.time() fname = paths.exp / u.time_stamp(f'train_rect_{size}_{output}_{tries}_{testing}_{use_bk}_{tie_parameters}','txt') f = open(fname,'w+') u.echo(f,f'\nrectangle {size} x {size}, output {output}, data_sizes {data_sizes}, testing {testing}, use_bk {use_bk}, tie {tie_parameters}\n') u.echo(f,f'fixed batch size {batch_size}') u.echo(f,'output logged into logs/exp/') def get_data(data_size): # full data t_evidence, t_labels = rdata.get(size,output,noisy_image_count=size,noise_count=size) v_evidence, v_labels = rdata.get(size,output,noisy_image_count=2*size,noise_count=2*size) # random subset t_percentage = data_size / len(t_labels) v_percentage = max(1000,data_size)/len(v_labels) # no less than 1000 t_evidence, t_labels = data.random_subset(t_evidence,t_labels,t_percentage) v_evidence, v_labels = data.random_subset(v_evidence,v_labels,v_percentage) return t_evidence, t_labels, v_evidence, v_labels # get model net, inputs = rmodel.get(size,output,testing,use_bk,tie_parameters) # compile model into circuit circuit = tac.TAC(net,inputs,output,trainable=True,profile=False) u.echo(f,f'circuit size {circuit.size:,}, paramater count {circuit.parameter_count}\n') for data_size, count in zip(data_sizes,tries): u.echo(f,f'==data size {data_size}') t_evidence, t_labels, v_evidence, v_labels = get_data(data_size) u.echo(f,f' train {len(t_labels)}, test {len(v_labels)}') u.echo(f,f' accuracy ({count}):',end='',flush=True) sample = [] for i in range(count): circuit.fit(t_evidence,t_labels,loss_type='CE',metric_type='CA',batch_size=batch_size) acc = 100*circuit.metric(v_evidence,v_labels,metric_type='CA') sample.append(acc) u.echo(f,f' {acc:.2f}',end='',flush=True) u.echo(f,f'\naccuracy mean {s.mean(sample):.2f}, std {s.stdev(sample):.2f}\n') all_time = time.time() - start_time u.echo(f,f'Total Time: {all_time:.3f} sec') f.close()
def validate(size, digits, testing, elm_method='minfill', elm_wait=30): assert size >= 7 assert all(d in range(10) for d in digits) # get data (ground truth) evidence, labels = ddata.get(size, digits) data_size = len(labels) circuit_type = 'TAC' if testing else 'AC' u.show( f'\n===Checking {circuit_type} for digits {digits} in {size}x{size} images: {data_size} total' ) # get model net, inputs, output = dmodel.get(size, digits, testing, use_bk=True, tie_parameters=False, remove_common=False) # compile model into circuit circuit = tac.TAC(net, inputs, output, trainable=False, profile=False, elm_method=elm_method, elm_wait=elm_wait) # evaluate circuit on evidence to get predictions predictions = circuit.evaluate(evidence) # verify that predictions match labels if u.equal(predictions, labels): u.show('\n===All good!\n') else: u.show('***bumper!!!') quit()
def validateThirdOrderHMM(size, card, num_examples=10): # validate AC of HMM model by running query pr(X_t,Y[1:t]) compared to the forward algorithm transition = np.random.rand(card, card, card, card) transition_sum = np.sum(transition, axis=-1, keepdims=True) transition = transition / transition_sum emission = np.random.rand(card, card) emission_sum = np.sum(emission, axis=1, keepdims=True) emission = emission / emission_sum # generate random transition and emission probabilities bn = hmm.getNthOrderHMM(size, card, 3, param=True, transition=transition, emission=emission) # define an hmm model with these parameters logging("Start testing third order HMM of length {}".format(size)) inputs = ['e_' + str(i) for i in range(size - 1)] output = 'h_' + str(size - 1) ac = tac.TAC(bn, inputs, output, trainable=False) # compile an ac that computes pr(X_T+1, Y[1:T]) evidence_ac, evidence_dp = generate_hard_evidence(size - 1, 1) labels_ac = ac.evaluate(evidence_ac) logging("ac labels: %s" % (labels_ac)) labels_dp = [] for evid in evidence_dp: label = hmm.predictThirdOrder(size, evid, transition, emission) labels_dp.append(label) labels_dp = np.stack(labels_dp) logging("forward labels: %s" % (labels_dp)) if u.equal(labels_ac, labels_dp, tolerance=True): logging("Successfully validate third order HMM of length {}\n".format( size)) else: logging( "Inconsistence queries for third order HMM of length {}\n".format( size))
def train_fn2(size,card): functions = [ # lambda x,y: .7, # lambda x,y: x, lambda x,y: 0.5*math.exp(-5*(x-.5)**2-5*(y-.5)**2), lambda x,y: .5 + .5 * math.sin(2*math.pi*x), lambda x,y: 1.0/(1+math.exp(-32*(y-.5))), lambda x,y: math.exp(math.sin(math.pi*(x+y))-1), lambda x,y: (1-x)*(1-x)*(1-x)*y*y*y, lambda x,y: math.sin(math.pi*(1-x)*(1-y)), lambda x,y: math.sin((math.pi/2)*(2-x-y)), lambda x,y: .5*x*y*(x+y)] tbn, e1, e2, q = get.fn2_chain(size,card) TAC = tac.TAC(tbn,[e1,e2],q,trainable=True,profile=False) for fn in functions: evidence, marginals = data.simulate_fn2(fn,1024) visualize.plot3D(evidence,marginals,e1,e2,q) TAC.fit(evidence,marginals,loss_type='CE',metric_type='CE') predictions = TAC.evaluate(evidence) visualize.plot3D(evidence,predictions,e1,e2,q)
import pprint, copy import csv import tac ### Scopes ### global ScopeList, currentScope, AttrList,Error Error = False ScopeList = { "NULL" : None, "global" : {"name":"global", "parent" : "NULL", "scope_type" : "global", "tac" : tac.TAC("global",0), "offset" : 0 }, } currentScope = "global" AttrList = ['name','type', 'star', 'id_type', 'specifier', 'value', 'is_defined', 'order', 'num', 'parameters', 'access', 'myscope', 'inc', 'dec', "tac_name" , "offset"] scope_ctr = 1 previous_scope = "" scope_transitions = [] ### Classes ### access_specifier = "public" ### Functions ### function_list = [] is_func_decl = False parameter_specifiers = ["register", "auto", "const", "volatile"] parenthesis_ctr = 0 is_parameter = False parameter_offset = 0 ### Namespaces ### namespace_list = [] is_namespace = False is_ns_member = False