RKM class
RKM(model_name, n_visible, n_hidden, k=1, lr=0.001, max_epochs=200000, energy_type='RKM', optimizer='SGD', regularization=False, l1_factor=0, l2_factor=0.001, g_v=0.5, g_h=0.5, batch_size=1, train_algo='vRDM', centering=False, average_data=None, model_beta=1, mytype=torch.float32, min_W=-10, max_W=10, offset=0.0, sampling='bernoulli', distribution='gaussian', layer_scaled=True)
dataclass
Bases: RBM
A class to represent a Restricted Kirchhoff Machine (RKM). It is inherited from the RBM class, so look at the RBM class for info about the attributes and methods. Also, refer to the paper https://arxiv.org/abs/2509.15842 for more details on the RKM.
Parameters:
-
energy_type
(str
, default:'RKM'
) –The type of energy function to use (default is 'RKM').
-
offset
(float
, default:0.0
) –Offset parameter for the energy function (default is 0.0).
-
sampling
(str
, default:'bernoulli'
) –Sampling method to use (default is 'bernoulli').
-
distribution
(str
, default:'gaussian'
) –Distribution to use for sampling (default is 'gaussian').
-
layer_scaled
(bool
, default:True
) –Whether to scale the layer by the number of units (default is True).
Adam_update(t, dEdW_data, dEdW_model, dEdv_bias_data, dEdv_bias_model, dEdh_bias_data, dEdh_bias_model)
Update the model parameters using Adam optimizer.
Parameters:
-
t
(int
) –Current time step.
-
dEdW_data
(Tensor
) –Gradient of the weights from data.
-
dEdW_model
(Tensor
) –Gradient of the weights from the model.
-
dEdv_bias_data
(Tensor
) –Gradient of the visible biases from data.
-
dEdv_bias_model
(Tensor
) –Gradient of the visible biases from the model.
-
dEdh_bias_data
(Tensor
) –Gradient of the hidden biases from data.
-
dEdh_bias_model
(Tensor
) –Gradient of the hidden biases from the model.
Source code in src/pyrkm/rkm.py
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 |
|
Bernoulli_h_to_v(h, beta)
Convert hidden units to visible units using Bernoulli sampling.
Parameters:
-
h
(Tensor
) –Hidden units.
-
beta
(float
) –Inverse temperature parameter.
Returns:
-
tuple of torch.Tensor
–Probabilities and sampled visible units.
Source code in src/pyrkm/rkm.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
|
Bernoulli_v_to_h(v, beta)
Convert visible units to hidden units using Bernoulli sampling.
Parameters:
-
v
(Tensor
) –Visible units.
-
beta
(float
) –Inverse temperature parameter.
Returns:
-
tuple of torch.Tensor
–Probabilities and sampled hidden units.
Source code in src/pyrkm/rkm.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
|
Deterministic_h_to_v(h, beta)
Deterministically convert hidden units to visible units.
Parameters:
-
h
(Tensor
) –Hidden units.
-
beta
(float
) –Inverse temperature parameter.
Returns:
-
tuple of torch.Tensor
–Deterministic visible units.
Source code in src/pyrkm/rkm.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
|
Deterministic_v_to_h(v, beta)
Deterministically convert visible units to hidden units.
Parameters:
-
v
(Tensor
) –Visible units.
-
beta
(float
) –Inverse temperature parameter.
Returns:
-
tuple of torch.Tensor
–Deterministic hidden units.
Source code in src/pyrkm/rkm.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
SGD_update(dEdW_data, dEdW_model, dEdv_bias_data, dEdv_bias_model, dEdh_bias_data, dEdh_bias_model)
Update the model parameters using SGD.
Parameters:
-
dEdW_data
(Tensor
) –Gradient of the weights from data.
-
dEdW_model
(Tensor
) –Gradient of the weights from the model.
-
dEdv_bias_data
(Tensor
) –Gradient of the visible biases from data.
-
dEdv_bias_model
(Tensor
) –Gradient of the visible biases from the model.
-
dEdh_bias_data
(Tensor
) –Gradient of the hidden biases from data.
-
dEdh_bias_model
(Tensor
) –Gradient of the hidden biases from the model.
Source code in src/pyrkm/rkm.py
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 |
|
after_step_keepup()
Perform operations after each training step.
Source code in src/pyrkm/rkm.py
584 585 586 587 588 |
|
av_power_backward(h)
Computes the average power dissipated by the RKM in the backward pass.
Parameters:
-
h
(Tensor
) –Hidden units, shape (N, n_h).
Returns:
-
Tensor
–Average power dissipated by the RKM.
Source code in src/pyrkm/rkm.py
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 |
|
av_power_forward(v)
Computes the average power dissipated by the RKM in the forward pass.
Parameters:
-
v
(Tensor
) –Visible units, shape (N, n_v).
Returns:
-
Tensor
–Average power dissipated by the RKM.
Source code in src/pyrkm/rkm.py
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
|
clip_bias()
Clip the biases to be within the specified range.
Source code in src/pyrkm/rkm.py
765 766 767 768 |
|
clip_weights()
Clip the weights to be within the specified range.
Source code in src/pyrkm/rkm.py
760 761 762 763 |
|
delta_eh(v)
Compute the change in energy for hidden units given visible units.
Parameters:
-
v
(Tensor
) –Visible units.
Returns:
-
Tensor
–Change in energy for hidden units.
Source code in src/pyrkm/rkm.py
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 |
|
delta_ev(h)
Compute the change in energy for visible units given hidden units.
Parameters:
-
h
(Tensor
) –Hidden units.
Returns:
-
Tensor
–Change in energy for visible units.
Source code in src/pyrkm/rkm.py
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 |
|
derivatives(v, h)
Compute the derivatives for the specified energy type.
Parameters:
-
v
(Tensor
) –Visible units.
-
h
(Tensor
) –Hidden units.
Returns:
-
tuple of torch.Tensor
–Gradients of the weights, visible biases, and hidden biases.
Source code in src/pyrkm/rkm.py
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 |
|
derivatives_hopfield(v, h)
Compute the derivatives for the Hopfield energy.
Parameters:
-
v
(Tensor
) –Visible units.
-
h
(Tensor
) –Hidden units.
Returns:
-
tuple of torch.Tensor
–Gradients of the weights, visible biases, and hidden biases.
Source code in src/pyrkm/rkm.py
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 |
|
forward(v, k, beta=None)
Perform a forward pass through the network.
Parameters:
-
v
(Tensor
) –Visible units.
-
k
(int
) –Number of Gibbs sampling steps.
-
beta
(float
, default:None
) –Inverse temperature parameter, by default None.
Returns:
-
Tensor
–Reconstructed visible units.
Source code in src/pyrkm/rkm.py
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 |
|
generate(n_samples, k, h_binarized=True, from_visible=True, beta=None)
Generate samples from the model.
Parameters:
-
n_samples
(int
) –Number of samples to generate.
-
k
(int
) –Number of Gibbs sampling steps.
-
h_binarized
(bool
, default:True
) –Whether to binarize hidden units, by default True.
-
from_visible
(bool
, default:True
) –Whether to generate from visible units, by default True.
-
beta
(float
, default:None
) –Inverse temperature parameter, by default None.
Returns:
-
ndarray
–Generated samples.
Source code in src/pyrkm/rkm.py
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 |
|
h_to_v(h, beta=None)
Convert hidden units to visible units.
Parameters:
-
h
(Tensor
) –Hidden units.
-
beta
(float
, default:None
) –Inverse temperature parameter, by default None.
Returns:
-
tuple of torch.Tensor
–Probabilities and sampled visible units.
Source code in src/pyrkm/rkm.py
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 |
|
plot_bias(t)
Plot the biases of the model.
Parameters:
-
t
(int
) –Current epoch.
Source code in src/pyrkm/rkm.py
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 |
|
plot_visible_bias(t)
Plot the visible biases of the model.
Parameters:
-
t
(int
) –Current epoch.
Source code in src/pyrkm/rkm.py
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 |
|
plot_weights(t)
Plot the weights of the model.
Parameters:
-
t
(int
) –Current epoch.
Source code in src/pyrkm/rkm.py
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 |
|
power_backward(h)
Computes the power dissipated by the RKM in the backward pass.
Parameters:
-
h
(Tensor
) –Hidden units, shape (N, n_h).
Returns:
-
Tensor
–Power dissipated by the RKM, shape (N,).
Source code in src/pyrkm/rkm.py
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 |
|
power_forward(v)
Computes the power dissipated by the RKM in the forward pass.
Parameters:
-
v
(Tensor
) –Visible units, shape (N, n_v).
Returns:
-
Tensor
–Power dissipated by the RKM, shape (N,).
Source code in src/pyrkm/rkm.py
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 |
|
pretrain(pretrained_model, model_state_path='model_states/')
Pretrain the model using a pretrained model.
Parameters:
-
pretrained_model
(str
) –Name of the pretrained model.
-
model_state_path
(str
, default:'model_states/'
) –Path to the model states, by default 'model_states/'.
Source code in src/pyrkm/rkm.py
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 |
|
reconstruct(data, k)
Reconstruct the visible units from the data.
Parameters:
Returns:
-
tuple of numpy.ndarray
–Original and reconstructed visible units.
Source code in src/pyrkm/rkm.py
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 |
|
relaxation_times()
Computes the relaxation times of the RKM in the forward and backward pass.
Returns:
-
tuple of torch.Tensor
–t_forward : relaxation times of the RKM in the forward pass, shape (n_v,). t_backward : relaxation times of the RKM in the backward pass, shape (n_h,).
Source code in src/pyrkm/rkm.py
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 |
|
train(train_data, test_data=[], print_error=False, print_test_error=False, model_state_path='model_states/', print_every=100)
Train the model using the given data and parameters.
Parameters:
-
train_data
(Tensor
) –Training data.
-
test_data
(Tensor
, default:[]
) –Test data, by default [].
-
print_error
(bool
, default:False
) –Whether to print training error, by default False.
-
print_test_error
(bool
, default:False
) –Whether to print test error, by default False.
-
model_state_path
(str
, default:'model_states/'
) –Path to save model states, by default 'model_states/'.
-
print_every
(int
, default:100
) –Frequency of printing progress, by default 100.
Source code in src/pyrkm/rkm.py
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 |
|
v_to_h(v, beta=None)
Convert visible units to hidden units.
Parameters:
-
v
(Tensor
) –Visible units.
-
beta
(float
, default:None
) –Inverse temperature parameter, by default None.
Returns:
-
tuple of torch.Tensor
–Probabilities and sampled hidden units.
Source code in src/pyrkm/rkm.py
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 |
|