promote metric0.3
Browse files- bridge2vec.py +23 -2
- model.safetensors +1 -1
- results/eval.json +17 -17
- results/map.png +2 -2
- results/train.json +11 -7
bridge2vec.py
CHANGED
|
@@ -242,6 +242,17 @@ def schedule(step, warmup, total):
|
|
| 242 |
return 0.5 * (1 + math.cos(math.pi * (step - warmup) / max(1, total - warmup)))
|
| 243 |
|
| 244 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
def push_result(repo, name, result, revision=None):
|
| 246 |
"""Upload a result as results/<name>.json in the model repo."""
|
| 247 |
HfApi().upload_file(
|
|
@@ -277,12 +288,14 @@ def train(args):
|
|
| 277 |
batch = torch.randint(len(cards), (args.batch_size,), device=device)
|
| 278 |
hands, tricks = permute_suits(cards[batch], dd[batch])
|
| 279 |
with torch.autocast(device, dtype=torch.bfloat16, enabled=device == "cuda"):
|
| 280 |
-
|
| 281 |
expected = torch.stack([tricks.roll(-d, 2) for d in range(4)], 1).float()
|
| 282 |
table_loss = functional.cross_entropy(logits.float().flatten(0, 2), tricks.flatten())
|
| 283 |
value_loss = functional.mse_loss(value.float(), expected)
|
| 284 |
shape_loss = functional.mse_loss(shape.float(), profile(hands) / scale)
|
| 285 |
-
|
|
|
|
|
|
|
| 286 |
optimizer.zero_grad(set_to_none=True)
|
| 287 |
loss.backward()
|
| 288 |
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
|
|
@@ -295,6 +308,7 @@ def train(args):
|
|
| 295 |
"table": round(table_loss.item(), 4),
|
| 296 |
"value": round(value_loss.item(), 4),
|
| 297 |
"shape": round(shape_loss.item(), 4),
|
|
|
|
| 298 |
"exact": round((logits.argmax(-1) == tricks).float().mean().item(), 3),
|
| 299 |
"minutes": round((time.time() - start) / 60, 1),
|
| 300 |
}
|
|
@@ -331,6 +345,9 @@ def train(args):
|
|
| 331 |
"learning_rate": args.lr,
|
| 332 |
"value_weight": args.value_weight,
|
| 333 |
"aux_weight": args.aux_weight,
|
|
|
|
|
|
|
|
|
|
| 334 |
"dim": args.dim,
|
| 335 |
"depth": args.depth,
|
| 336 |
"embed_dim": args.embed_dim,
|
|
@@ -560,6 +577,10 @@ def main():
|
|
| 560 |
train_parser.add_argument("--warmup", type=int, default=1000)
|
| 561 |
train_parser.add_argument("--value-weight", type=float, default=0.1)
|
| 562 |
train_parser.add_argument("--aux-weight", type=float, default=0.1)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
train_parser.add_argument("--dim", type=int, default=256)
|
| 564 |
train_parser.add_argument("--depth", type=int, default=4)
|
| 565 |
train_parser.add_argument("--embed-dim", type=int, default=128)
|
|
|
|
| 242 |
return 0.5 * (1 + math.cos(math.pi * (step - warmup) / max(1, total - warmup)))
|
| 243 |
|
| 244 |
|
| 245 |
+
def metric_loss(z, value, temperature, target_temperature):
|
| 246 |
+
"""Make hands that play alike close: soft targets from the distance between expected tables."""
|
| 247 |
+
z = z.float().flatten(0, 1)
|
| 248 |
+
value = value.detach().float().flatten(0, 1).flatten(1)
|
| 249 |
+
self_pair = torch.eye(len(z), dtype=torch.bool, device=z.device)
|
| 250 |
+
distance = torch.cdist(value, value, p=1) / value.shape[1]
|
| 251 |
+
target = (-distance / target_temperature).masked_fill(self_pair, -1e9).softmax(1)
|
| 252 |
+
logits = (z @ z.T / temperature).masked_fill(self_pair, -1e9)
|
| 253 |
+
return -(target * logits.log_softmax(1)).sum(1).mean()
|
| 254 |
+
|
| 255 |
+
|
| 256 |
def push_result(repo, name, result, revision=None):
|
| 257 |
"""Upload a result as results/<name>.json in the model repo."""
|
| 258 |
HfApi().upload_file(
|
|
|
|
| 288 |
batch = torch.randint(len(cards), (args.batch_size,), device=device)
|
| 289 |
hands, tricks = permute_suits(cards[batch], dd[batch])
|
| 290 |
with torch.autocast(device, dtype=torch.bfloat16, enabled=device == "cuda"):
|
| 291 |
+
z, logits, value, shape = model(hands)
|
| 292 |
expected = torch.stack([tricks.roll(-d, 2) for d in range(4)], 1).float()
|
| 293 |
table_loss = functional.cross_entropy(logits.float().flatten(0, 2), tricks.flatten())
|
| 294 |
value_loss = functional.mse_loss(value.float(), expected)
|
| 295 |
shape_loss = functional.mse_loss(shape.float(), profile(hands) / scale)
|
| 296 |
+
metric = metric_loss(z, value, args.temperature, args.target_temperature)
|
| 297 |
+
loss = (table_loss + args.value_weight * value_loss + args.aux_weight * shape_loss
|
| 298 |
+
+ args.metric_weight * metric)
|
| 299 |
optimizer.zero_grad(set_to_none=True)
|
| 300 |
loss.backward()
|
| 301 |
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
|
|
|
|
| 308 |
"table": round(table_loss.item(), 4),
|
| 309 |
"value": round(value_loss.item(), 4),
|
| 310 |
"shape": round(shape_loss.item(), 4),
|
| 311 |
+
"metric": round(metric.item(), 4),
|
| 312 |
"exact": round((logits.argmax(-1) == tricks).float().mean().item(), 3),
|
| 313 |
"minutes": round((time.time() - start) / 60, 1),
|
| 314 |
}
|
|
|
|
| 345 |
"learning_rate": args.lr,
|
| 346 |
"value_weight": args.value_weight,
|
| 347 |
"aux_weight": args.aux_weight,
|
| 348 |
+
"metric_weight": args.metric_weight,
|
| 349 |
+
"temperature": args.temperature,
|
| 350 |
+
"target_temperature": args.target_temperature,
|
| 351 |
"dim": args.dim,
|
| 352 |
"depth": args.depth,
|
| 353 |
"embed_dim": args.embed_dim,
|
|
|
|
| 577 |
train_parser.add_argument("--warmup", type=int, default=1000)
|
| 578 |
train_parser.add_argument("--value-weight", type=float, default=0.1)
|
| 579 |
train_parser.add_argument("--aux-weight", type=float, default=0.1)
|
| 580 |
+
train_parser.add_argument("--metric-weight", type=float, default=0.0,
|
| 581 |
+
help="weight of the loss that ties embedding to table distance")
|
| 582 |
+
train_parser.add_argument("--temperature", type=float, default=0.1)
|
| 583 |
+
train_parser.add_argument("--target-temperature", type=float, default=0.1)
|
| 584 |
train_parser.add_argument("--dim", type=int, default=256)
|
| 585 |
train_parser.add_argument("--depth", type=int, default=4)
|
| 586 |
train_parser.add_argument("--embed-dim", type=int, default=128)
|
model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 19656236
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c788114fe205ccd973888f9530318853b73360f3f9443bd53ae176972702b3ea
|
| 3 |
size 19656236
|
results/eval.json
CHANGED
|
@@ -2,36 +2,36 @@
|
|
| 2 |
"model": "jgalego/bridge2vec",
|
| 3 |
"tables": {
|
| 4 |
"deals": 32000,
|
| 5 |
-
"mae": 0.
|
| 6 |
-
"exact": 0.
|
| 7 |
-
"within_one": 0.
|
| 8 |
-
"table_exact": 0.
|
| 9 |
"mae_by_strain": {
|
| 10 |
-
"S": 0.
|
| 11 |
-
"H": 0.
|
| 12 |
-
"D": 0.
|
| 13 |
-
"C": 0.
|
| 14 |
-
"N": 0.
|
| 15 |
}
|
| 16 |
},
|
| 17 |
"hands": {
|
| 18 |
"hands": 1000,
|
| 19 |
"deals_per_hand": 32.0,
|
| 20 |
-
"expected_mae": 0.
|
| 21 |
"constant_mae": 1.251,
|
| 22 |
"retrieval": {
|
| 23 |
-
"embedding": 0.
|
| 24 |
-
"hcp_shape": 0.
|
| 25 |
-
"random": 1.
|
| 26 |
},
|
| 27 |
"hard_pairs": {
|
| 28 |
"triplets": 98216,
|
| 29 |
-
"accuracy": 0.
|
| 30 |
}
|
| 31 |
},
|
| 32 |
"deal_retrieval": {
|
| 33 |
-
"embedding": 1.
|
| 34 |
-
"hcp_shape": 1.
|
| 35 |
-
"random": 3.
|
| 36 |
}
|
| 37 |
}
|
|
|
|
| 2 |
"model": "jgalego/bridge2vec",
|
| 3 |
"tables": {
|
| 4 |
"deals": 32000,
|
| 5 |
+
"mae": 0.398,
|
| 6 |
+
"exact": 0.628,
|
| 7 |
+
"within_one": 0.98,
|
| 8 |
+
"table_exact": 0.012,
|
| 9 |
"mae_by_strain": {
|
| 10 |
+
"S": 0.368,
|
| 11 |
+
"H": 0.371,
|
| 12 |
+
"D": 0.369,
|
| 13 |
+
"C": 0.368,
|
| 14 |
+
"N": 0.513
|
| 15 |
}
|
| 16 |
},
|
| 17 |
"hands": {
|
| 18 |
"hands": 1000,
|
| 19 |
"deals_per_hand": 32.0,
|
| 20 |
+
"expected_mae": 0.346,
|
| 21 |
"constant_mae": 1.251,
|
| 22 |
"retrieval": {
|
| 23 |
+
"embedding": 0.55,
|
| 24 |
+
"hcp_shape": 0.616,
|
| 25 |
+
"random": 1.781
|
| 26 |
},
|
| 27 |
"hard_pairs": {
|
| 28 |
"triplets": 98216,
|
| 29 |
+
"accuracy": 0.901
|
| 30 |
}
|
| 31 |
},
|
| 32 |
"deal_retrieval": {
|
| 33 |
+
"embedding": 1.277,
|
| 34 |
+
"hcp_shape": 1.412,
|
| 35 |
+
"random": 3.135
|
| 36 |
}
|
| 37 |
}
|
results/map.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
results/train.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
{
|
| 2 |
"step": 49999,
|
| 3 |
-
"loss":
|
| 4 |
-
"table": 0.
|
| 5 |
-
"value": 5.
|
| 6 |
-
"shape": 0.
|
| 7 |
-
"
|
| 8 |
-
"
|
|
|
|
| 9 |
"data": "jgalego/bridge2vec-deals",
|
| 10 |
"deals": 200000,
|
| 11 |
"steps": 50000,
|
|
@@ -13,11 +14,14 @@
|
|
| 13 |
"learning_rate": 0.0003,
|
| 14 |
"value_weight": 0.1,
|
| 15 |
"aux_weight": 0.1,
|
|
|
|
|
|
|
|
|
|
| 16 |
"dim": 256,
|
| 17 |
"depth": 4,
|
| 18 |
"embed_dim": 128,
|
| 19 |
"hidden": 1024,
|
| 20 |
"parameters": 4912479,
|
| 21 |
-
"runtime_s":
|
| 22 |
"device": "NVIDIA A10G"
|
| 23 |
}
|
|
|
|
| 1 |
{
|
| 2 |
"step": 49999,
|
| 3 |
+
"loss": 2.7616,
|
| 4 |
+
"table": 0.8088,
|
| 5 |
+
"value": 5.3021,
|
| 6 |
+
"shape": 0.0011,
|
| 7 |
+
"metric": 4.7414,
|
| 8 |
+
"exact": 0.648,
|
| 9 |
+
"minutes": 117.4,
|
| 10 |
"data": "jgalego/bridge2vec-deals",
|
| 11 |
"deals": 200000,
|
| 12 |
"steps": 50000,
|
|
|
|
| 14 |
"learning_rate": 0.0003,
|
| 15 |
"value_weight": 0.1,
|
| 16 |
"aux_weight": 0.1,
|
| 17 |
+
"metric_weight": 0.3,
|
| 18 |
+
"temperature": 0.1,
|
| 19 |
+
"target_temperature": 0.1,
|
| 20 |
"dim": 256,
|
| 21 |
"depth": 4,
|
| 22 |
"embed_dim": 128,
|
| 23 |
"hidden": 1024,
|
| 24 |
"parameters": 4912479,
|
| 25 |
+
"runtime_s": 7050,
|
| 26 |
"device": "NVIDIA A10G"
|
| 27 |
}
|