def subsampleExampleDistance(x1, x2, D): dist = 0.0 arr = [] for x in range(0, 784): arr += [x] util.permute(arr) for d in range(D): dist += (x1[arr[d]] - x2[arr[d]]) * (x1[arr[d]] - x2[arr[d]]) return sqrt(dist)
def shufflePoints(X, Y): """ Randomize the order of the points. """ [N,D] = X.shape order = range(N) util.permute(order) retX = X[order,:] retY = Y[order] return (retX, retY)
def shufflePoints(X, Y): """ Randomize the order of the points. """ [N, D] = X.shape order = range(N) util.permute(order) retX = X[order, :] retY = Y[order] return (retX, retY)
def generalize_test(self, batch_size, included_inds, targets, max_epochs, thresh, change_epochs, optim_args): """ See how long it takes the network to reach accuracy threshold on target inputs, when training on items specified by included_inds. Then restore the parameters. 'targets' can be an array of indices or a logical mask into the full set of inputs. """ self.train() # Save original state of network to restore later net_state_dict = deepcopy(self.state_dict()) optimizer = torch.optim.SGD(self.parameters(), **optim_args[0]) epochs = 0 while epochs < max_epochs: if epochs in change_epochs: k_change = change_epochs.index(epochs) for key, val in optim_args[k_change + 1].items(): optimizer.param_groups[0][key] = val order = util.permute(included_inds) self.train_epoch(order, optimizer, batch_size) perfect = self.evaluate_input_set(targets, threshold=0.5)[3] if perfect >= thresh: break epochs += 1 # Restore old state of network self.load_state_dict(net_state_dict) etg_string = '= ' + str(epochs + 1) if epochs < max_epochs else '> ' + str(max_epochs) return epochs, etg_string
def sample(self, n_nodes, n_samples, tolerance=1000): """ Sample n graphs from this ergm via Metropolis-Hastings MCMC. Args: n: The number of samples to generate. Returns: X: A n_samples x n_nodes^2 2d numpy array representing the sampled graphs. Each row in X represents a single graph. """ samples = np.zeros((n_samples, n_nodes**2)) # start from a random adjacency matrix this_G = np.random.randint(0, 1, (n_nodes, n_nodes)) this_w = self.weight(this_G) i = 0 ntries = 0 while i < n_samples and ntries < tolerance: new_G = util.permute(this_G) new_w = self.weight(new_G) u = np.random.rand() if new_w > this_w or u < (new_w / this_w): samples[i] = new_G.reshape(n_nodes**2) this_G = new_G this_w = new_w i += 1 ntries += 1 if ntries == tolerance: print 'did not converge' return samples
def sample(self, n_nodes, n_samples, tolerance=1000): """ Sample n graphs from this ergm via Metropolis-Hastings MCMC. Args: n: The number of samples to generate. Returns: X: A n_samples x n_nodes^2 2d numpy array representing the sampled graphs. Each row in X represents a single graph. """ samples = np.zeros((n_samples, n_nodes**2)) # start from a random adjacency matrix this_G = np.random.randint(0, 1, (n_nodes, n_nodes)) this_w = self.weight(this_G) i = 0 ntries = 0 while i < n_samples and ntries < tolerance: new_G = util.permute(this_G) new_w = self.weight(new_G) u = np.random.rand() if new_w > this_w or u < (new_w/this_w): samples[i] = new_G.reshape(n_nodes**2) this_G = new_G this_w = new_w i += 1 ntries += 1 if ntries == tolerance: print 'did not converge' return samples
def runOnlineClassifier(h, X, Y): N,D = X.shape order = range(N) util.permute(order) plot(X[Y< 0.5,0], X[Y< 0.5,1], 'b+', X[Y>=0.5,0], X[Y>=0.5,1], 'ro') noStop = False for n in order: print (Y[n], X[n,:]) h.nextExample(X[n,:], Y[n]) hold(True) plot([X[n,0]], [X[n,1]], 'ys') hold(False) if not noStop: v = raw_input() if v == "q": noStop = True plotLinearClassifier(h, X, Y)
def subsampleExampleDistance(x1, x2, D): dist = 0. arr = [] for x in range(0, 784): arr[x] = x util.permute(arr) for i, v1 in x1.iteritems(): if i < D: v2 = 0. if x2.has_key(arr[i]): v2 = x2[arr[i]] dist += (v1 - v2) * (v1 - v2) for i, v2 in x2.iteritems(): if i < D: if not x1.has_key(arr[i]): dist += v2 * v2 return sqrt(dist)
def mcmc(self,GX,n_graph_samples=1000): v=len(GX) p=nx.density(GX) #print(p) current_graph=nx.adjacency_matrix(nx.erdos_renyi_graph(v,p)).todense() current_g= np.asarray(current_graph) #print(current_g) current_w=self.weight(current_g) # print(current_w) g = np.zeros((n_graph_samples,v*v),dtype=int) for i in xrange(n_graph_samples): new_g = util.permute(current_g) new_w =self.weight(new_g) # print(new_w) if new_w > current_w or random.random() < (new_w/current_w): g[i] = new_g.reshape(1,v*v) current_w=new_w return g
def generalize_test(self, batch_size, optimizer, included_inds, targets, max_epochs=2000, thresh=0.99): """ See how long it takes the network to reach accuracy threshold on target inputs, when training on items specified by included_inds. Then restore the parameters. 'targets' can be an array of indices or a logical mask into the full set of inputs. """ self.train() # Save original state of network to restore later net_state_dict = deepcopy(self.state_dict()) optim_state_dict = deepcopy(optimizer.state_dict()) epochs = 0 while epochs < max_epochs: order = util.permute(included_inds) self.train_epoch(order, batch_size, optimizer) mean_target_wacc = self.evaluate_input_set(targets)[2] if mean_target_wacc >= thresh: break epochs += 1 # Restore old state of network self.load_state_dict(net_state_dict) optimizer.load_state_dict(optim_state_dict) etg_string = '= ' + str( epochs + 1) if epochs < max_epochs else '> ' + str(max_epochs) return epochs, etg_string
def do_training(self, **train_params): """ Do the training! num_epochs is either a scalar or sequence of integers if there are multiple training stages. If there are multiple stages, lr, momentum, and weight_decay can either be sequences or scalars to use the same hyperparameter for each stage. The weight decay here is not scaled by the learning rate (different from PyTorch's definition). If batch_size == 0, do full training set batches. """ # Merge default params with overrides train_params = {**train_defaults, **train_params} num_epochs = train_params['num_epochs'] if isinstance(num_epochs, tuple): n_stages = len(num_epochs) else: n_stages = 1 num_epochs = (num_epochs,) lr = train_params['lr'] if isinstance(lr, tuple): assert len(lr) == n_stages, 'Wrong number of lr values for number of stages' else: lr = tuple(lr for _ in range(n_stages)) momentum = train_params['momentum'] if isinstance(momentum, tuple): assert len(momentum) == n_stages, 'Wrong number of momentum values for number of stages' else: momentum = tuple(momentum for _ in range(n_stages)) weight_decay = train_params['weight_decay'] if isinstance(weight_decay, tuple): assert len(weight_decay) == n_stages, 'Wrong number of weight decay values for number of stages' else: weight_decay = tuple(weight_decay for _ in range(n_stages)) # weight_decay is Hinton's version which is the fraction to reduce the weights by after an update # (independent of the learning rate). To convert to what pytorch means by weight decay, have to # divide by the learning rate. wd_torch = tuple(wd / rate for wd, rate in zip(weight_decay, lr)) train_optim_args = [{'lr': this_lr, 'momentum': this_mm, 'weight_decay': this_wd} for this_lr, this_mm, this_wd in zip(lr, momentum, wd_torch)] test_optim_args = [{**optim_args, 'weight_decay': 0} for optim_args in train_optim_args] # test_optim_args = train_optim_args optimizer = torch.optim.SGD(self.parameters(), **train_optim_args[0]) # Choose the hold-out people/relationships from the English tree train_p1_inds = np.arange(self.person1_units) train_rel_inds = np.arange(self.rel_units) if train_params['do_combo_testing']: assert not train_params['do_tree_holdout'], "Can't do both combo testing and tree holdout" train_inds, holdout_inds = self.prepare_holdout(train_params['n_holdout']) elif train_params['do_tree_holdout']: train_p1_inds, train_rel_inds, train_inds, holdout_inds = self.prepare_tree_holdout() else: train_inds = np.arange(self.n_inputs) holdout_inds = np.array([]) # Prepare snapshots snap_epochs, epoch_digits, snaps = self.prepare_snapshots(**train_params) total_epochs = sum(num_epochs) change_epochs = list(np.cumsum(num_epochs))[:-1] epoch_digits = len(str(total_epochs-1)) etg_digits = len(str(train_params['test_max_epochs'])) + 2 n_report = total_epochs // train_params['report_freq'] + 1 n_etg = (n_report - 1) // train_params['reports_per_test'] + 1 reports = {rtype: np.zeros(n_report) for rtype in ['loss', 'accuracy', 'weighted_acc', 'weighted_acc_loose', 'weighted_acc_loose_indomain', 'frac_perfect']} if train_params['do_combo_testing']: for rtype in ['test_accuracy', 'test_weighted_acc', 'test_weighted_acc_indomain', 'test_frac_perfect']: reports[rtype] = np.zeros(n_report) if train_params['do_tree_holdout']: reports['new_tree_etg'] = np.zeros(n_etg, dtype=int) for epoch in range(total_epochs + (1 if train_params['include_final_eval'] else 0)): if epoch in change_epochs: # Move to new stage of learning k_change = change_epochs.index(epoch) for key, val in train_optim_args[k_change + 1].items(): optimizer.param_groups[0][key] = val if epoch in snap_epochs: # collect snapshots k_snap = snap_epochs.index(epoch) with torch.no_grad(): people_input = torch.eye(self.person1_units, device=self.device)[train_p1_inds] rels_input = torch.eye(self.rel_units, device=self.device)[train_rel_inds] people_dummy = torch.zeros((len(train_rel_inds), self.person1_units), device=self.device) rels_dummy = torch.zeros((len(train_p1_inds), self.rel_units), device=self.device) p1_repr_preact_snap = self.person1_to_repr(people_input) p1_repr_snap = self.act_fn(p1_repr_preact_snap) dummy_rel_repr = self.act_fn(self.rel_to_repr(rels_dummy)) combined_repr = torch.cat((p1_repr_snap, dummy_rel_repr), dim=1) p1_hidden_snap = self.act_fn(self.repr_to_hidden(combined_repr)) snaps['person1_repr_preact'][k_snap][train_p1_inds] = p1_repr_preact_snap snaps['person1_repr'][k_snap][train_p1_inds] = p1_repr_snap snaps['person1_hidden'][k_snap][train_p1_inds] = p1_hidden_snap rel_repr_snap = self.act_fn(self.rel_to_repr(rels_input)) dummy_person1_repr = self.act_fn(self.person1_to_repr(people_dummy)) combined_repr = torch.cat((dummy_person1_repr, rel_repr_snap), dim=1) rel_hidden_snap = self.act_fn(self.repr_to_hidden(combined_repr)) snaps['relation_repr'][k_snap][train_rel_inds] = rel_repr_snap snaps['relation_hidden'][k_snap][train_rel_inds] = rel_hidden_snap # report progress if epoch % train_params['report_freq'] == 0: k_report = epoch // train_params['report_freq'] # get current performance (mean_loss, mean_acc, mean_wacc, mean_wacc_loose, mean_wacc_loose_masked, frac_perf) = self.evaluate_input_set(train_inds) reports['loss'][k_report] = mean_loss reports['accuracy'][k_report] = mean_acc reports['weighted_acc'][k_report] = mean_wacc reports['weighted_acc_loose'][k_report] = mean_wacc_loose reports['weighted_acc_loose_indomain'][k_report] = mean_wacc_loose_masked reports['frac_perfect'][k_report] = frac_perf # mean_out_test = self.evaluate_input_set(holdout_inds, output_stats_units=np.setdiff1d(range(self.person1_units), train_p1_inds))[4] report_str = (f'Epoch {epoch:{epoch_digits}d}: ' + f'loss = {mean_loss:7.3f}' + # f', accuracy = {mean_acc:.3f}' + f', weighted acc = {mean_wacc:.3f}' + f', {frac_perf*100:3.0f}% perfect (train)') # testing if train_params['do_tree_holdout'] and k_report % train_params['reports_per_test'] == 0: k_ho = k_report // train_params['reports_per_test'] # offset change epochs if we don't want to reset training parameters during holdout if not train_params['reset_optim_params_during_holdout']: test_change_epochs = [e - epoch for e in change_epochs] n_changes_passed = sum(e <= 0 for e in test_change_epochs) this_test_optim_args = test_optim_args[n_changes_passed:] test_change_epochs = test_change_epochs[n_changes_passed:] else: test_change_epochs = change_epochs this_test_optim_args = test_optim_args etg, etg_string = self.generalize_test( train_params['batch_size'], np.arange(self.n_inputs), holdout_inds, train_params['test_max_epochs'], train_params['test_thresh'], test_change_epochs, this_test_optim_args ) reports['new_tree_etg'][k_ho] = etg report_str += f', epochs for new tree {etg_string:>{etg_digits}}' if train_params['do_combo_testing']: # following the paper, use a more lenient threshold (0.5) for test items _, test_acc, test_wacc, _, test_wacc_masked, test_frac_perf = \ self.evaluate_input_set(holdout_inds, threshold=0.5) reports['test_accuracy'][k_report] = test_acc reports['test_weighted_acc'][k_report] = test_wacc reports['test_weighted_acc_indomain'][k_report] = test_wacc_masked reports['test_frac_perfect'][k_report] = test_frac_perf report_str += f', {test_frac_perf*100:3.0f}% perfect (test)' print(report_str) # do training if epoch < total_epochs: order = util.permute(train_inds) self.train_epoch(order, optimizer, train_params['batch_size']) snaps_cpu = {stype: s.cpu().numpy() for stype, s in snaps.items()} return {'reports': reports, 'snaps': snaps_cpu}
import numpy as np from array import array import configurations import networkx as nx import time import matplotlib.pyplot as plt print("import sucess") #G=create.create(11,34) #G = nx.florentine_families_graph() start_time = time.time() G=nx.random_graphs.barabasi_albert_graph(20, 3) GN=nx.adjacency_matrix(G).todense() GA=np.asarray(GN) X=util.permute(GA) nx.draw(G) plt.savefig("1.png") #plt.show() ''' G1=nx.erdos_renyi_graph(11,0.327) GN1=nx.adjacency_matrix(G1).todense() GA1=np.asarray(GN1) ''' ergm1=models.p1() #print(X) g=ergm1.mcmc(G) #print(g[0]) print(ergm1.sum_weights(g)) print(ergm1.fit(g,1000,100,1))
def testingdivs3(): # all numbers can be written as composites of primes, each to a given exponent # number of divisors = products of each (exponent + 1) # add a prime. # try all permutations of the primes and powers up to that number of primes # check which value is triangular and has divisors > 500 # this took ~2 min to find a matching result, so it doesn't technically qualify # also, I don't know a way to definitively say that this answer is the best. # only by entering each returned value until one of them was correct was I # able to consider this solved minval = -1 keeplooping = True primes = [] answers = [] while keeplooping: primes.append(getnextprime(primes)) sstr = [] count = [] result = [] for i in range(len(primes)): sstr.append(i) count.append(len(primes)) result.append(0) exponentslist = permute(sstr, count, result, 0, []) for exponents in exponentslist: ans = np.prod(np.power(primes, exponents)) numdivs = 1 for exponent in exponents: numdivs *= (exponent + 1) # print(ans, numdivs) if ans > 0 and numdivs > 500 and useintegersqrttodetermineifnumistriangular( ans): if minval == -1: minval = ans answers.append(ans) print(ans) else: if ans < minval: minval = ans print(ans) answers.append(ans) if len(answers) % 10 == 0: print(answers) print() return None
# B/C Subsampled to fixed dimensionality def computeSampledDistances(data, dims): N = len(data) d = len(dims) dist = [] for n in range(N): for m in range(n): dist.append(linalg.norm(data[n,dims]-data[m,dims])/sqrt(d)) return dist D = [2, 8, 32, 128, 512] Cols = ['#FF0000', '#880000', '#000000', '#000088', '#0000FF'] Bins = arange(0, 1, 0.02) plt.xlabel('distance / sqrt(dimensionality)') plt.ylabel('# of pairs of points at that distance') plt.title('Dimensionality versus uniform point distances') for i,d in enumerate(D) : ind = arange(0,784) util.permute(ind) distances = computeSampledDistances(datasets.DigitData.X, ind[arange(0,d)]) plt.hist(distances, Bins, histtype='step', color=Cols[i]) plt.legend(['%d dims' % d for d in D]) plt.savefig('Subsampled.png') plt.show()
def do_training(self, **train_params): """ Train the network for the specified number of epochs, etc. Return representation snapshots, training reports, and snapshot/report epochs. If batch_size is negative, use one batch per epoch. Holdout testing: train with one entire item, context, or both excluded, then periodically (every `reports_per_test` reports) test how many epochs are needed to train network up to obtaining test_thresh accuracy on the held out inputs. If holdout_testing is 'domain', hold out and test on the last domain. Combo testing: For each domain, hold out one item/context pair. At each report time, test the accuracy of the network on the held-out items and contexts. If param snapshots is true, also returns all weights and biases of the network at each snapshot epoch. """ # Merge default params with overrides p = {**train_defaults, **train_params} holdout_testing = p['holdout_testing'].lower( ) if p['holdout_testing'] is not None else None # Deal with old domain holdout syntax if p['domains_to_hold_out'] > 0: if holdout_testing not in ['none', 'domain']: raise ValueError( "Can't do both domain and non-domain hold out") holdout_testing = 'domain' elif holdout_testing == 'domain': if 'domains_to_hold_out' in train_params: # case where holding out 0 domains was explicitly specified raise ValueError( 'Must hold out > 0 domains if doing domain holdout') p['domains_to_hold_out'] = 1 optimizer = torch.optim.SGD(self.parameters(), lr=p['lr']) do_holdout_testing = holdout_testing is not None and holdout_testing != 'none' holdout_item = holdout_testing in ['full', 'item'] holdout_ctx = holdout_testing in ['full', 'context', 'ctx'] if do_holdout_testing and p['do_combo_testing']: raise NotImplementedError( "That's too much, man - I'm not doing both holdout and combo testing!" ) self.train_x_inds = np.arange(self.n_inputs) test_x_item_inds = test_x_ctx_inds = None # for item/context holdout testing included_inds_item = included_inds_ctx = None # for item/context holdout testing test_x_inds = None # for combo or domain holdout testing if do_holdout_testing: if holdout_testing == 'domain': self.train_x_inds, test_x_inds = self.prepare_domain_holdout( n=p['domains_to_hold_out']) else: self.train_x_inds, test_x_item_inds, test_x_ctx_inds = self.prepare_holdout( holdout_item, holdout_ctx) # which indices to use during testing included_inds_item = np.concatenate( [self.train_x_inds, test_x_item_inds]) included_inds_ctx = np.concatenate( [self.train_x_inds, test_x_ctx_inds]) elif p['do_combo_testing']: self.train_x_inds, test_x_inds = self.prepare_combo_testing( n_per_domain=p['n_combo_per_domain']) # reset cached properties, if necessary for attr_name in ['train_item_inds', 'train_ctx_inds']: if hasattr(self, attr_name): delattr(self, attr_name) etg_digits = len(str(p['test_max_epochs'])) + 2 snap_epochs = util.calc_snap_epochs(**p) epoch_digits = len(str(snap_epochs[-1])) n_snaps = len(snap_epochs) snaps = [] params = {} if p['param_snapshots']: params = { pname: torch.empty((n_snaps, *pval.shape)) for pname, pval in self.named_parameters() } n_report = (p['num_epochs']) // p['report_freq'] + 1 n_etg = int((n_report - 1) // p['reports_per_test'] + 1) train_reports = [ 'loss', 'accuracy', 'weighted_acc', 'weighted_acc_loose', 'weighted_acc_loose_indomain' ] test_reports = [ 'test_accuracy', 'test_weighted_acc', 'test_weighted_acc_loose', 'test_weighted_acc_loose_indomain' ] reports = {rname: np.zeros(n_report) for rname in train_reports} if holdout_item: reports['etg_item'] = np.zeros(n_etg, dtype=int) # "epochs to generalize" if holdout_ctx: reports['etg_context'] = np.zeros(n_etg, dtype=int) if holdout_testing == 'domain': reports['etg_domain'] = np.zeros(n_etg, dtype=int) for kd in range(1, p['domains_to_hold_out']): reports[f'etg_domain{kd+1}'] = np.zeros(n_etg, dtype=int) if p['do_combo_testing']: for test_rname in test_reports: reports[test_rname] = np.zeros(n_report) for epoch in range(p['num_epochs'] + (1 if p['include_final_eval'] else 0)): # collect snapshot if epoch in snap_epochs: k_snap = snap_epochs.index(epoch) snaps.append(self.take_snapshots()) with torch.no_grad(): if p['param_snapshots']: for pname, pval in self.named_parameters(): params[pname][k_snap] = pval # report progress if epoch % p['report_freq'] == 0: k_report = epoch // p['report_freq'] # get current performance perf_stats = self.evaluate_input_set(self.train_x_inds) mean_loss, mean_acc, mean_wacc, mean_wacc_loose, mean_wacc_loose_masked = perf_stats report_str = (f'Epoch {epoch:{epoch_digits}d}: ' + f'loss = {mean_loss:7.3f}, ' + f'weighted acc = {mean_wacc:.3f}') reports['loss'][k_report] = mean_loss reports['accuracy'][k_report] = mean_acc reports['weighted_acc'][k_report] = mean_wacc reports['weighted_acc_loose'][k_report] = mean_wacc_loose reports['weighted_acc_loose_indomain'][ k_report] = mean_wacc_loose_masked if do_holdout_testing and k_report % p['reports_per_test'] == 0: k_test = int(k_report // p['reports_per_test']) # Do item and context generalize tests separately if holdout_item: item_etg, item_etg_string = self.generalize_test( p['batch_size'], optimizer, included_inds_item, test_x_item_inds, thresh=p['test_thresh'], max_epochs=p['test_max_epochs']) report_str += f', epochs for new item = {item_etg_string:>{etg_digits}}' reports['etg_item'][k_test] = item_etg if holdout_ctx: ctx_etg, ctx_etg_string = self.generalize_test( p['batch_size'], optimizer, included_inds_ctx, test_x_ctx_inds, thresh=p['test_thresh'], max_epochs=p['test_max_epochs']) report_str += f', epochs for new context = {ctx_etg_string:>{etg_digits}}' reports['etg_context'][k_test] = ctx_etg if holdout_testing == 'domain': for kd, (dname, this_test_inds) in enumerate( test_x_inds.items()): included_inds = np.concatenate( (self.train_x_inds, this_test_inds)) domain_etg, domain_etg_string = self.generalize_test( p['batch_size'], optimizer, included_inds, this_test_inds, thresh=p['test_thresh'], max_epochs=p['test_max_epochs']) report_str += f'\n\tEpochs to learn domain {dname}: {domain_etg_string:>{etg_digits}}' report_type = 'etg_domain' + (str(kd + 1) if kd > 0 else '') reports[report_type][k_test] = domain_etg if p['do_combo_testing']: test_perf_stats = self.evaluate_input_set(test_x_inds) test_acc, test_wacc, test_wacc_loose, test_wacc_loose_masked = test_perf_stats[ 1:] report_str += f', test weighted acc = {test_wacc:.3f}' reports['test_accuracy'][k_report] = test_acc reports['test_weighted_acc'][k_report] = test_wacc reports['test_weighted_acc_loose'][ k_report] = test_wacc_loose reports['test_weighted_acc_loose_indomain'][ k_report] = test_wacc_loose_masked print(report_str) # do training if epoch < p['num_epochs']: order = util.permute(self.train_x_inds) self.train_epoch(order, p['batch_size'], optimizer) if p['scheduler'] is not None: p['scheduler'].step() # concatenate snapshots and move to cpu if len(snaps) > 0: snaps_cpu = { stype: np.stack([s[stype].cpu().numpy() for s in snaps]) for stype in snaps[0].keys() } else: snaps_cpu = {} ret_dict = {'snaps': snaps_cpu, 'reports': reports} if p['param_snapshots']: ret_dict['params'] = { pname: pval.cpu().numpy() for pname, pval in params.items() } return ret_dict
def activate(x, actfun, p=1, k=1, M=None, layer_type='conv', permute_type='shuffle', shuffle_maps=None, alpha_primes=None, alpha_dist=None, reduce_actfuns=False): if permute_type == 'invert': assert p % k == 0, 'k must divide p if you use the invert shuffle type ya big dummy.' # Unsqueeze a dimension and populate it with permutations of our inputs x = x.unsqueeze(2) curr_permute = permute_type permute_base = 0 for i in range(1, p): curr_shuffle = shuffle_maps[i] if permute_type == 'invert': if i % k == 0: curr_permute = 'shuffle' permute_base = 0 else: curr_permute = 'invert' permute_base = x.shape[2] - 1 curr_shuffle = torch.arange(k) curr_shuffle[0] = i % k curr_shuffle[i % k] = 0 permutation = util.permute(x[:, :, permute_base, ...], curr_permute, layer_type, k, offset=i, shuffle_map=curr_shuffle).unsqueeze(2) x = torch.cat((x[:, :, :i, ...], permutation), dim=2) # This transpose makes it so that during the next reshape (when we combine our p permutations # into a single dimension), the order goes one full permutation after another (instead of # interleaving the permutations) x = torch.transpose(x, dim0=1, dim1=2) # Combine p permutations into a single dimension, then cluster into groups of size k batch_size = x.shape[0] if layer_type == 'conv': num_channels = x.shape[2] height = x.shape[3] width = x.shape[4] # print(x.shape) x = x.reshape(batch_size, int(num_channels * p / k), k, height, width) elif layer_type == 'linear': num_channels = M x = x.reshape(batch_size, int(num_channels * p / k), k) bin_partition_actfuns = [ 'bin_part_full', 'bin_part_max_min_sgm', 'bin_part_max_sgm', 'ail_part_full', 'ail_part_or_and_xnor', 'ail_part_or_xnor' ] bin_all_actfuns = [ 'bin_all_full', 'bin_all_max_min', 'bin_all_max_sgm', 'bin_all_max_min_sgm', 'ail_all_full', 'ail_all_or_and', 'ail_all_or_xnor', 'ail_all_or_and_xnor' ] if actfun == 'combinact': x = combinact(x, p=p, layer_type=layer_type, alpha_primes=alpha_primes, alpha_dist=alpha_dist, reduce_actfuns=reduce_actfuns) elif actfun == 'cf_relu' or actfun == 'cf_abs': x = coin_flip(x, actfun, M=num_channels * p, k=k) elif actfun in bin_partition_actfuns or actfun in bin_all_actfuns: x = binary_ops(x, actfun, layer_type, bin_partition_actfuns, bin_all_actfuns) elif actfun == 'groupsort': x = groupsort(x, layer_type) else: x = x.squeeze() x = _ACTFUNS[actfun](x) return x
#!/bin/env python3 import sys import util """ Given: A positive integer 'n'. Return: The total number of permutations of length 'n', followed by a list of all such permutations, in any order """ n = int(sys.stdin.read(1)) set = list(range(1, n + 1)) permutations = list(util.permute(set)) print(len(permutations)) for perm in permutations: print(' '.join(str(i) for i in perm))