batch_size,
input,
ar_mask,
+ temperature,
deterministic_synthesis,
forbidden_tokens=None,
logit_biases=None,
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
+
######################################################################
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=6, padding=4)
logger(f"wrote {image_name}")
self.batch_size = batch_size
self.device = device
- self.height = 7
- self.width = 9
+ self.height = 6
+ self.width = 8
self.train_input = world.generate_seq(
nb_train_samples, height=self.height, width=self.width
nb_test_samples, height=self.height, width=self.width
).to(device)
- # print()
- # for a in world.seq2str(self.train_input):
- # print(a)
- # for a in world.seq2str(self.test_input):
- # print(a)
- # exit(0)
-
self.nb_codes = max(self.train_input.max(), self.test_input.max()) + 1
self.train_quizzes = []
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,
)
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,
)
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)
nb,
model,
other_models,
+ desired_average_logits=None,
):
###############################################################
# Generate quizzes with model
)
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
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,
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,
for k in nb_correct:
f.write(f"{k}\n")
- return quizzes, nb_correct.sum(dim=0)
+ return quizzes, nb_correct.sum(dim=0), average_logits