From: Francois Fleuret Date: Wed, 27 Jul 2022 14:42:28 +0000 (+0200) Subject: OCDC X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=1ad9ea3cca4489b07bad8521966382f66a493eea;p=mygpt.git OCDC --- diff --git a/main.py b/main.py index 339d185..5f3e8cf 100755 --- a/main.py +++ b/main.py @@ -156,7 +156,7 @@ import picoclvr class TaskPicoCLVR(Task): - def descr2tensor(self, descr): + def tensorize(self, descr): t = [ [ self.token2id[u] for u in s ] for s in descr ] return torch.tensor(t, device = self.device) @@ -173,8 +173,8 @@ class TaskPicoCLVR(Task): descr = [ s.strip().split(' ') for s in descr ] l = max([ len(s) for s in descr ]) - #descr = [ [ '' ] * (l - len(s)) + s for s in descr ] - descr = [ s + [ '' ] * (l - len(s)) for s in descr ] + #descr = [ [ '' ] * (l - len(s)) + s for s in descr ] + descr = [ s + [ '' ] * (l - len(s)) for s in descr ] return descr @@ -196,8 +196,8 @@ class TaskPicoCLVR(Task): self.id2token = dict([ (n, t) for n, t in enumerate(tokens) ]) # Tokenize the train and test sets - self.train_input = descr2tensor(self.train_descr) - self.test_input = descr2tensor(self.test_descr) + self.train_input = self.tensorize(self.train_descr) + self.test_input = self.tensorize(self.test_descr) def batches(self, split = 'train'): assert split in { 'train', 'test' } @@ -208,14 +208,6 @@ class TaskPicoCLVR(Task): def vocabulary_size(self): return len(self.token2id) - def generate(self, primer_descr, model, nb_tokens): - results = autoregression( - model, self.batch_size, - nb_samples = 1, nb_tokens = nb_tokens, primer = descr2tensor(primer_descr), - device = self.device - ) - return ' '.join([ self.id2token[t.item()] for t in results.flatten() ]) - def produce_results(self, n_epoch, model): nb_tokens = self.height * self.width + 3 result_descr = [ ] @@ -229,10 +221,20 @@ class TaskPicoCLVR(Task): ]: for k in range(nb_per_primer): - result_descr.append(self.generate(primer_descr, model, nb_tokens)) + results = autoregression( + model, self.batch_size, + nb_samples = 1, nb_tokens = nb_tokens, + primer = self.tensorize(primer_descr), + device = self.device + ) + r = ' '.join([ self.id2token[t.item()] for t in results.flatten() ]) + result_descr.append(r) + + img = [ + picoclvr.descr2img(d, height = self.height, width = self.width) + for d in result_descr + ] - img = [ picoclvr.descr2img(d, height = self.height, width = self.width) - for d in result_descr ] img = torch.cat(img, 0) image_name = f'result_picoclvr_{n_epoch:04d}.png' torchvision.utils.save_image( @@ -276,7 +278,7 @@ class TaskWiki103(Task): self.vocab = torchtext.vocab.build_vocab_from_iterator( yield_tokens(), - specials = [ '', '' ], + specials = [ '', '' ], min_freq = self.min_freq ) @@ -284,7 +286,7 @@ class TaskWiki103(Task): def tensorize(self, s): a = max(len(x) for x in s) - return torch.tensor([ self.vocab(x + [ '' ] * (a - len(x))) for x in s ]) + return torch.tensor([ self.vocab(x + [ '' ] * (a - len(x))) for x in s ]) def yield_batches(self, ds): s = [ ] @@ -342,7 +344,7 @@ class TaskWiki103(Task): else: t_next = logits.argmax() t_generated.append(self.vocab.lookup_token(t_next)) - if t_generated[-1] == '': break + if t_generated[-1] == '': break s = ' '.join(t_generated)