######################################################################
-def nb_rank_error(output, targets):
- output = output.reshape(-1, output.size(-1))
- targets = targets.reshape(-1, targets.size(-1))
- i = outputs.argmax(1)
- # out=input.gather out[i][j]=input[i][index[i][j]]
- # u[k]=targets[k][i[k]]
- return output[targets.argmax(1)]
-
-
def one_shot(gpt, task):
t = gpt.training
gpt.eval()
- model = nn.Linear(args.dim_model, 4).to(device)
+ model = nn.Sequential(
+ nn.Linear(args.dim_model, args.dim_model),
+ nn.ReLU(),
+ nn.Linear(args.dim_model, 4),
+ ).to(device)
+
+ print(f"{args.nb_epochs=}")
for n_epoch in range(args.nb_epochs):
- optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
+ print(f"{n_epoch=}")
+ learning_rate = learning_rate_schedule[n_epoch]
+ optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
acc_train_loss, nb_train_samples = 0, 0
for input, targets in task.policy_batches(split="train"):
output_gpt = gpt(mygpt.BracketedSequence(input), with_readout=False).x
output = model(output_gpt)
- loss = -(output.log_softmax(-1) * targets).sum(-1).mean()
+ loss = (
+ -(output.log_softmax(-1) * targets).sum(-1).mean()
+ + targets.xlogy(targets).sum(-1).mean()
+ )
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)
- loss = -(output.log_softmax(-1) * targets).sum(-1).mean()
+ loss = (
+ -(output.log_softmax(-1) * targets).sum(-1).mean()
+ + targets.xlogy(targets).sum(-1).mean()
+ )
acc_test_loss += loss.item() * input.size(0)
nb_test_samples += input.size(0)
- print(
- f"{n_epoch=} {acc_train_loss/nb_train_samples=} {acc_test_loss/nb_test_samples=}"
+ log_string(
+ f"diff_ce {n_epoch} train {acc_train_loss/nb_train_samples} test {acc_test_loss/nb_test_samples}"
+ )
+
+ # -------------------
+ input, targets = next(task.policy_batches(split="test"))
+ 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 / losses.max()
+ print(f"{input.size()=} {losses.size()=} {losses.min()=} {losses.max()=}")
+ losses = losses * (input == 0)
+ 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)