X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=tasks.py;h=b967465de6c23a94b9ee1af8a672337167903ac8;hb=8a548630c88957264306db4354e880414b0fa8ef;hp=cb5900b74877e1582e9a8f1cef0dd76c8ea61814;hpb=1506fb905b0f83034107e8e8dc336d10bdb1a7a7;p=culture.git diff --git a/tasks.py b/tasks.py index cb5900b..b967465 100755 --- a/tasks.py +++ b/tasks.py @@ -22,6 +22,7 @@ def masked_inplace_autoregression( batch_size, input, ar_mask, + temperature, deterministic_synthesis, forbidden_tokens=None, logit_biases=None, @@ -44,17 +45,22 @@ def masked_inplace_autoregression( t = model.training model.eval() + sum_logits = 0 + for input, ar_mask in batches: - model.masked_inplace_autoregression( - input, - ar_mask, - deterministic_synthesis, - forbidden_tokens, - logit_biases, + sum_logits += model.masked_inplace_autoregression( + input=input, + ar_mask=ar_mask, + temperature=temperature, + deterministic_synthesis=deterministic_synthesis, + forbidden_tokens=forbidden_tokens, + forced_biases=logit_biases, ) model.train(t) + return sum_logits + ###################################################################### @@ -79,9 +85,9 @@ import world class World(Task): def save_image(self, input, result_dir, filename, logger): - img = world.sample2img(input.to("cpu"), self.height, self.width) + img = world.seq2img(input.to("cpu"), self.height, self.width) image_name = os.path.join(result_dir, filename) - torchvision.utils.save_image(img.float() / 255.0, image_name, nrow=8, padding=2) + torchvision.utils.save_image(img.float() / 255.0, image_name, nrow=6, padding=4) logger(f"wrote {image_name}") def make_ar_mask(self, input): @@ -104,11 +110,11 @@ class World(Task): self.height = 6 self.width = 8 - self.train_input = world.generate( + self.train_input = world.generate_seq( nb_train_samples, height=self.height, width=self.width ).to(device) - self.test_input = world.generate( + self.test_input = world.generate_seq( nb_test_samples, height=self.height, width=self.width ).to(device) @@ -119,7 +125,7 @@ class World(Task): if result_dir is not None: self.save_image( - self.train_input[:96], result_dir, f"world_train.png", logger + self.train_input[:72], result_dir, f"world_train.png", logger ) def batches(self, split="train", desc=None): @@ -167,11 +173,12 @@ class World(Task): result = input.clone() * (1 - ar_mask) masked_inplace_autoregression( - model, - self.batch_size, - result, - ar_mask, - deterministic_synthesis, + model=model, + batch_size=self.batch_size, + input=result, + ar_mask=ar_mask, + temperature=1.0, + deterministic_synthesis=deterministic_synthesis, progress_bar_desc=None, device=self.device, ) @@ -205,17 +212,18 @@ class World(Task): result = input.clone() * (1 - ar_mask) masked_inplace_autoregression( - model, - self.batch_size, - result, - ar_mask, - deterministic_synthesis, + model=model, + batch_size=self.batch_size, + input=result, + ar_mask=ar_mask, + temperature=1.0, + deterministic_synthesis=deterministic_synthesis, progress_bar_desc=None, device=self.device, ) self.save_image( - result[:96], + result[:72], result_dir, f"world_prediction_{n_epoch:04d}_{model.id:02d}.png", logger, @@ -223,6 +231,14 @@ class World(Task): return main_test_accuracy + def renew_samples(self, nb, for_train=True): + input = self.train_input if for_train else self.test_input + nb = min(nb, input.size(0)) + input[:-nb] = input[nb:].clone() + input[-nb:] = world.generate_seq(nb, height=self.height, width=self.width).to( + self.device + ) + def store_new_quizzes(self, new_quizzes, for_train=True): if for_train: self.train_quizzes.append(new_quizzes) @@ -237,6 +253,7 @@ class World(Task): nb, model, other_models, + desired_average_logits=None, ): ############################################################### # Generate quizzes with model @@ -246,16 +263,35 @@ class World(Task): ) ar_mask = torch.full(quizzes.size(), 1, device=self.device) - masked_inplace_autoregression( - model, - self.batch_size, - quizzes, - ar_mask, + sum_logits = masked_inplace_autoregression( + model=model, + batch_size=self.batch_size, + input=quizzes, + ar_mask=ar_mask, + temperature=1.0, deterministic_synthesis=False, progress_bar_desc="creating quizzes", device=self.device, ) + average_logits = sum_logits / quizzes.numel() + + # It's a bit brutal to do it twice, we should probably have a + # moving average and apply it right away + + if desired_average_logits is not None: + temperature = average_logits / desired_average_logits + masked_inplace_autoregression( + model=model, + batch_size=self.batch_size, + input=quizzes, + ar_mask=ar_mask, + temperature=temperature, + deterministic_synthesis=False, + progress_bar_desc="creating quizzes", + device=self.device, + ) + ############################################################### # Create the reverse quizzes @@ -274,16 +310,17 @@ class World(Task): # Check how many of the other models can solve them in both # directions - nb_correct = 0 + nb_correct = [] for m in other_models: result = quizzes.clone() masked_inplace_autoregression( - m, - self.batch_size, - result, - ar_mask, + model=m, + batch_size=self.batch_size, + input=result, + ar_mask=ar_mask, + temperature=1.0, deterministic_synthesis=True, progress_bar_desc="solving quizzes", device=self.device, @@ -294,10 +331,11 @@ class World(Task): reverse_result = reverse_quizzes.clone() masked_inplace_autoregression( - m, - self.batch_size, - reverse_result, - ar_mask, + model=m, + batch_size=self.batch_size, + input=reverse_result, + ar_mask=ar_mask, + temperature=1.0, deterministic_synthesis=True, progress_bar_desc="solving reversed quizzes", device=self.device, @@ -307,6 +345,13 @@ class World(Task): (reverse_quizzes == reverse_result).long().min(dim=-1).values ) - nb_correct += correct * reverse_correct + nb_correct.append((correct * reverse_correct)[None, :]) + + nb_correct = torch.cat(nb_correct, dim=0) + + filename = os.path.join(result_dir, "correct_{n_epoch:04d}.dat") + with open(filename, "w") as f: + for k in nb_correct: + f.write(f"{k}\n") - return quizzes, nb_correct + return quizzes, nb_correct.sum(dim=0), average_logits