1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
|
import numpy as np
import numba
import umap.distances as dist
from umap.utils import tau_rand_int
from tqdm.auto import tqdm
@numba.njit()
def clip(val):
"""Standard clamping of a value into a fixed range (in this case -4.0 to
4.0)
Parameters
----------
val: float
The value to be clamped.
Returns
-------
The clamped value, now fixed to be in the range -4.0 to 4.0.
"""
if val > 4.0:
return 4.0
elif val < -4.0:
return -4.0
else:
return val
@numba.njit(
"f4(f4[::1],f4[::1])",
fastmath=True,
cache=True,
locals={
"result": numba.types.float32,
"diff": numba.types.float32,
"dim": numba.types.intp,
},
)
def rdist(x, y):
"""Reduced Euclidean distance.
Parameters
----------
x: array of shape (embedding_dim,)
y: array of shape (embedding_dim,)
Returns
-------
The squared euclidean distance between x and y
"""
result = 0.0
dim = x.shape[0]
for i in range(dim):
diff = x[i] - y[i]
result += diff * diff
return result
def _optimize_layout_euclidean_single_epoch(
head_embedding,
tail_embedding,
head,
tail,
n_vertices,
epochs_per_sample,
a,
b,
rng_state,
gamma,
dim,
move_other,
alpha,
epochs_per_negative_sample,
epoch_of_next_negative_sample,
epoch_of_next_sample,
n,
densmap_flag,
dens_phi_sum,
dens_re_sum,
dens_re_cov,
dens_re_std,
dens_re_mean,
dens_lambda,
dens_R,
dens_mu,
dens_mu_tot,
):
for i in numba.prange(epochs_per_sample.shape[0]):
if epoch_of_next_sample[i] <= n:
j = head[i]
k = tail[i]
current = head_embedding[j]
other = tail_embedding[k]
dist_squared = rdist(current, other)
if densmap_flag:
phi = 1.0 / (1.0 + a * pow(dist_squared, b))
dphi_term = (
a * b * pow(dist_squared, b - 1) / (1.0 + a * pow(dist_squared, b))
)
q_jk = phi / dens_phi_sum[k]
q_kj = phi / dens_phi_sum[j]
drk = q_jk * (
(1.0 - b * (1 - phi)) / np.exp(dens_re_sum[k]) + dphi_term
)
drj = q_kj * (
(1.0 - b * (1 - phi)) / np.exp(dens_re_sum[j]) + dphi_term
)
re_std_sq = dens_re_std * dens_re_std
weight_k = (
dens_R[k]
- dens_re_cov * (dens_re_sum[k] - dens_re_mean) / re_std_sq
)
weight_j = (
dens_R[j]
- dens_re_cov * (dens_re_sum[j] - dens_re_mean) / re_std_sq
)
grad_cor_coeff = (
dens_lambda
* dens_mu_tot
* (weight_k * drk + weight_j * drj)
/ (dens_mu[i] * dens_re_std)
/ n_vertices
)
if dist_squared > 0.0:
grad_coeff = -2.0 * a * b * pow(dist_squared, b - 1.0)
grad_coeff /= a * pow(dist_squared, b) + 1.0
else:
grad_coeff = 0.0
for d in range(dim):
grad_d = clip(grad_coeff * (current[d] - other[d]))
if densmap_flag:
# FIXME: grad_cor_coeff might be referenced before assignment
grad_d += clip(2 * grad_cor_coeff * (current[d] - other[d]))
current[d] += grad_d * alpha
if move_other:
other[d] += -grad_d * alpha
epoch_of_next_sample[i] += epochs_per_sample[i]
n_neg_samples = int(
(n - epoch_of_next_negative_sample[i]) / epochs_per_negative_sample[i]
)
for p in range(n_neg_samples):
k = tau_rand_int(rng_state) % n_vertices
other = tail_embedding[k]
dist_squared = rdist(current, other)
if dist_squared > 0.0:
grad_coeff = 2.0 * gamma * b
grad_coeff /= (0.001 + dist_squared) * (
a * pow(dist_squared, b) + 1
)
elif j == k:
continue
else:
grad_coeff = 0.0
for d in range(dim):
if grad_coeff > 0.0:
grad_d = clip(grad_coeff * (current[d] - other[d]))
else:
grad_d = 4.0
current[d] += grad_d * alpha
epoch_of_next_negative_sample[i] += (
n_neg_samples * epochs_per_negative_sample[i]
)
def _optimize_layout_euclidean_densmap_epoch_init(
head_embedding,
tail_embedding,
head,
tail,
a,
b,
re_sum,
phi_sum,
):
re_sum.fill(0)
phi_sum.fill(0)
for i in numba.prange(head.size):
j = head[i]
k = tail[i]
current = head_embedding[j]
other = tail_embedding[k]
dist_squared = rdist(current, other)
phi = 1.0 / (1.0 + a * pow(dist_squared, b))
re_sum[j] += phi * dist_squared
re_sum[k] += phi * dist_squared
phi_sum[j] += phi
phi_sum[k] += phi
epsilon = 1e-8
for i in range(re_sum.size):
re_sum[i] = np.log(epsilon + (re_sum[i] / phi_sum[i]))
def optimize_layout_euclidean(
head_embedding,
tail_embedding,
head,
tail,
n_epochs,
n_vertices,
epochs_per_sample,
a,
b,
rng_state,
gamma=1.0,
initial_alpha=1.0,
negative_sample_rate=5.0,
parallel=False,
verbose=False,
densmap=False,
densmap_kwds=None,
tqdm_kwds=None,
move_other=False,
):
"""Improve an embedding using stochastic gradient descent to minimize the
fuzzy set cross entropy between the 1-skeletons of the high dimensional
and low dimensional fuzzy simplicial sets. In practice this is done by
sampling edges based on their membership strength (with the (1-p) terms
coming from negative sampling similar to word2vec).
Parameters
----------
head_embedding: array of shape (n_samples, n_components)
The initial embedding to be improved by SGD.
tail_embedding: array of shape (source_samples, n_components)
The reference embedding of embedded points. If not embedding new
previously unseen points with respect to an existing embedding this
is simply the head_embedding (again); otherwise it provides the
existing embedding to embed with respect to.
head: array of shape (n_1_simplices)
The indices of the heads of 1-simplices with non-zero membership.
tail: array of shape (n_1_simplices)
The indices of the tails of 1-simplices with non-zero membership.
n_epochs: int
The number of training epochs to use in optimization.
n_vertices: int
The number of vertices (0-simplices) in the dataset.
epochs_per_sample: array of shape (n_1_simplices)
A float value of the number of epochs per 1-simplex. 1-simplices with
weaker membership strength will have more epochs between being sampled.
a: float
Parameter of differentiable approximation of right adjoint functor
b: float
Parameter of differentiable approximation of right adjoint functor
rng_state: array of int64, shape (3,)
The internal state of the rng
gamma: float (optional, default 1.0)
Weight to apply to negative samples.
initial_alpha: float (optional, default 1.0)
Initial learning rate for the SGD.
negative_sample_rate: int (optional, default 5)
Number of negative samples to use per positive sample.
parallel: bool (optional, default False)
Whether to run the computation using numba parallel.
Running in parallel is non-deterministic, and is not used
if a random seed has been set, to ensure reproducibility.
verbose: bool (optional, default False)
Whether to report information on the current progress of the algorithm.
densmap: bool (optional, default False)
Whether to use the density-augmented densMAP objective
densmap_kwds: dict (optional, default None)
Auxiliary data for densMAP
tqdm_kwds: dict (optional, default None)
Keyword arguments for tqdm progress bar.
move_other: bool (optional, default False)
Whether to adjust tail_embedding alongside head_embedding
Returns
-------
embedding: array of shape (n_samples, n_components)
The optimized embedding.
"""
dim = head_embedding.shape[1]
alpha = initial_alpha
epochs_per_negative_sample = epochs_per_sample / negative_sample_rate
epoch_of_next_negative_sample = epochs_per_negative_sample.copy()
epoch_of_next_sample = epochs_per_sample.copy()
optimize_fn = numba.njit(
_optimize_layout_euclidean_single_epoch, fastmath=True, parallel=parallel
)
if densmap_kwds is None:
densmap_kwds = {}
if tqdm_kwds is None:
tqdm_kwds = {}
if densmap:
dens_init_fn = numba.njit(
_optimize_layout_euclidean_densmap_epoch_init,
fastmath=True,
parallel=parallel,
)
dens_mu_tot = np.sum(densmap_kwds["mu_sum"]) / 2
dens_lambda = densmap_kwds["lambda"]
dens_R = densmap_kwds["R"]
dens_mu = densmap_kwds["mu"]
dens_phi_sum = np.zeros(n_vertices, dtype=np.float32)
dens_re_sum = np.zeros(n_vertices, dtype=np.float32)
dens_var_shift = densmap_kwds["var_shift"]
else:
dens_mu_tot = 0
dens_lambda = 0
dens_R = np.zeros(1, dtype=np.float32)
dens_mu = np.zeros(1, dtype=np.float32)
dens_phi_sum = np.zeros(1, dtype=np.float32)
dens_re_sum = np.zeros(1, dtype=np.float32)
if "disable" not in tqdm_kwds:
tqdm_kwds["disable"] = not verbose
for n in tqdm(range(n_epochs), **tqdm_kwds):
densmap_flag = (
densmap
and (densmap_kwds["lambda"] > 0)
and (((n + 1) / float(n_epochs)) > (1 - densmap_kwds["frac"]))
)
if densmap_flag:
# FIXME: dens_init_fn might be referenced before assignment
dens_init_fn(
head_embedding,
tail_embedding,
head,
tail,
a,
b,
dens_re_sum,
dens_phi_sum,
)
# FIXME: dens_var_shift might be referenced before assignment
dens_re_std = np.sqrt(np.var(dens_re_sum) + dens_var_shift)
dens_re_mean = np.mean(dens_re_sum)
dens_re_cov = np.dot(dens_re_sum, dens_R) / (n_vertices - 1)
else:
dens_re_std = 0
dens_re_mean = 0
dens_re_cov = 0
optimize_fn(
head_embedding,
tail_embedding,
head,
tail,
n_vertices,
epochs_per_sample,
a,
b,
rng_state,
gamma,
dim,
move_other,
alpha,
epochs_per_negative_sample,
epoch_of_next_negative_sample,
epoch_of_next_sample,
n,
densmap_flag,
dens_phi_sum,
dens_re_sum,
dens_re_cov,
dens_re_std,
dens_re_mean,
dens_lambda,
dens_R,
dens_mu,
dens_mu_tot,
)
alpha = initial_alpha * (1.0 - (float(n) / float(n_epochs)))
return head_embedding
def _optimize_layout_generic_single_epoch(
epochs_per_sample,
epoch_of_next_sample,
head,
tail,
head_embedding,
tail_embedding,
output_metric,
output_metric_kwds,
dim,
alpha,
move_other,
n,
epoch_of_next_negative_sample,
epochs_per_negative_sample,
rng_state,
n_vertices,
a,
b,
gamma,
):
for i in range(epochs_per_sample.shape[0]):
if epoch_of_next_sample[i] <= n:
j = head[i]
k = tail[i]
current = head_embedding[j]
other = tail_embedding[k]
dist_output, grad_dist_output = output_metric(
current, other, *output_metric_kwds
)
_, rev_grad_dist_output = output_metric(other, current, *output_metric_kwds)
if dist_output > 0.0:
w_l = pow((1 + a * pow(dist_output, 2 * b)), -1)
else:
w_l = 1.0
grad_coeff = 2 * b * (w_l - 1) / (dist_output + 1e-6)
for d in range(dim):
grad_d = clip(grad_coeff * grad_dist_output[d])
current[d] += grad_d * alpha
if move_other:
grad_d = clip(grad_coeff * rev_grad_dist_output[d])
other[d] += grad_d * alpha
epoch_of_next_sample[i] += epochs_per_sample[i]
n_neg_samples = int(
(n - epoch_of_next_negative_sample[i]) / epochs_per_negative_sample[i]
)
for p in range(n_neg_samples):
k = tau_rand_int(rng_state) % n_vertices
other = tail_embedding[k]
dist_output, grad_dist_output = output_metric(
current, other, *output_metric_kwds
)
if dist_output > 0.0:
w_l = pow((1 + a * pow(dist_output, 2 * b)), -1)
elif j == k:
continue
else:
w_l = 1.0
grad_coeff = gamma * 2 * b * w_l / (dist_output + 1e-6)
for d in range(dim):
grad_d = clip(grad_coeff * grad_dist_output[d])
current[d] += grad_d * alpha
epoch_of_next_negative_sample[i] += (
n_neg_samples * epochs_per_negative_sample[i]
)
return epoch_of_next_sample, epoch_of_next_negative_sample
def optimize_layout_generic(
head_embedding,
tail_embedding,
head,
tail,
n_epochs,
n_vertices,
epochs_per_sample,
a,
b,
rng_state,
gamma=1.0,
initial_alpha=1.0,
negative_sample_rate=5.0,
output_metric=dist.euclidean,
output_metric_kwds=(),
verbose=False,
tqdm_kwds=None,
move_other=False,
):
"""Improve an embedding using stochastic gradient descent to minimize the
fuzzy set cross entropy between the 1-skeletons of the high dimensional
and low dimensional fuzzy simplicial sets. In practice this is done by
sampling edges based on their membership strength (with the (1-p) terms
coming from negative sampling similar to word2vec).
Parameters
----------
head_embedding: array of shape (n_samples, n_components)
The initial embedding to be improved by SGD.
tail_embedding: array of shape (source_samples, n_components)
The reference embedding of embedded points. If not embedding new
previously unseen points with respect to an existing embedding this
is simply the head_embedding (again); otherwise it provides the
existing embedding to embed with respect to.
head: array of shape (n_1_simplices)
The indices of the heads of 1-simplices with non-zero membership.
tail: array of shape (n_1_simplices)
The indices of the tails of 1-simplices with non-zero membership.
n_epochs: int
The number of training epochs to use in optimization.
n_vertices: int
The number of vertices (0-simplices) in the dataset.
epochs_per_sample: array of shape (n_1_simplices)
A float value of the number of epochs per 1-simplex. 1-simplices with
weaker membership strength will have more epochs between being sampled.
a: float
Parameter of differentiable approximation of right adjoint functor
b: float
Parameter of differentiable approximation of right adjoint functor
rng_state: array of int64, shape (3,)
The internal state of the rng
gamma: float (optional, default 1.0)
Weight to apply to negative samples.
initial_alpha: float (optional, default 1.0)
Initial learning rate for the SGD.
negative_sample_rate: int (optional, default 5)
Number of negative samples to use per positive sample.
verbose: bool (optional, default False)
Whether to report information on the current progress of the algorithm.
tqdm_kwds: dict (optional, default None)
Keyword arguments for tqdm progress bar.
move_other: bool (optional, default False)
Whether to adjust tail_embedding alongside head_embedding
Returns
-------
embedding: array of shape (n_samples, n_components)
The optimized embedding.
"""
dim = head_embedding.shape[1]
alpha = initial_alpha
epochs_per_negative_sample = epochs_per_sample / negative_sample_rate
epoch_of_next_negative_sample = epochs_per_negative_sample.copy()
epoch_of_next_sample = epochs_per_sample.copy()
optimize_fn = numba.njit(
_optimize_layout_generic_single_epoch,
fastmath=True,
)
if tqdm_kwds is None:
tqdm_kwds = {}
if "disable" not in tqdm_kwds:
tqdm_kwds["disable"] = not verbose
for n in tqdm(range(n_epochs), **tqdm_kwds):
optimize_fn(
epochs_per_sample,
epoch_of_next_sample,
head,
tail,
head_embedding,
tail_embedding,
output_metric,
output_metric_kwds,
dim,
alpha,
move_other,
n,
epoch_of_next_negative_sample,
epochs_per_negative_sample,
rng_state,
n_vertices,
a,
b,
gamma,
)
alpha = initial_alpha * (1.0 - (float(n) / float(n_epochs)))
return head_embedding
def _optimize_layout_inverse_single_epoch(
epochs_per_sample,
epoch_of_next_sample,
head,
tail,
head_embedding,
tail_embedding,
output_metric,
output_metric_kwds,
weight,
sigmas,
dim,
alpha,
move_other,
n,
epoch_of_next_negative_sample,
epochs_per_negative_sample,
rng_state,
n_vertices,
rhos,
gamma,
):
for i in range(epochs_per_sample.shape[0]):
if epoch_of_next_sample[i] <= n:
j = head[i]
k = tail[i]
current = head_embedding[j]
other = tail_embedding[k]
dist_output, grad_dist_output = output_metric(
current, other, *output_metric_kwds
)
w_l = weight[i]
grad_coeff = -(1 / (w_l * sigmas[k] + 1e-6))
for d in range(dim):
grad_d = clip(grad_coeff * grad_dist_output[d])
current[d] += grad_d * alpha
if move_other:
other[d] += -grad_d * alpha
epoch_of_next_sample[i] += epochs_per_sample[i]
n_neg_samples = int(
(n - epoch_of_next_negative_sample[i]) / epochs_per_negative_sample[i]
)
for p in range(n_neg_samples):
k = tau_rand_int(rng_state) % n_vertices
other = tail_embedding[k]
dist_output, grad_dist_output = output_metric(
current, other, *output_metric_kwds
)
# w_l = 0.0 # for negative samples, the edge does not exist
w_h = np.exp(-max(dist_output - rhos[k], 1e-6) / (sigmas[k] + 1e-6))
grad_coeff = -gamma * ((0 - w_h) / ((1 - w_h) * sigmas[k] + 1e-6))
for d in range(dim):
grad_d = clip(grad_coeff * grad_dist_output[d])
current[d] += grad_d * alpha
epoch_of_next_negative_sample[i] += (
n_neg_samples * epochs_per_negative_sample[i]
)
def optimize_layout_inverse(
head_embedding,
tail_embedding,
head,
tail,
weight,
sigmas,
rhos,
n_epochs,
n_vertices,
epochs_per_sample,
a,
b,
rng_state,
gamma=1.0,
initial_alpha=1.0,
negative_sample_rate=5.0,
output_metric=dist.euclidean,
output_metric_kwds=(),
verbose=False,
tqdm_kwds=None,
move_other=False,
):
"""Improve an embedding using stochastic gradient descent to minimize the
fuzzy set cross entropy between the 1-skeletons of the high dimensional
and low dimensional fuzzy simplicial sets. In practice this is done by
sampling edges based on their membership strength (with the (1-p) terms
coming from negative sampling similar to word2vec).
Parameters
----------
head_embedding: array of shape (n_samples, n_components)
The initial embedding to be improved by SGD.
tail_embedding: array of shape (source_samples, n_components)
The reference embedding of embedded points. If not embedding new
previously unseen points with respect to an existing embedding this
is simply the head_embedding (again); otherwise it provides the
existing embedding to embed with respect to.
head: array of shape (n_1_simplices)
The indices of the heads of 1-simplices with non-zero membership.
tail: array of shape (n_1_simplices)
The indices of the tails of 1-simplices with non-zero membership.
weight: array of shape (n_1_simplices)
The membership weights of the 1-simplices.
sigmas:
rhos:
n_epochs: int
The number of training epochs to use in optimization.
n_vertices: int
The number of vertices (0-simplices) in the dataset.
epochs_per_sample: array of shape (n_1_simplices)
A float value of the number of epochs per 1-simplex. 1-simplices with
weaker membership strength will have more epochs between being sampled.
a: float
Parameter of differentiable approximation of right adjoint functor
b: float
Parameter of differentiable approximation of right adjoint functor
rng_state: array of int64, shape (3,)
The internal state of the rng
gamma: float (optional, default 1.0)
Weight to apply to negative samples.
initial_alpha: float (optional, default 1.0)
Initial learning rate for the SGD.
negative_sample_rate: int (optional, default 5)
Number of negative samples to use per positive sample.
verbose: bool (optional, default False)
Whether to report information on the current progress of the algorithm.
tqdm_kwds: dict (optional, default None)
Keyword arguments for tqdm progress bar.
move_other: bool (optional, default False)
Whether to adjust tail_embedding alongside head_embedding
Returns
-------
embedding: array of shape (n_samples, n_components)
The optimized embedding.
"""
dim = head_embedding.shape[1]
alpha = initial_alpha
epochs_per_negative_sample = epochs_per_sample / negative_sample_rate
epoch_of_next_negative_sample = epochs_per_negative_sample.copy()
epoch_of_next_sample = epochs_per_sample.copy()
optimize_fn = numba.njit(
_optimize_layout_inverse_single_epoch,
fastmath=True,
)
if tqdm_kwds is None:
tqdm_kwds = {}
if "disable" not in tqdm_kwds:
tqdm_kwds["disable"] = not verbose
for n in tqdm(range(n_epochs), **tqdm_kwds):
optimize_fn(
epochs_per_sample,
epoch_of_next_sample,
head,
tail,
head_embedding,
tail_embedding,
output_metric,
output_metric_kwds,
weight,
sigmas,
dim,
alpha,
move_other,
n,
epoch_of_next_negative_sample,
epochs_per_negative_sample,
rng_state,
n_vertices,
rhos,
gamma,
)
alpha = initial_alpha * (1.0 - (float(n) / float(n_epochs)))
return head_embedding
def _optimize_layout_aligned_euclidean_single_epoch(
head_embeddings,
tail_embeddings,
heads,
tails,
epochs_per_sample,
a,
b,
regularisation_weights,
relations,
rng_state,
gamma,
lambda_,
dim,
move_other,
alpha,
epochs_per_negative_sample,
epoch_of_next_negative_sample,
epoch_of_next_sample,
n,
):
n_embeddings = len(heads)
window_size = (relations.shape[1] - 1) // 2
max_n_edges = 0
for e_p_s in epochs_per_sample:
if e_p_s.shape[0] >= max_n_edges:
max_n_edges = e_p_s.shape[0]
embedding_order = np.arange(n_embeddings).astype(np.int32)
np.random.seed(abs(rng_state[0]))
np.random.shuffle(embedding_order)
for i in range(max_n_edges):
for m in embedding_order:
if i < epoch_of_next_sample[m].shape[0] and epoch_of_next_sample[m][i] <= n:
j = heads[m][i]
k = tails[m][i]
current = head_embeddings[m][j]
other = tail_embeddings[m][k]
dist_squared = rdist(current, other)
if dist_squared > 0.0:
grad_coeff = -2.0 * a * b * pow(dist_squared, b - 1.0)
grad_coeff /= a * pow(dist_squared, b) + 1.0
else:
grad_coeff = 0.0
for d in range(dim):
grad_d = clip(grad_coeff * (current[d] - other[d]))
for offset in range(-window_size, window_size):
neighbor_m = m + offset
if n_embeddings > neighbor_m >= 0 != offset:
identified_index = relations[m, offset + window_size, j]
if identified_index >= 0:
grad_d -= clip(
(lambda_ * np.exp(-(np.abs(offset) - 1)))
* regularisation_weights[m, offset + window_size, j]
* (
current[d]
- head_embeddings[neighbor_m][
identified_index, d
]
)
)
current[d] += clip(grad_d) * alpha
if move_other:
other_grad_d = clip(grad_coeff * (other[d] - current[d]))
for offset in range(-window_size, window_size):
neighbor_m = m + offset
if n_embeddings > neighbor_m >= 0 != offset:
identified_index = relations[m, offset + window_size, k]
if identified_index >= 0:
grad_d -= clip(
(lambda_ * np.exp(-(np.abs(offset) - 1)))
* regularisation_weights[
m, offset + window_size, k
]
* (
other[d]
- head_embeddings[neighbor_m][
identified_index, d
]
)
)
other[d] += clip(other_grad_d) * alpha
epoch_of_next_sample[m][i] += epochs_per_sample[m][i]
if epochs_per_negative_sample[m][i] > 0:
n_neg_samples = int(
(n - epoch_of_next_negative_sample[m][i])
/ epochs_per_negative_sample[m][i]
)
else:
n_neg_samples = 0
for p in range(n_neg_samples):
k = tau_rand_int(rng_state) % tail_embeddings[m].shape[0]
other = tail_embeddings[m][k]
dist_squared = rdist(current, other)
if dist_squared > 0.0:
grad_coeff = 2.0 * gamma * b
grad_coeff /= (0.001 + dist_squared) * (
a * pow(dist_squared, b) + 1
)
elif j == k:
continue
else:
grad_coeff = 0.0
for d in range(dim):
if grad_coeff > 0.0:
grad_d = clip(grad_coeff * (current[d] - other[d]))
else:
grad_d = 4.0
for offset in range(-window_size, window_size):
neighbor_m = m + offset
if n_embeddings > neighbor_m >= 0 != offset:
identified_index = relations[m, offset + window_size, j]
if identified_index >= 0:
grad_d -= clip(
(lambda_ * np.exp(-(np.abs(offset) - 1)))
* regularisation_weights[
m, offset + window_size, j
]
* (
current[d]
- head_embeddings[neighbor_m][
identified_index, d
]
)
)
current[d] += clip(grad_d) * alpha
epoch_of_next_negative_sample[m][i] += (
n_neg_samples * epochs_per_negative_sample[m][i]
)
def optimize_layout_aligned_euclidean(
head_embeddings,
tail_embeddings,
heads,
tails,
n_epochs,
epochs_per_sample,
regularisation_weights,
relations,
rng_state,
a=1.576943460405378,
b=0.8950608781227859,
gamma=1.0,
lambda_=5e-3,
initial_alpha=1.0,
negative_sample_rate=5.0,
parallel=True,
verbose=False,
tqdm_kwds=None,
move_other=False,
):
dim = head_embeddings[0].shape[1]
alpha = initial_alpha
epochs_per_negative_sample = numba.typed.List.empty_list(numba.types.float32[::1])
epoch_of_next_negative_sample = numba.typed.List.empty_list(
numba.types.float32[::1]
)
epoch_of_next_sample = numba.typed.List.empty_list(numba.types.float32[::1])
for m in range(len(heads)):
epochs_per_negative_sample.append(
epochs_per_sample[m].astype(np.float32) / negative_sample_rate
)
epoch_of_next_negative_sample.append(
epochs_per_negative_sample[m].astype(np.float32)
)
epoch_of_next_sample.append(epochs_per_sample[m].astype(np.float32))
optimize_fn = numba.njit(
_optimize_layout_aligned_euclidean_single_epoch,
fastmath=True,
parallel=parallel,
)
if tqdm_kwds is None:
tqdm_kwds = {}
if "disable" not in tqdm_kwds:
tqdm_kwds["disable"] = not verbose
for n in tqdm(range(n_epochs), **tqdm_kwds):
optimize_fn(
head_embeddings,
tail_embeddings,
heads,
tails,
epochs_per_sample,
a,
b,
regularisation_weights,
relations,
rng_state,
gamma,
lambda_,
dim,
move_other,
alpha,
epochs_per_negative_sample,
epoch_of_next_negative_sample,
epoch_of_next_sample,
n,
)
alpha = initial_alpha * (1.0 - (float(n) / float(n_epochs)))
return head_embeddings
|