def one_shot(gpt, task):
t = gpt.training
gpt.eval()
+
model = nn.Sequential(
+ nn.Linear(args.dim_model, args.dim_model),
+ nn.ReLU(),
nn.Linear(args.dim_model, args.dim_model),
nn.ReLU(),
nn.Linear(args.dim_model, 4),
for input, targets in task.policy_batches(split="train"):
output_gpt = gpt(mygpt.BracketedSequence(input), with_readout=False).x
output = model(output_gpt)
+ targets = targets * (input.unsqueeze(-1) == maze.v_empty)
+ output = output * (input.unsqueeze(-1) == maze.v_empty)
+ # loss = (output.softmax(-1) - targets).abs().max(-1).values.mean()
loss = (
- -(output.log_softmax(-1) * targets).sum(-1).mean()
- + targets.xlogy(targets).sum(-1).mean()
+ -(output.log_softmax(-1) * targets).sum()
+ / (input == maze.v_empty).sum()
)
acc_train_loss += loss.item() * input.size(0)
nb_train_samples += input.size(0)
for input, targets in task.policy_batches(split="test"):
output_gpt = gpt(mygpt.BracketedSequence(input), with_readout=False).x
output = model(output_gpt)
+ targets = targets * (input.unsqueeze(-1) == maze.v_empty)
+ output = output * (input.unsqueeze(-1) == maze.v_empty)
+ # loss = (output.softmax(-1) - targets).abs().max(-1).values.mean()
loss = (
- -(output.log_softmax(-1) * targets).sum(-1).mean()
- + targets.xlogy(targets).sum(-1).mean()
+ -(output.log_softmax(-1) * targets).sum()
+ / (input == maze.v_empty).sum()
)
acc_test_loss += loss.item() * input.size(0)
nb_test_samples += input.size(0)
f"diff_ce {n_epoch} train {acc_train_loss/nb_train_samples} test {acc_test_loss/nb_test_samples}"
)
+ # -------------------
+ input = task.test_input[:32, : task.height * task.width]
+ targets = task.test_policies[:32]
+ output_gpt = gpt(mygpt.BracketedSequence(input), with_readout=False).x
+ output = model(output_gpt)
+ # losses = (-output.log_softmax(-1) * targets + targets.xlogy(targets)).sum(-1)
+ # losses = losses * (input == maze.v_empty)
+ # losses = losses / losses.max()
+ # losses = (output.softmax(-1) - targets).abs().max(-1).values
+ # losses = (losses >= 0.05).float()
+ losses = (
+ (F.one_hot(output.argmax(-1), num_classes=4) * targets).sum(-1) == 0
+ ).float()
+ losses = losses.reshape(-1, args.maze_height, args.maze_width)
+ input = input.reshape(-1, args.maze_height, args.maze_width)
+ maze.save_image(
+ os.path.join(args.result_dir, f"oneshot_{n_epoch:04d}.png"),
+ mazes=input,
+ score_paths=losses,
+ )
+ # -------------------
+
gpt.train(t)
_, predicted_paths = self.seq2map(result)
maze.save_image(
os.path.join(args.result_dir, f"result_{n_epoch:04d}.png"),
- mazes,
- paths,
- predicted_paths,
- maze.path_correctness(mazes, predicted_paths),
+ mazes=mazes,
+ target_paths=paths,
+ predicted_paths=predicted_paths,
+ path_correct=maze.path_correctness(mazes, predicted_paths),
)
model.train(t)