From: François Fleuret Date: Thu, 23 Mar 2023 16:01:35 +0000 (+0100) Subject: Update X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=24d40cc2693dddf190fce9d43e458a86b685a7d3;p=beaver.git Update --- diff --git a/beaver.py b/beaver.py index f5f092b..86008f6 100755 --- a/beaver.py +++ b/beaver.py @@ -143,10 +143,10 @@ def generation_order(x, fixed_len): return order -def reorder(x, order, back=False): # x is NxTxD1x...xDk, order is NxT' +def reorder(x, order, reverse=False): # x is NxTxD1x...xDk, order is NxT' u = x.reshape(x.size()[:2] + (-1,)) order = order.unsqueeze(-1).expand(-1, -1, u.size(-1)) - if back: + if reverse: v = u.new(u.size()) v.scatter_(1, order, u) else: @@ -199,7 +199,7 @@ def compute_perplexity(model, task, fixed_len, split="train"): input = input.to(device) x, order = shuffle(input, fixed_len) x = model(mygpt.BracketedSequence(x), order=order).x - output = reorder(x, order, back=True) + output = reorder(x, order, reverse=True) loss = F.cross_entropy(output.transpose(1, 2), input) acc_loss += loss.item() * input.size(0) nb_samples += input.size(0) @@ -265,7 +265,7 @@ def oneshot(gpt, task): for mazes, policies in task.policy_batches(split="train"): x, order = shuffle(mazes, task.height * task.width) x = gpt(mygpt.BracketedSequence(x), mode=args.oneshot_input, order=order).x - output_gpt = reorder(x, order, back=True) + output_gpt = reorder(x, order, reverse=True) output = model(output_gpt) loss = compute_loss(mazes, output, policies, task.height, task.width) @@ -280,7 +280,7 @@ def oneshot(gpt, task): for mazes, policies in task.policy_batches(split="test"): x, order = shuffle(mazes, task.height * task.width) x = gpt(mygpt.BracketedSequence(x), mode=args.oneshot_input, order=order).x - output_gpt = reorder(x, order, back=True) + output_gpt = reorder(x, order, reverse=True) output = model(output_gpt) loss = compute_loss(mazes, output, policies, task.height, task.width) acc_test_loss += loss.item() * mazes.size(0) @@ -295,7 +295,7 @@ def oneshot(gpt, task): policies = task.test_policies[:32] x, order = shuffle(mazes, task.height * task.width) x = gpt(mygpt.BracketedSequence(x), mode=args.oneshot_input, order=order).x - output_gpt = reorder(x, order, back=True) + output_gpt = reorder(x, order, reverse=True) output = model(output_gpt) if args.oneshot_output == "policy": targets = policies.permute(0, 2, 1) @@ -440,7 +440,7 @@ class TaskMaze(Task): masked_inplace_autoregression( model, self.batch_size, x, ar_mask, order=order ) - result = reorder(x, order, back=True) + result = reorder(x, order, reverse=True) mazes, paths = self.seq2map(result) nb_correct += maze.path_correctness(mazes, paths).long().sum() nb_total += mazes.size(0) @@ -475,7 +475,7 @@ class TaskMaze(Task): masked_inplace_autoregression( model, self.batch_size, x, ar_mask, order=order ) - result = reorder(x, order, back=True) + result = reorder(x, order, reverse=True) mazes, paths = self.seq2map(input) _, predicted_paths = self.seq2map(result) @@ -630,7 +630,7 @@ for n_epoch in range(nb_epochs_finished, args.nb_epochs): input = input.to(device) x, order = shuffle(input, task.height * task.width) x = model(mygpt.BracketedSequence(x), order=order).x - output = reorder(x, order, back=True) + output = reorder(x, order, reverse=True) loss = F.cross_entropy(output.transpose(1, 2), input) acc_train_loss += loss.item() * input.size(0) nb_train_samples += input.size(0)