RBM class
Restricted Boltzmann Machine implementation.
RBM(model_name, n_visible, n_hidden, k=1, lr=0.001, max_epochs=200000, energy_type='hopfield', 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)
dataclass
A class to represent a Restricted Boltzmann Machine (RBM).
Parameters:
-
model_name(str) –The name of the model.
-
n_visible(int) –The number of visible units.
-
n_hidden(int) –The number of hidden units.
-
k(int, default:1) –The number of Gibbs sampling steps (default is 1).
-
lr(float, default:0.001) –The learning rate.
-
max_epochs(int, default:200000) –The maximum number of training epochs.
-
energy_type(str, default:'hopfield') –The type of energy function to use (default is 'hopfield').
-
optimizer(str, default:'SGD') –The optimizer to use (default is 'SGD', but also Adam is available).
-
batch_size(int, default:1) –The batch size for training (default is 1).
-
train_algo(str, default:'vRDM') –The training algorithm to use between Contrastive Divergence (CD), Persistent Contrastive Divergence (PCD), visible-random (default, vRDM), hidden-random (hRDM).
-
average_data(tensor, default:None) –The average data tensor for centering and initialization (default is None).
-
model_beta(int, default:1) –The inverse temperature parameter (default is 1).
-
mytype(type, default:float32) –The data type for tensors (default is torch.float32).
-
min_W(float, default:-10) –The minimum weight value used for clipping (default is -10).
-
max_W(float, default:10) –The maximum weight value used for clipping (default is 10).
-
regularization(bool, default:False) –Whether to use L1+L2 regularization (default is False).
-
l1_factor(float, default:0) –The L1 regularization factor.
-
l2_factor(float, default:0.001) –The L2 regularization factor.
-
centering(bool, default:False) –Whether to use centering (default is False).
-
g_v(float, default:0.5) –The visible unit gain, required for gradient centering (default is 0.5).
-
g_h(float, default:0.5) –The hidden unit gain, required for gradient centering (default is 0.5).
Adam_update(t, dEdW_data, dEdW_model, dEdv_bias_data, dEdv_bias_model, dEdh_bias_data, dEdh_bias_model)
Updates the model parameters using the Adam optimizer.
Parameters:
-
t(int) –The current epoch.
-
dEdW_data(Tensor) –The gradient of the energy with respect to the weights from the data.
-
dEdW_model(Tensor) –The gradient of the energy with respect to the weights from the model.
-
dEdv_bias_data(Tensor) –The gradient of the energy with respect to the visible biases from the data.
-
dEdv_bias_model(Tensor) –The gradient of the energy with respect to the visible biases from the model.
-
dEdh_bias_data(Tensor) –The gradient of the energy with respect to the hidden biases from the data.
-
dEdh_bias_model(Tensor) –The gradient of the energy with respect to the hidden biases from the model.
Source code in src/pyrkm/rbm.py
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 | |
Bernoulli_h_to_v(h, beta)
Converts hidden units to visible units using Bernoulli sampling.
Parameters:
-
h(Tensor) –The hidden units.
-
beta(float) –The inverse temperature parameter.
Returns:
-
tuple–The probabilities and samples of the visible units.
Source code in src/pyrkm/rbm.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
Bernoulli_v_to_h(v, beta)
Converts visible units to hidden units using Bernoulli sampling.
Parameters:
-
v(Tensor) –The visible units.
-
beta(float) –The inverse temperature parameter.
Returns:
-
tuple–The probabilities and samples of the hidden units.
Source code in src/pyrkm/rbm.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
Deterministic_h_to_v(h, beta)
Deterministically converts hidden units to visible units.
Parameters:
-
h(Tensor) –The hidden units.
-
beta(float) –The inverse temperature parameter.
Returns:
-
tuple–The deterministic visible units.
Source code in src/pyrkm/rbm.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
Deterministic_v_to_h(v, beta)
Deterministically converts visible units to hidden units.
Parameters:
-
v(Tensor) –The visible units.
-
beta(float) –The inverse temperature parameter.
Returns:
-
tuple–The deterministic hidden units.
Source code in src/pyrkm/rbm.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
SGD_update(dEdW_data, dEdW_model, dEdv_bias_data, dEdv_bias_model, dEdh_bias_data, dEdh_bias_model)
Updates the model parameters using Stochastic Gradient Descent (SGD).
Parameters:
-
dEdW_data(Tensor) –The gradient of the energy with respect to the weights from the data.
-
dEdW_model(Tensor) –The gradient of the energy with respect to the weights from the model.
-
dEdv_bias_data(Tensor) –The gradient of the energy with respect to the visible biases from the data.
-
dEdv_bias_model(Tensor) –The gradient of the energy with respect to the visible biases from the model.
-
dEdh_bias_data(Tensor) –The gradient of the energy with respect to the hidden biases from the data.
-
dEdh_bias_model(Tensor) –The gradient of the energy with respect to the hidden biases from the model.
Source code in src/pyrkm/rbm.py
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 | |
after_step_keepup()
Performs operations to keep the model parameters within specified bounds after each training step.
Source code in src/pyrkm/rbm.py
586 587 588 589 590 | |
av_power_backward(h)
Computes the average backward power of the hidden units.
Parameters:
-
h(Tensor) –The hidden units.
Returns:
-
Tensor–The average backward power of the hidden units.
Source code in src/pyrkm/rbm.py
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 | |
av_power_forward(v)
Computes the average forward power of the visible units.
Parameters:
-
v(Tensor) –The visible units.
Returns:
-
Tensor–The average forward power of the visible units.
Source code in src/pyrkm/rbm.py
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 | |
clip_bias()
Clips the biases of the RBM model to be within specified bounds.
Source code in src/pyrkm/rbm.py
762 763 764 765 | |
clip_weights()
Clips the weights of the RBM model to be within specified bounds.
Source code in src/pyrkm/rbm.py
757 758 759 760 | |
delta_eh(v)
Computes the change in energy with respect to the hidden units.
Parameters:
-
v(Tensor) –The visible units.
Returns:
-
Tensor–The change in energy with respect to the hidden units.
Source code in src/pyrkm/rbm.py
805 806 807 808 809 810 811 812 813 814 815 816 817 818 | |
delta_ev(h)
Computes the change in energy with respect to the visible units.
Parameters:
-
h(Tensor) –The hidden units.
Returns:
-
Tensor–The change in energy with respect to the visible units.
Source code in src/pyrkm/rbm.py
820 821 822 823 824 825 826 827 828 829 830 831 832 833 | |
derivatives(v, h)
Computes the derivatives of the energy with respect to the weights and biases.
Parameters:
-
v(Tensor) –The visible units.
-
h(Tensor) –The hidden units.
Returns:
-
tuple–The derivatives of the energy with respect to the weights, visible biases, and hidden biases.
Source code in src/pyrkm/rbm.py
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 | |
derivatives_hopfield(v, h)
Computes the derivatives of the energy with respect to the weights and biases using the Hopfield energy function.
Parameters:
-
v(Tensor) –The visible units.
-
h(Tensor) –The hidden units.
Returns:
-
tuple–The derivatives of the energy with respect to the weights, visible biases, and hidden biases.
Source code in src/pyrkm/rbm.py
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 | |
forward(v, k, beta=None)
Performs a forward pass through the RBM model.
Parameters:
-
v(Tensor) –The visible units.
-
k(int) –The number of Gibbs sampling steps.
-
beta(float, default:None) –The inverse temperature parameter (default is None).
Returns:
-
Tensor–The reconstructed visible units.
Source code in src/pyrkm/rbm.py
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 | |
free_energy(v, beta=None)
Computes the free energy of the visible units.
Parameters:
-
v(Tensor) –The visible units.
-
beta(float, default:None) –The inverse temperature parameter (default is None).
Returns:
-
Tensor–The free energy of the visible units.
Source code in src/pyrkm/rbm.py
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 | |
generate(n_samples, k, h_binarized=True, from_visible=True, beta=None)
Generates samples from the RBM model.
Parameters:
-
n_samples(int) –The number of samples to generate.
-
k(int) –The number of Gibbs sampling steps.
-
h_binarized(bool, default:True) –Whether to binarize the hidden units (default is True).
-
from_visible(bool, default:True) –Whether to generate samples from visible units (default is True).
-
beta(float, default:None) –The inverse temperature parameter (default is None).
Returns:
-
ndarray–The generated samples.
Source code in src/pyrkm/rbm.py
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 | |
h_to_v(h, beta=None)
Converts hidden units to visible units.
Parameters:
-
h(Tensor) –The hidden units.
-
beta(float, default:None) –The inverse temperature parameter (default is None).
Returns:
-
tuple–The probabilities and samples of the visible units.
Source code in src/pyrkm/rbm.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
plot_bias(t)
Plots the hidden and visible biases of the RBM model.
Parameters:
-
t(int) –The current epoch.
Source code in src/pyrkm/rbm.py
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 | |
plot_visible_bias(t)
Plots the visible biases of the RBM model.
Parameters:
-
t(int) –The current epoch.
Source code in src/pyrkm/rbm.py
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 | |
plot_weights(t)
Plots the weights of the RBM model.
Parameters:
-
t(int) –The current epoch.
Source code in src/pyrkm/rbm.py
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 | |
power_backward(h)
Computes the backward power of the hidden units.
Parameters:
-
h(Tensor) –The hidden units.
Returns:
-
Tensor–The backward power of the hidden units.
Source code in src/pyrkm/rbm.py
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 | |
power_forward(v)
Computes the forward power of the visible units.
Parameters:
-
v(Tensor) –The visible units.
Returns:
-
Tensor–The forward power of the visible units.
Source code in src/pyrkm/rbm.py
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 | |
pretrain(pretrained_model, model_state_path='model_states/')
Loads pretrained parameters from a specified model.
Parameters:
-
pretrained_model(str) –The name of the pretrained model.
-
model_state_path(str, default:'model_states/') –The path to the directory containing the model states (default is 'model_states/').
Source code in src/pyrkm/rbm.py
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 | |
reconstruct(data, k)
Reconstructs the visible units from the data using k Gibbs sampling steps.
Parameters:
Returns:
-
tuple–The original and reconstructed visible units.
Source code in src/pyrkm/rbm.py
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 | |
relaxation_times()
Computes the relaxation times for the forward and backward passes.
Returns:
-
tuple–The relaxation times for the forward and backward passes.
Source code in src/pyrkm/rbm.py
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 | |
train(train_data, test_data=None, print_error=False, print_test_error=False, model_state_path='model_states/', print_every=100)
Trains the RBM model using the specified training algorithm.
Parameters:
-
train_data(iterable) –The training data.
-
test_data(iterable, default:None) –The test data (default is an empty list).
-
print_error(bool, default:False) –Whether to print the training error (default is False).
-
print_test_error(bool, default:False) –Whether to print the test error (default is False).
-
model_state_path(str, default:'model_states/') –The path to the directory containing the model states (default is 'model_states/').
-
print_every(int, default:100) –The number of epochs between printing the training status (default is 100).
Source code in src/pyrkm/rbm.py
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 | |
v_to_h(v, beta=None)
Converts visible units to hidden units.
Parameters:
-
v(Tensor) –The visible units.
-
beta(float, default:None) –The inverse temperature parameter (default is None).
Returns:
-
tuple–The probabilities and samples of the hidden units.
Source code in src/pyrkm/rbm.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |