def generate(nb_starting_values=3, max_input=9, prog_len=6, nb_runs=5):
prog_len = (1 + torch.randint(2 * prog_len, (1,))).clamp(max=prog_len).item()
- prog = [rpl_ops[k] for k in torch.randint(len(rpl_ops), (prog_len,))]
- result = []
- for _ in range(nb_runs):
- stack = [x.item() for x in torch.randint(max_input + 1, (nb_starting_values,))]
- result_stack = rpl_exec(prog, stack)
- result = result + ["<input>"] + stack + ["<output>"] + result_stack
+ while True:
+ no_empty_stack = True
+ prog = [rpl_ops[k] for k in torch.randint(len(rpl_ops), (prog_len,))]
+
+ result = []
+ for _ in range(nb_runs):
+ stack = [
+ x.item() for x in torch.randint(max_input + 1, (nb_starting_values,))
+ ]
+ result_stack = rpl_exec(prog, stack)
+ if len(result_stack) == 0:
+ no_empty_stack = False
+ result = result + ["<input>"] + stack + ["<output>"] + result_stack
+
+ result = result + ["<prog>"] + prog
+ result = result + ["<end>"]
+ if no_empty_stack:
+ break
- result = result + ["<prog>"] + prog
- result = result + ["<end>"]
return result
if len(set(prog) - set(rpl_ops)) > 0:
# Program is not valid, we count 100% error
for start_stack, target_stack in io:
- stacks.append((start_stack, target_stack, "N/A", False))
+ stacks.append((start_stack, target_stack, ["N/A"], False))
nb_total += len(target_stack)
nb_errors += len(target_stack)
max_input=9,
prog_len=6,
nb_runs=5,
+ logger=None,
device=torch.device("cpu"),
):
super().__init__()
self.train_input = self.tensorize(train_sequences)
self.test_input = self.tensorize(test_sequences)
+ if logger is not None:
+ for x in self.train_input[:10]:
+ end = (x != self.t_nul).nonzero().max().item() + 1
+ seq = [self.id2token[i.item()] for i in x[:end]]
+ s = " ".join(seq)
+ logger(f"example_seq {s}")
+
self.nb_codes = max(self.train_input.max(), self.test_input.max()) + 1
def batches(self, split="train", nb_to_use=-1, desc=None):
_, _, gt_prog, _ = rpl.compute_nb_errors(gt_seq)
gt_prog = " ".join([str(x) for x in gt_prog])
prog = " ".join([str(x) for x in prog])
- logger(f"PROG [{gt_prog}] PREDICTED [{prog}]")
+ comment = "*" if nb_errors == 0 else "-"
+ logger(f"{comment} PROG [{gt_prog}] PREDICTED [{prog}]")
for start_stack, target_stack, result_stack, correct in stacks:
- comment = " CORRECT" if correct else ""
+ comment = "*" if correct else "-"
start_stack = " ".join([str(x) for x in start_stack])
target_stack = " ".join([str(x) for x in target_stack])
result_stack = " ".join([str(x) for x in result_stack])
logger(
- f" [{start_stack}] -> [{target_stack}] PREDICTED [{result_stack}]{comment}"
+ f" {comment} [{start_stack}] -> [{target_stack}] PREDICTED [{result_stack}]"
)
nb_to_log -= 1