device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
######################################################################
-
parser = argparse.ArgumentParser(description = 'My own GPT.')
parser.add_argument('--log_filename',
results = torch.cat((primer, results), 1)
for input in results.split(batch_size):
- for s in tqdm.tqdm(range(first, input.size(1)), desc = 'synth'):
+ for s in range(first, input.size(1)):
output = model(input)
logits = output[:, s]
if args.synthesis_sampling:
def vocabulary_size(self):
pass
- def produce_results(self, n_epoch, model, nb_tokens = 50):
+ def produce_results(self, n_epoch, model):
pass
######################################################################
class TaskPicoCLVR(Task):
- def descr2tensor(self, descr):
+ def tensorize(self, descr):
+ descr = [ s.strip().split(' ') for s in descr ]
+ l = max([ len(s) for s in descr ])
+ #descr = [ [ '<nul>' ] * (l - len(s)) + s for s in descr ]
+ descr = [ s + [ '<nul>' ] * (l - len(s)) for s in descr ]
t = [ [ self.token2id[u] for u in s ] for s in descr ]
return torch.tensor(t, device = self.device)
device = torch.device('cpu')):
def generate_descr(nb):
- descr = picoclvr.generate(
+ return picoclvr.generate(
nb,
height = self.height, width = self.width,
nb_colors = nb_colors
)
- descr = [ s.strip().split(' ') for s in descr ]
- l = max([ len(s) for s in descr ])
- #descr = [ [ '<unk>' ] * (l - len(s)) + s for s in descr ]
- descr = [ s + [ '<unk>' ] * (l - len(s)) for s in descr ]
-
- return descr
-
self.height = height
self.width = width
self.batch_size = batch_size
self.test_descr = generate_descr((nb * 1) // 5)
# Build the tokenizer
- tokens = set()
+ tokens = { '<nul>' }
for d in [ self.train_descr, self.test_descr ]:
for s in d:
- for t in s: tokens.add(t)
+ for t in s.strip().split(' '): tokens.add(t)
self.token2id = dict([ (t, n) for n, t in enumerate(tokens) ])
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' }
- if split == 'train':
- for batch in tqdm.tqdm(self.train_input.split(self.batch_size), desc = f'epoch-{split}'):
- yield batch
- else:
- for batch in tqdm.tqdm(self.test_input.split(self.batch_size), desc = f'epoch-{split}'):
- yield batch
+ input = self.train_input if split == 'train' else self.test_input
+ for batch in tqdm.tqdm(input.split(self.batch_size), desc = f'epoch-{split}'):
+ yield batch
def vocabulary_size(self):
return len(self.token2id)
- def generate(self, descr_primer, model, nb_tokens):
- results = autoregression(
- model, self.batch_size,
- 1, nb_tokens, primer = descr2tensor(descr_primer),
- device = self.device
- )
- return ' '.join([ self.id2token[t.item()] for t in results.flatten() ])
-
- def produce_results(self, n_epoch, model, nb_tokens = None):
- if nb_tokens is None:
- nb_tokens = self.height * self.width + 3
+ def produce_results(self, n_epoch, model):
+ nb_tokens = self.height * self.width + 3
result_descr = [ ]
nb_per_primer = 8
- for descr_primer in [
+ for primer_descr in [
'red above green <sep> green top <sep> blue right of red <img>',
'there is red <sep> there is yellow <sep> there is blue <img>',
'red below yellow <sep> yellow below green <sep> green below blue <sep> red right <sep> yellow left <sep> green right <sep> blue left <img>',
]:
for k in range(nb_per_primer):
- result_descr.append(self.generate(descr_primer, model, nb_tokens))
+ results = autoregression(
+ model, self.batch_size,
+ nb_samples = 1, nb_tokens_to_generate = 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(
self.vocab = torchtext.vocab.build_vocab_from_iterator(
yield_tokens(),
- specials = [ '<unk>', '<non>' ],
+ specials = [ '<unk>', '<nul>' ],
min_freq = self.min_freq
)
def tensorize(self, s):
a = max(len(x) for x in s)
- return torch.tensor([ self.vocab(x + [ '<non>' ] * (a - len(x))) for x in s ])
+ return torch.tensor([ self.vocab(x + [ '<nul>' ] * (a - len(x))) for x in s ])
def yield_batches(self, ds):
s = [ ]
def vocabulary_size(self):
return len(self.vocab)
- def produce_results(self, n_epoch, model, nb_tokens = 50):
+ def produce_results(self, n_epoch, model):
+ nb_tokens = 50
file_name = f'result_wiki103_{n_epoch:04d}.txt'
with open(file_name, 'w') as outfile:
else:
t_next = logits.argmax()
t_generated.append(self.vocab.lookup_token(t_next))
- if t_generated[-1] == '<non>': break
+ if t_generated[-1] == '<nul>': break
s = ' '.join(t_generated)
def vocabulary_size(self):
return 256
- def produce_results(self, n_epoch, model, nb_samples = 64):
+ def produce_results(self, n_epoch, model):
+ nb_samples = 64
results = autoregression(model, self.batch_size, nb_samples, 28 * 28, device = self.device)
image_name = f'result_mnist_{n_epoch:04d}.png'
torchvision.utils.save_image(1 - results.reshape(-1, 1, 28, 28) / 255.,