rbms 0.2.0__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {rbms-0.2.0 → rbms-0.3.0}/PKG-INFO +1 -1
- {rbms-0.2.0 → rbms-0.3.0}/pyproject.toml +1 -1
- {rbms-0.2.0 → rbms-0.3.0}/rbms/bernoulli_bernoulli/implement.py +9 -3
- {rbms-0.2.0 → rbms-0.3.0}/rbms/classes.py +1 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/dataset/__init__.py +3 -2
- {rbms-0.2.0 → rbms-0.3.0}/rbms/dataset/dataset_class.py +54 -4
- {rbms-0.2.0 → rbms-0.3.0}/rbms/dataset/load_fasta.py +1 -3
- {rbms-0.2.0 → rbms-0.3.0}/rbms/dataset/load_h5.py +8 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/dataset/parser.py +6 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/io.py +4 -1
- {rbms-0.2.0 → rbms-0.3.0}/rbms/parser.py +41 -11
- {rbms-0.2.0 → rbms-0.3.0}/rbms/potts_bernoulli/classes.py +0 -3
- {rbms-0.2.0 → rbms-0.3.0}/rbms/potts_bernoulli/implement.py +15 -8
- {rbms-0.2.0 → rbms-0.3.0}/rbms/scripts/train_rbm.py +9 -7
- {rbms-0.2.0 → rbms-0.3.0}/rbms/training/pcd.py +22 -10
- {rbms-0.2.0 → rbms-0.3.0}/rbms/training/utils.py +27 -4
- {rbms-0.2.0 → rbms-0.3.0}/rbms/utils.py +5 -5
- {rbms-0.2.0 → rbms-0.3.0}/tests/conftest.py +5 -1
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/bernoulli_bernoulli/test_bernoulli_utils_bbrbm.py +6 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/potts_bernoulli/test_utils_pbrbm.py +6 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/training/test_pcd.py +0 -3
- {rbms-0.2.0 → rbms-0.3.0}/tests/use_cases/test_bbrbm.py +1 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/use_cases/test_pbrbm.py +2 -0
- rbms-0.2.0/.github/workflows/docs.yaml +0 -49
- rbms-0.2.0/docs/Makefile +0 -20
- rbms-0.2.0/docs/make.bat +0 -35
- rbms-0.2.0/docs/source/api.md +0 -151
- rbms-0.2.0/docs/source/auto_examples/images/sphx_glr_plot_generated_samples_001.png +0 -0
- rbms-0.2.0/docs/source/auto_examples/images/sphx_glr_plot_generated_samples_002.png +0 -0
- rbms-0.2.0/docs/source/auto_examples/images/sphx_glr_plot_generated_samples_003.png +0 -0
- rbms-0.2.0/docs/source/auto_examples/images/sphx_glr_plot_generated_samples_004.png +0 -0
- rbms-0.2.0/docs/source/auto_examples/images/thumb/sphx_glr_plot_generated_samples_thumb.png +0 -0
- rbms-0.2.0/docs/source/auto_examples/index.rst +0 -63
- rbms-0.2.0/docs/source/auto_examples/plot_generated_samples.codeobj.json +0 -989
- rbms-0.2.0/docs/source/auto_examples/plot_generated_samples.ipynb +0 -187
- rbms-0.2.0/docs/source/auto_examples/plot_generated_samples.py +0 -127
- rbms-0.2.0/docs/source/auto_examples/plot_generated_samples.py.md5 +0 -1
- rbms-0.2.0/docs/source/auto_examples/plot_generated_samples.rst +0 -343
- rbms-0.2.0/docs/source/auto_examples/sg_execution_times.rst +0 -37
- rbms-0.2.0/docs/source/conf.py +0 -66
- rbms-0.2.0/docs/source/examples.md +0 -0
- rbms-0.2.0/docs/source/index.md +0 -39
- rbms-0.2.0/docs/source/myst.yml +0 -4
- rbms-0.2.0/docs/source/rbm.md +0 -131
- rbms-0.2.0/docs/source/references.bib +0 -275
- rbms-0.2.0/docs/source/sg_execution_times.rst +0 -37
- rbms-0.2.0/docs/source/tuto/dataset.md +0 -39
- rbms-0.2.0/docs/source/tuto/parallel_tempering.md +0 -1
- rbms-0.2.0/docs/source/tuto/train_rbm.md +0 -51
- rbms-0.2.0/docs/source/tutorials.md +0 -12
- rbms-0.2.0/docs/source/whats_new.rst +0 -0
- rbms-0.2.0/examples/GALLERY_HEADER.rst +0 -4
- rbms-0.2.0/examples/plot_generated_samples.py +0 -127
- {rbms-0.2.0 → rbms-0.3.0}/.github/workflows/codecov.yaml +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/.github/workflows/test.yaml +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/.github/workflows/whats_new.yaml +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/.gitignore +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/LICENSE +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/MANIFEST.in +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/README.md +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/__init__.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/bernoulli_bernoulli/__init__.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/bernoulli_bernoulli/classes.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/bernoulli_bernoulli/functional.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/const.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/custom_fn.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/dataset/fasta_utils.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/dataset/utils.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/map_model.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/metrics/__init__.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/metrics/aats.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/partition_function/ais.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/partition_function/exact.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/plot.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/potts_bernoulli/__init__.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/potts_bernoulli/functional.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/potts_bernoulli/tools.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/potts_bernoulli/utils.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/sampling/gibbs.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/sampling/pt.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/scripts/__init__.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/scripts/entrypoint.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/rbms/scripts/pt_sampling.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/bernoulli_bernoulli/implement/test_compute_energy_bbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/bernoulli_bernoulli/implement/test_compute_energy_hiddens_bbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/bernoulli_bernoulli/implement/test_compute_energy_visibles_bbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/bernoulli_bernoulli/implement/test_compute_gradient_bbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/bernoulli_bernoulli/implement/test_init_chains_bbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/bernoulli_bernoulli/implement/test_init_parameters_bbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/bernoulli_bernoulli/implement/test_sample_hiddens_bbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/bernoulli_bernoulli/implement/test_sample_visibles_bbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/bernoulli_bernoulli/test_functional_bbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/bernoulli_bernoulli/test_methods_bbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/metrics/test_aats.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/potts_bernoulli/implement/test_compute_energy_hiddens_pbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/potts_bernoulli/implement/test_compute_energy_pbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/potts_bernoulli/implement/test_compute_energy_visibles_pbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/potts_bernoulli/implement/test_compute_gradient_pbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/potts_bernoulli/implement/test_init_chains_pbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/potts_bernoulli/implement/test_init_parameters_pbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/potts_bernoulli/implement/test_sample_hiddens_pbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/potts_bernoulli/implement/test_sample_visibles_pbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/potts_bernoulli/test_functional_pbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/potts_bernoulli/test_methods_pbrbm.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/test_custom_fn.py +0 -0
- {rbms-0.2.0 → rbms-0.3.0}/tests/unit_test/test_utils.py +0 -0
|
@@ -47,7 +47,9 @@ def _compute_energy_visibles(
|
|
|
47
47
|
) -> Tensor:
|
|
48
48
|
field = v @ vbias
|
|
49
49
|
exponent = hbias + (v @ weight_matrix)
|
|
50
|
-
log_term = torch.where(
|
|
50
|
+
log_term = torch.where(
|
|
51
|
+
exponent < 10, torch.log(1.0 + torch.exp(exponent)), exponent
|
|
52
|
+
)
|
|
51
53
|
return -field - log_term.sum(1)
|
|
52
54
|
|
|
53
55
|
|
|
@@ -57,7 +59,9 @@ def _compute_energy_hiddens(
|
|
|
57
59
|
) -> Tensor:
|
|
58
60
|
field = h @ hbias
|
|
59
61
|
exponent = vbias + (h @ weight_matrix.T)
|
|
60
|
-
log_term = torch.where(
|
|
62
|
+
log_term = torch.where(
|
|
63
|
+
exponent < 10, torch.log(1.0 + torch.exp(exponent)), exponent
|
|
64
|
+
)
|
|
61
65
|
return -field - log_term.sum(1)
|
|
62
66
|
|
|
63
67
|
|
|
@@ -140,7 +144,9 @@ def _init_chains(
|
|
|
140
144
|
|
|
141
145
|
if start_v is None:
|
|
142
146
|
# Dummy mean visible
|
|
143
|
-
mv =
|
|
147
|
+
mv = (
|
|
148
|
+
torch.ones(size=(num_samples, num_visibles), device=device, dtype=dtype) / 2
|
|
149
|
+
)
|
|
144
150
|
v = torch.bernoulli(mv)
|
|
145
151
|
else:
|
|
146
152
|
# Dummy mean visible
|
|
@@ -249,4 +249,5 @@ class RBM(EBM):
|
|
|
249
249
|
for _ in range(n_steps):
|
|
250
250
|
new_chains = self.sample_hiddens(chains=new_chains, beta=beta)
|
|
251
251
|
new_chains = self.sample_visibles(chains=new_chains, beta=beta)
|
|
252
|
+
new_chains = self.sample_hiddens(chains=new_chains, beta=beta)
|
|
252
253
|
return new_chains
|
|
@@ -42,8 +42,6 @@ def load_dataset(
|
|
|
42
42
|
alphabet=alphabet,
|
|
43
43
|
device=device,
|
|
44
44
|
)
|
|
45
|
-
if not binarize:
|
|
46
|
-
is_binary = False
|
|
47
45
|
case _:
|
|
48
46
|
raise ValueError(
|
|
49
47
|
"""
|
|
@@ -53,6 +51,9 @@ def load_dataset(
|
|
|
53
51
|
- '.fasta'
|
|
54
52
|
"""
|
|
55
53
|
)
|
|
54
|
+
if not binarize:
|
|
55
|
+
is_binary = False
|
|
56
|
+
|
|
56
57
|
# Select subset of dataset w.r.t. labels
|
|
57
58
|
if subset_labels is not None and labels is not None:
|
|
58
59
|
data, labels = get_subset_labels(data, labels, subset_labels)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import gzip
|
|
2
2
|
import textwrap
|
|
3
|
-
from typing import Dict, Union
|
|
3
|
+
from typing import Dict, Union, Self, Tuple, Optional
|
|
4
4
|
|
|
5
5
|
import numpy as np
|
|
6
6
|
import torch
|
|
@@ -116,9 +116,59 @@ class RBMDataset(Dataset):
|
|
|
116
116
|
for i in pbar:
|
|
117
117
|
en[i] = len(
|
|
118
118
|
gzip.compress(
|
|
119
|
-
(
|
|
120
|
-
|
|
121
|
-
)
|
|
119
|
+
(
|
|
120
|
+
self.data[torch.randperm(self.data.shape[0])[:num_samples]]
|
|
121
|
+
).astype(int)
|
|
122
122
|
)
|
|
123
123
|
)
|
|
124
124
|
return np.mean(en)
|
|
125
|
+
|
|
126
|
+
def split_train_test(
|
|
127
|
+
self,
|
|
128
|
+
rng: np.random.Generator,
|
|
129
|
+
train_size: float,
|
|
130
|
+
test_size: Optional[float] = None,
|
|
131
|
+
) -> Tuple[Self, Self | None]:
|
|
132
|
+
num_samples = self.data.shape[0]
|
|
133
|
+
# Shuffle dataset
|
|
134
|
+
permutation_index = rng.permutation(num_samples)
|
|
135
|
+
train_size = int(train_size * num_samples)
|
|
136
|
+
if test_size is not None:
|
|
137
|
+
test_size = int(test_size * num_samples)
|
|
138
|
+
else:
|
|
139
|
+
test_size = num_samples - train_size
|
|
140
|
+
train_dataset = RBMDataset(
|
|
141
|
+
data=self.data[permutation_index[:train_size]].cpu().numpy(),
|
|
142
|
+
labels=self.labels[permutation_index[:train_size]].cpu().numpy(),
|
|
143
|
+
weights=self.weights[permutation_index[:train_size]].cpu().numpy(),
|
|
144
|
+
names=self.names[permutation_index[:train_size]],
|
|
145
|
+
dataset_name=self.dataset_name,
|
|
146
|
+
is_binary=self.is_binary,
|
|
147
|
+
device=self.device,
|
|
148
|
+
dtype=self.dtype,
|
|
149
|
+
)
|
|
150
|
+
test_dataset = None
|
|
151
|
+
if test_size > 0:
|
|
152
|
+
test_dataset = RBMDataset(
|
|
153
|
+
data=self.data[permutation_index[train_size : train_size + test_size]]
|
|
154
|
+
.cpu()
|
|
155
|
+
.numpy(),
|
|
156
|
+
labels=self.labels[
|
|
157
|
+
permutation_index[train_size : train_size + test_size]
|
|
158
|
+
]
|
|
159
|
+
.cpu()
|
|
160
|
+
.numpy(),
|
|
161
|
+
weights=self.weights[
|
|
162
|
+
permutation_index[train_size : train_size + test_size]
|
|
163
|
+
]
|
|
164
|
+
.cpu()
|
|
165
|
+
.numpy(),
|
|
166
|
+
names=self.names[
|
|
167
|
+
permutation_index[train_size : train_size + test_size]
|
|
168
|
+
],
|
|
169
|
+
dataset_name=self.dataset_name,
|
|
170
|
+
is_binary=self.is_binary,
|
|
171
|
+
device=self.device,
|
|
172
|
+
dtype=self.dtype,
|
|
173
|
+
)
|
|
174
|
+
return train_dataset, test_dataset
|
|
@@ -52,11 +52,9 @@ def load_FASTA(
|
|
|
52
52
|
weights = weights.squeeze(-1)
|
|
53
53
|
if binarize:
|
|
54
54
|
num_categories = len(np.unique(dataset))
|
|
55
|
-
|
|
55
|
+
dataset = (
|
|
56
56
|
one_hot(torch.from_numpy(dataset).int(), num_classes=num_categories)
|
|
57
57
|
.view(dataset.shape[0], -1)
|
|
58
58
|
.numpy()
|
|
59
59
|
)
|
|
60
|
-
print("ici")
|
|
61
|
-
assert np.allclose(dataset, dataset_oh)
|
|
62
60
|
return dataset, weights, names
|
|
@@ -31,6 +31,14 @@ def load_HDF5(
|
|
|
31
31
|
f"Ignoring labels since its dimension ({labels.shape[0]}) does not match the number of samples ({dataset.shape[0]})."
|
|
32
32
|
)
|
|
33
33
|
labels = None
|
|
34
|
+
# From BEG to Potts
|
|
35
|
+
if not binarize:
|
|
36
|
+
X = (dataset[:, :] + 1) / 2
|
|
37
|
+
for i in range(X.shape[0]):
|
|
38
|
+
for j in range(X.shape[1]):
|
|
39
|
+
if X[i, j] == 0.5:
|
|
40
|
+
X[i, j] = 2
|
|
41
|
+
return X, labels
|
|
34
42
|
if "cont" not in str(filename.resolve()):
|
|
35
43
|
unique_values = np.unique(dataset)
|
|
36
44
|
is_ising = np.all(unique_values == np.array([-1, 1]))
|
|
@@ -51,4 +51,10 @@ def add_args_dataset(parser: argparse.ArgumentParser) -> argparse.ArgumentParser
|
|
|
51
51
|
action="store_true",
|
|
52
52
|
help="(Defaults to False). Binarize the dataset.",
|
|
53
53
|
)
|
|
54
|
+
dataset_args.add_argument(
|
|
55
|
+
"--seed",
|
|
56
|
+
default=None,
|
|
57
|
+
type=int,
|
|
58
|
+
help="(Defaults to None). Seed to split the dataset.",
|
|
59
|
+
)
|
|
54
60
|
return parser
|
|
@@ -130,7 +130,10 @@ def load_model(
|
|
|
130
130
|
hyperparameters["learning_rate"] = float(
|
|
131
131
|
f["hyperparameters"]["learning_rate"][()]
|
|
132
132
|
)
|
|
133
|
-
|
|
133
|
+
if "seed" in f["hyperparameters"].keys():
|
|
134
|
+
hyperparameters["seed"] = int(f["hyperparameters"]["seed"][()])
|
|
135
|
+
if "train_size" in f["hyperparameters"].keys():
|
|
136
|
+
hyperparameters["train_size"] = int(f["hyperparameters"]["train_size"][()])
|
|
134
137
|
params = load_params(
|
|
135
138
|
filename=filename, index=index, device=device, dtype=dtype, map_model=map_model
|
|
136
139
|
)
|
|
@@ -2,6 +2,7 @@ import argparse
|
|
|
2
2
|
from typing import Any
|
|
3
3
|
|
|
4
4
|
import torch
|
|
5
|
+
import numpy as np
|
|
5
6
|
|
|
6
7
|
|
|
7
8
|
def add_args_pytorch(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
|
|
@@ -38,7 +39,7 @@ def add_args_saves(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
|
|
|
38
39
|
"-o",
|
|
39
40
|
"--filename",
|
|
40
41
|
type=str,
|
|
41
|
-
default=
|
|
42
|
+
default=None,
|
|
42
43
|
help="(Defaults to RBM.h5). Path to the file where to save the model or load if training is restored.",
|
|
43
44
|
)
|
|
44
45
|
save_args.add_argument(
|
|
@@ -50,14 +51,14 @@ def add_args_saves(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
|
|
|
50
51
|
save_args.add_argument(
|
|
51
52
|
"--acc_ptt",
|
|
52
53
|
type=float,
|
|
53
|
-
default=
|
|
54
|
+
default=None,
|
|
54
55
|
help="(Defaults to 0.25). Minimum PTT acceptance to save configurations for ptt file.",
|
|
55
56
|
)
|
|
56
57
|
save_args.add_argument(
|
|
57
58
|
"--acc_ll",
|
|
58
59
|
type=float,
|
|
59
|
-
default=
|
|
60
|
-
help="(Defaults to 0.
|
|
60
|
+
default=None,
|
|
61
|
+
help="(Defaults to 0.7). Minimum PTT acceptance to save configurations for ll file.",
|
|
61
62
|
)
|
|
62
63
|
save_args.add_argument(
|
|
63
64
|
"--spacing",
|
|
@@ -88,42 +89,42 @@ def add_args_rbm(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
|
|
|
88
89
|
rbm_args.add_argument(
|
|
89
90
|
"--num_hiddens",
|
|
90
91
|
type=int,
|
|
91
|
-
default=
|
|
92
|
+
default=None,
|
|
92
93
|
help="(Defaults to 100). Number of hidden units.",
|
|
93
94
|
)
|
|
94
95
|
rbm_args.add_argument(
|
|
95
96
|
"--batch_size",
|
|
96
97
|
type=int,
|
|
97
|
-
default=
|
|
98
|
+
default=None,
|
|
98
99
|
help="(Defaults to 2000). Minibatch size.",
|
|
99
100
|
)
|
|
100
101
|
rbm_args.add_argument(
|
|
101
102
|
"--gibbs_steps",
|
|
102
103
|
type=int,
|
|
103
|
-
default=
|
|
104
|
+
default=None,
|
|
104
105
|
help="(Defaults to 100). Number of gibbs steps to perform for each gradient update.",
|
|
105
106
|
)
|
|
106
107
|
rbm_args.add_argument(
|
|
107
108
|
"--learning_rate",
|
|
108
109
|
type=float,
|
|
109
|
-
default=
|
|
110
|
+
default=None,
|
|
110
111
|
help="(Defaults to 0.01). Learning rate.",
|
|
111
112
|
)
|
|
112
113
|
rbm_args.add_argument(
|
|
113
114
|
"--num_chains",
|
|
114
115
|
type=int,
|
|
115
|
-
default=
|
|
116
|
+
default=None,
|
|
116
117
|
help="(Defaults to 2000). Number of parallel chains.",
|
|
117
118
|
)
|
|
118
119
|
rbm_args.add_argument(
|
|
119
120
|
"--num_updates",
|
|
120
|
-
default=
|
|
121
|
+
default=None,
|
|
121
122
|
type=int,
|
|
122
123
|
help="(Defaults to 10 000). Number of gradient updates to perform.",
|
|
123
124
|
)
|
|
124
125
|
rbm_args.add_argument(
|
|
125
126
|
"--beta",
|
|
126
|
-
default=
|
|
127
|
+
default=None,
|
|
127
128
|
type=float,
|
|
128
129
|
help="(Defaults to 1.0). The inverse temperature of the RBM",
|
|
129
130
|
)
|
|
@@ -166,3 +167,32 @@ def match_args_dtype(args: dict[str, Any]) -> dict[str, Any]:
|
|
|
166
167
|
case "double":
|
|
167
168
|
args["dtype"] = torch.float64
|
|
168
169
|
return args
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
default_args: dict[str, Any] = {
|
|
173
|
+
"filename": "RBM.h5",
|
|
174
|
+
"n_save": 50,
|
|
175
|
+
"acc_ptt": 0.25,
|
|
176
|
+
"acc_ll": 0.7,
|
|
177
|
+
"spacing": "exp",
|
|
178
|
+
"log": True,
|
|
179
|
+
"overwrite": True,
|
|
180
|
+
"num_hiddens": 100,
|
|
181
|
+
"batch_size": 2000,
|
|
182
|
+
"gibbs_steps": 100,
|
|
183
|
+
"learning_rate": 0.01,
|
|
184
|
+
"num_chains": 2000,
|
|
185
|
+
"num_updates": 10000,
|
|
186
|
+
"beta": 1.0,
|
|
187
|
+
"restore": False,
|
|
188
|
+
"seed": np.random.randint(0, 1000000000000),
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def set_args_default(
|
|
193
|
+
args: dict[str, Any], default_args: dict[str, Any]
|
|
194
|
+
) -> dict[str, Any]:
|
|
195
|
+
for k, v in args.items():
|
|
196
|
+
if v is None and k in default_args.keys():
|
|
197
|
+
args[k] = default_args[k]
|
|
198
|
+
return args
|
|
@@ -159,9 +159,6 @@ class PBRBM(RBM):
|
|
|
159
159
|
var_init=var_init,
|
|
160
160
|
)
|
|
161
161
|
params = PBRBM(weight_matrix=weight_matrix, vbias=vbias, hbias=hbias)
|
|
162
|
-
from rbms.potts_bernoulli.utils import ensure_zero_sum_gauge
|
|
163
|
-
|
|
164
|
-
ensure_zero_sum_gauge(params)
|
|
165
162
|
return params
|
|
166
163
|
|
|
167
164
|
def named_parameters(self):
|
|
@@ -158,15 +158,21 @@ def _compute_gradient(
|
|
|
158
158
|
h_gen_centered = h_chain
|
|
159
159
|
|
|
160
160
|
# Gradient
|
|
161
|
-
grad_weight_matrix =
|
|
162
|
-
(
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
161
|
+
grad_weight_matrix = (
|
|
162
|
+
torch.tensordot(
|
|
163
|
+
v_data_centered,
|
|
164
|
+
h_data_centered,
|
|
165
|
+
dims=[[0], [0]],
|
|
166
|
+
)
|
|
167
|
+
/ v_data.shape[0]
|
|
168
|
+
- torch.tensordot(
|
|
169
|
+
v_gen_centered,
|
|
170
|
+
h_gen_centered,
|
|
171
|
+
dims=[[0], [0]],
|
|
172
|
+
)
|
|
173
|
+
/ v_chain.shape[0]
|
|
169
174
|
)
|
|
175
|
+
|
|
170
176
|
grad_vbias = v_data_mean - v_gen_mean
|
|
171
177
|
grad_hbias = h_data_mean - h_gen_mean
|
|
172
178
|
weight_matrix.grad.set_(grad_weight_matrix)
|
|
@@ -229,4 +235,5 @@ def _init_parameters(
|
|
|
229
235
|
)
|
|
230
236
|
* var_init
|
|
231
237
|
)
|
|
238
|
+
# print(torch.svd(weight_matrix.reshape(-1, weight_matrix.shape[-1])).S)
|
|
232
239
|
return vbias, hbias, weight_matrix
|
|
@@ -11,6 +11,7 @@ from rbms.parser import (
|
|
|
11
11
|
add_args_saves,
|
|
12
12
|
match_args_dtype,
|
|
13
13
|
remove_argument,
|
|
14
|
+
default_args,
|
|
14
15
|
)
|
|
15
16
|
from rbms.training.pcd import train
|
|
16
17
|
from rbms.training.utils import get_checkpoints
|
|
@@ -30,30 +31,31 @@ def train_rbm(args: dict):
|
|
|
30
31
|
checkpoints = get_checkpoints(
|
|
31
32
|
num_updates=args["num_updates"], n_save=args["n_save"], spacing=args["spacing"]
|
|
32
33
|
)
|
|
33
|
-
|
|
34
|
+
dataset, _ = load_dataset(
|
|
34
35
|
dataset_name=args["data"],
|
|
35
36
|
subset_labels=args["subset_labels"],
|
|
36
37
|
use_weights=args["use_weights"],
|
|
37
38
|
alphabet=args["alphabet"],
|
|
38
39
|
binarize=args["binarize"],
|
|
39
|
-
train_size=
|
|
40
|
-
test_size=
|
|
40
|
+
train_size=1.0,
|
|
41
|
+
test_size=None,
|
|
41
42
|
device=args["device"],
|
|
42
43
|
dtype=args["dtype"],
|
|
44
|
+
seed=args["seed"],
|
|
43
45
|
)
|
|
44
|
-
print(
|
|
45
|
-
if
|
|
46
|
+
print(dataset)
|
|
47
|
+
if dataset.is_binary:
|
|
46
48
|
model_type = "BBRBM"
|
|
47
49
|
else:
|
|
48
50
|
model_type = "PBRBM"
|
|
49
51
|
train(
|
|
50
|
-
dataset=
|
|
51
|
-
test_dataset=test_dataset,
|
|
52
|
+
dataset=dataset,
|
|
52
53
|
model_type=model_type,
|
|
53
54
|
args=args,
|
|
54
55
|
dtype=args["dtype"],
|
|
55
56
|
checkpoints=checkpoints,
|
|
56
57
|
map_model=map_model,
|
|
58
|
+
default_args=default_args,
|
|
57
59
|
)
|
|
58
60
|
|
|
59
61
|
|
|
@@ -14,6 +14,8 @@ from rbms.potts_bernoulli.classes import PBRBM
|
|
|
14
14
|
from rbms.potts_bernoulli.utils import ensure_zero_sum_gauge
|
|
15
15
|
from rbms.training.utils import create_machine, setup_training
|
|
16
16
|
from rbms.utils import check_file_existence, log_to_csv
|
|
17
|
+
from rbms.parser import default_args, set_args_default
|
|
18
|
+
from rbms.potts_bernoulli.utils import ensure_zero_sum_gauge
|
|
17
19
|
|
|
18
20
|
|
|
19
21
|
def fit_batch_pcd(
|
|
@@ -24,12 +26,12 @@ def fit_batch_pcd(
|
|
|
24
26
|
beta: float,
|
|
25
27
|
centered: bool = True,
|
|
26
28
|
) -> Tuple[dict[str, Tensor], dict]:
|
|
27
|
-
"""Sample the
|
|
29
|
+
"""Sample the EBM and compute the gradient.
|
|
28
30
|
|
|
29
31
|
Args:
|
|
30
32
|
batch (Tuple[Tensor, Tensor]): Dataset samples and associated weights.
|
|
31
33
|
parallel_chains (dict[str, Tensor]): Parallel chains used for gradient computation.
|
|
32
|
-
params (
|
|
34
|
+
params (EBM): Parameters of the EBM.
|
|
33
35
|
gibbs_steps (int): Number of Gibbs steps to perform.
|
|
34
36
|
beta (float): Inverse temperature.
|
|
35
37
|
|
|
@@ -54,12 +56,12 @@ def fit_batch_pcd(
|
|
|
54
56
|
|
|
55
57
|
def train(
|
|
56
58
|
dataset: RBMDataset,
|
|
57
|
-
test_dataset: RBMDataset,
|
|
58
59
|
model_type: str,
|
|
59
60
|
args: dict,
|
|
60
61
|
dtype: torch.dtype,
|
|
61
62
|
checkpoints: np.ndarray,
|
|
62
63
|
map_model: dict[str, EBM] = map_model,
|
|
64
|
+
default_args: dict = default_args,
|
|
63
65
|
) -> None:
|
|
64
66
|
"""Train an EBM.
|
|
65
67
|
|
|
@@ -71,20 +73,28 @@ def train(
|
|
|
71
73
|
dtype (torch.dtype): The data type for the parameters.
|
|
72
74
|
checkpoints (np.ndarray): An array of checkpoints for saving model states.
|
|
73
75
|
"""
|
|
76
|
+
|
|
74
77
|
filename = args["filename"]
|
|
75
78
|
if not (args["overwrite"]):
|
|
76
79
|
check_file_existence(filename)
|
|
77
80
|
|
|
78
81
|
num_visibles = dataset.get_num_visibles()
|
|
79
|
-
|
|
80
82
|
# Create a first archive with the initialized model
|
|
81
83
|
if not (args["restore"]):
|
|
84
|
+
args = set_args_default(args=args, default_args=default_args)
|
|
85
|
+
rng = np.random.default_rng(args["seed"])
|
|
86
|
+
train_dataset, test_dataset = dataset.split_train_test(
|
|
87
|
+
rng, args["train_size"], args["test_size"]
|
|
88
|
+
)
|
|
82
89
|
params = map_model[model_type].init_parameters(
|
|
83
90
|
num_hiddens=args["num_hiddens"],
|
|
84
|
-
dataset=
|
|
91
|
+
dataset=train_dataset,
|
|
85
92
|
device=args["device"],
|
|
86
93
|
dtype=dtype,
|
|
87
94
|
)
|
|
95
|
+
|
|
96
|
+
if isinstance(params, PBRBM):
|
|
97
|
+
ensure_zero_sum_gauge(params)
|
|
88
98
|
create_machine(
|
|
89
99
|
filename=filename,
|
|
90
100
|
params=params,
|
|
@@ -94,23 +104,25 @@ def train(
|
|
|
94
104
|
batch_size=args["batch_size"],
|
|
95
105
|
gibbs_steps=args["gibbs_steps"],
|
|
96
106
|
learning_rate=args["learning_rate"],
|
|
107
|
+
train_size=args["train_size"],
|
|
97
108
|
log=args["log"],
|
|
98
109
|
flags=["checkpoint"],
|
|
110
|
+
seed=args["seed"],
|
|
99
111
|
)
|
|
100
|
-
|
|
101
112
|
(
|
|
102
113
|
params,
|
|
103
114
|
parallel_chains,
|
|
104
115
|
args,
|
|
105
|
-
learning_rate,
|
|
106
116
|
num_updates,
|
|
107
117
|
start,
|
|
108
118
|
elapsed_time,
|
|
109
119
|
log_filename,
|
|
110
120
|
pbar,
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
121
|
+
train_dataset,
|
|
122
|
+
test_dataset,
|
|
123
|
+
) = setup_training(args, map_model=map_model, dataset=dataset)
|
|
124
|
+
args = set_args_default(args=args, default_args=default_args)
|
|
125
|
+
optimizer = SGD(params.parameters(), lr=args["learning_rate"], maximize=True)
|
|
114
126
|
|
|
115
127
|
for k, v in args.items():
|
|
116
128
|
print(f"{k} : {v}")
|
|
@@ -13,13 +13,25 @@ from rbms.const import LOG_FILE_HEADER
|
|
|
13
13
|
from rbms.io import load_model, save_model
|
|
14
14
|
from rbms.map_model import map_model
|
|
15
15
|
from rbms.utils import get_saved_updates
|
|
16
|
+
from rbms.dataset.dataset_class import RBMDataset
|
|
16
17
|
|
|
17
18
|
|
|
18
19
|
def setup_training(
|
|
19
20
|
args: dict,
|
|
21
|
+
dataset: RBMDataset,
|
|
20
22
|
map_model: dict[str, EBM] = map_model,
|
|
21
23
|
) -> Tuple[
|
|
22
|
-
EBM,
|
|
24
|
+
EBM,
|
|
25
|
+
dict[str, Tensor],
|
|
26
|
+
dict[str, Any],
|
|
27
|
+
float,
|
|
28
|
+
int,
|
|
29
|
+
float,
|
|
30
|
+
float,
|
|
31
|
+
pathlib.Path,
|
|
32
|
+
tqdm,
|
|
33
|
+
RBMDataset,
|
|
34
|
+
RBMDataset,
|
|
23
35
|
]:
|
|
24
36
|
# Retrieve the the number of training updates already performed on the model
|
|
25
37
|
updates = get_saved_updates(filename=args["filename"])
|
|
@@ -40,8 +52,14 @@ def setup_training(
|
|
|
40
52
|
|
|
41
53
|
# Hyperparameters
|
|
42
54
|
for k, v in hyperparameters.items():
|
|
43
|
-
|
|
44
|
-
|
|
55
|
+
if v is not None:
|
|
56
|
+
args[k] = v
|
|
57
|
+
|
|
58
|
+
train_dataset, test_dataset = dataset.split_train_test(
|
|
59
|
+
rng=np.random.default_rng(args["seed"]),
|
|
60
|
+
train_size=args["train_size"],
|
|
61
|
+
test_size=args["test_size"],
|
|
62
|
+
)
|
|
45
63
|
|
|
46
64
|
# Open the log file if it exists
|
|
47
65
|
log_filename = pathlib.Path(args["filename"]).parent / pathlib.Path(
|
|
@@ -70,12 +88,13 @@ def setup_training(
|
|
|
70
88
|
params,
|
|
71
89
|
parallel_chains,
|
|
72
90
|
args,
|
|
73
|
-
learning_rate,
|
|
74
91
|
num_updates,
|
|
75
92
|
start,
|
|
76
93
|
elapsed_time,
|
|
77
94
|
log_filename,
|
|
78
95
|
pbar,
|
|
96
|
+
train_dataset,
|
|
97
|
+
test_dataset,
|
|
79
98
|
)
|
|
80
99
|
|
|
81
100
|
|
|
@@ -88,8 +107,10 @@ def create_machine(
|
|
|
88
107
|
batch_size: int,
|
|
89
108
|
gibbs_steps: int,
|
|
90
109
|
learning_rate: float,
|
|
110
|
+
train_size: float,
|
|
91
111
|
log: bool,
|
|
92
112
|
flags: List[str],
|
|
113
|
+
seed: int,
|
|
93
114
|
) -> None:
|
|
94
115
|
"""Create a RBM and save it to a new file.
|
|
95
116
|
|
|
@@ -116,6 +137,8 @@ def create_machine(
|
|
|
116
137
|
hyperparameters["gibbs_steps"] = gibbs_steps
|
|
117
138
|
hyperparameters["filename"] = str(filename)
|
|
118
139
|
hyperparameters["learning_rate"] = learning_rate
|
|
140
|
+
hyperparameters["train_size"] = True
|
|
141
|
+
hyperparameters["seed"] = seed
|
|
119
142
|
|
|
120
143
|
save_model(
|
|
121
144
|
filename=filename,
|
|
@@ -8,7 +8,7 @@ import numpy as np
|
|
|
8
8
|
import torch
|
|
9
9
|
from torch import Tensor
|
|
10
10
|
|
|
11
|
-
from rbms.classes import
|
|
11
|
+
from rbms.classes import EBM
|
|
12
12
|
from rbms.const import LOG_FILE_HEADER
|
|
13
13
|
|
|
14
14
|
|
|
@@ -28,7 +28,7 @@ def get_eigenvalues_history(filename: str):
|
|
|
28
28
|
gradient_updates = []
|
|
29
29
|
eigenvalues = []
|
|
30
30
|
for key in f.keys():
|
|
31
|
-
if "
|
|
31
|
+
if "update_" in key:
|
|
32
32
|
weight_matrix = f[key]["params"]["weight_matrix"][()]
|
|
33
33
|
weight_matrix = weight_matrix.reshape(-1, weight_matrix.shape[-1])
|
|
34
34
|
eig = np.linalg.svd(weight_matrix, compute_uv=False)
|
|
@@ -56,7 +56,7 @@ def get_saved_updates(filename: str) -> np.ndarray:
|
|
|
56
56
|
updates = []
|
|
57
57
|
with h5py.File(filename, "r") as f:
|
|
58
58
|
for key in f.keys():
|
|
59
|
-
if "
|
|
59
|
+
if "update_" in key:
|
|
60
60
|
update = int(key.replace("update_", ""))
|
|
61
61
|
updates.append(update)
|
|
62
62
|
return np.sort(np.array(updates))
|
|
@@ -202,7 +202,7 @@ def log_to_csv(logs: dict[str, float], log_file: str) -> None:
|
|
|
202
202
|
|
|
203
203
|
|
|
204
204
|
def compute_log_likelihood(
|
|
205
|
-
v_data: Tensor, w_data: Tensor, params:
|
|
205
|
+
v_data: Tensor, w_data: Tensor, params: EBM, log_z: float
|
|
206
206
|
) -> float:
|
|
207
207
|
"""Compute the log likelihood of the RBM on the data, given its log partition function.
|
|
208
208
|
|
|
@@ -295,7 +295,7 @@ def get_flagged_updates(filename: str, flag: str) -> np.ndarray:
|
|
|
295
295
|
flagged_updates = []
|
|
296
296
|
with h5py.File(filename, "r") as f:
|
|
297
297
|
for key in f.keys():
|
|
298
|
-
if "
|
|
298
|
+
if "update_" in key:
|
|
299
299
|
update = int(key.replace("update_", ""))
|
|
300
300
|
if flag in f[key]["flags"]:
|
|
301
301
|
if f[key]["flags"][flag][()]:
|
|
@@ -13,7 +13,8 @@ pytest.NUM_SAMPLES = 11
|
|
|
13
13
|
pytest.NUM_CHAINS = 13
|
|
14
14
|
pytest.BATCH_SIZE = 17
|
|
15
15
|
pytest.GIBBS_STEPS = 19
|
|
16
|
-
|
|
16
|
+
pytest.SEED = 42
|
|
17
|
+
pytest.TRAIN_SIZE = 0.6
|
|
17
18
|
|
|
18
19
|
pytest.DEVICE_CUDA = "meta"
|
|
19
20
|
pytest.LEARNING_RATE = 0.03
|
|
@@ -148,6 +149,9 @@ def sample_args(tmp_path):
|
|
|
148
149
|
"filename": filename,
|
|
149
150
|
"beta": 1.0,
|
|
150
151
|
"overwrite": True,
|
|
152
|
+
"seed": pytest.SEED,
|
|
153
|
+
"train_size": 0.6,
|
|
154
|
+
"test_size": None,
|
|
151
155
|
}
|
|
152
156
|
|
|
153
157
|
|