From 38d3035f027881bb2baffdaffc8cd666d3df5dba Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fran=C3=A7ois=20Fleuret?= Date: Tue, 4 Jul 2023 15:56:03 +0200 Subject: [PATCH] Update. --- expr.py | 57 +++++++++++++++++++++++++++++++-------------------------- main.py | 35 +++++++++++++++++++++++++++++------ 2 files changed, 60 insertions(+), 32 deletions(-) diff --git a/expr.py b/expr.py index 8a89945..b453f23 100755 --- a/expr.py +++ b/expr.py @@ -7,71 +7,76 @@ import torch, torchvision from torch import nn from torch.nn import functional as F + def random_var(nb_variables=None, variables=None): if variables is None: - return chr(ord('A') + torch.randint(nb_variables, (1,)).item()) + return chr(ord("A") + torch.randint(nb_variables, (1,)).item()) else: l = list(variables) return l[torch.randint(len(l), (1,)).item()] + def random_expr(variables, budget): if budget <= 5: - op=torch.randint(2, (1,)).item() + op = torch.randint(2, (1,)).item() if op == 0 and len(variables) > 0: return random_var(variables=variables) else: return str(torch.randint(10, (1,)).item()) else: - op=torch.randint(4, (1,)).item() + op = torch.randint(4, (1,)).item() if op == 0: - e=random_expr(variables,budget-2) - if ("+" in e or "-" in e or "*" in e) and (e[0]!="(" or e[-1]!=")"): - return "("+e+")" + e = random_expr(variables, budget - 2) + if ("+" in e or "-" in e or "*" in e) and (e[0] != "(" or e[-1] != ")"): + return "(" + e + ")" else: return e else: - b = 2 + torch.randint(budget-5, (1,)).item() - e1=random_expr(variables,b) - e2=random_expr(variables,budget-b-1) + b = 2 + torch.randint(budget - 5, (1,)).item() + e1 = random_expr(variables, b) + e2 = random_expr(variables, budget - b - 1) if op == 1: - return e1+"+"+e2 + return e1 + "+" + e2 elif op == 2: - return e1+"+"+e2 + return e1 + "+" + e2 elif op == 3: - return e1+"*"+e2 + return e1 + "*" + e2 + def generate_program(nb_variables, length): s = "" variables = set() while len(s) < length: v = random_var(nb_variables=nb_variables) - s += v+"="+random_expr(variables,budget = min(20,length-3-len(s)))+";" + s += v + "=" + random_expr(variables, budget=min(20, length - 3 - len(s))) + ";" variables.add(v) return s, variables -def generate_sequences(nb, nb_variables = 5, length=20): - sequences=[] + +def generate_sequences(nb, nb_variables=5, length=20): + sequences = [] for n in range(nb): result = None - while result==None or max(result.values())>100: - p,v=generate_program(nb_variables, length) - v=", ".join([ "\""+v+"\": "+v for v in v ]) - ldict={} - exec(p+"result={"+v+"}",globals(),ldict) - result=ldict["result"] - - k=list(result.keys()) + while result == None or max(result.values()) > 100: + p, v = generate_program(nb_variables, length) + v = ", ".join(['"' + v + '": ' + v for v in v]) + ldict = {} + exec(p + "result={" + v + "}", globals(), ldict) + result = ldict["result"] + + k = list(result.keys()) k.sort() - sequences.append(p+" "+";".join([v+":"+str(result[v]) for v in k])) + sequences.append(p + " " + ";".join([v + ":" + str(result[v]) for v in k])) return sequences + if __name__ == "__main__": import time + start_time = time.perf_counter() - sequences=generate_sequences(1000) + sequences = generate_sequences(1000) end_time = time.perf_counter() for s in sequences[:10]: print(s) print(f"{len(sequences) / (end_time - start_time):.02f} samples per second") - diff --git a/main.py b/main.py index 319e94b..b774fce 100755 --- a/main.py +++ b/main.py @@ -32,7 +32,10 @@ parser = argparse.ArgumentParser( ) parser.add_argument( - "--task", type=str, default="picoclvr", help="picoclvr, mnist, maze, snake, stack, expr" + "--task", + type=str, + default="picoclvr", + help="picoclvr, mnist, maze, snake, stack, expr", ) parser.add_argument("--log_filename", type=str, default="train.log", help=" ") @@ -223,7 +226,6 @@ def masked_inplace_autoregression( progress_bar_desc="autoregression", device=torch.device("cpu"), ): - batches = zip(input.split(batch_size), ar_mask.split(batch_size)) if progress_bar_desc is not None: @@ -1018,11 +1020,32 @@ class TaskExpr(Task): train_sequences = expr.generate_sequences(nb_train_samples) test_sequences = expr.generate_sequences(nb_test_samples) - self.char2id = dict([ (c,n) for n,c in enumerate(set("".join(train_sequences + test_sequences))) ]) - self.id2char = dict([ (n,c) for n,c in self.char2id.items() ]) + self.char2id = dict( + [ + (c, n) + for n, c in enumerate(set("".join(train_sequences + test_sequences))) + ] + ) + self.id2char = dict([(n, c) for n, c in self.char2id.items()]) len_max = max([len(x) for x in train_sequences + test_sequences]) - self.train_input = torch.cat([torch.tensor([char2id(c) for c in s + " "*(len_max-len(s))] for s in train_sequences)], 0) - self.test_input = torch.cat([torch.tensor([char2id(c) for c in s + " "*(len_max-len(s))] for s in test_sequences)], 0) + self.train_input = torch.cat( + [ + torch.tensor( + [char2id(c) for c in s + " " * (len_max - len(s))] + for s in train_sequences + ) + ], + 0, + ) + self.test_input = torch.cat( + [ + torch.tensor( + [char2id(c) for c in s + " " * (len_max - len(s))] + for s in test_sequences + ) + ], + 0, + ) self.nb_codes = max(self.train_input.max(), self.test_input.max()) + 1 def batches(self, split="train", nb_to_use=-1, desc=None): -- 2.39.5