rbms 0.1__py3-none-any.whl

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/classes.py ADDED
@@ -0,0 +1,216 @@
1
+ from abc import ABC, abstractmethod
2
+ from typing import List, Optional, Self
3
+
4
+ import torch
5
+ from torch import Tensor
6
+
7
+ from rbms.dataset.dataset_class import RBMDataset
8
+
9
+
10
+ class RBM(ABC):
11
+ """An abstract class representing the parameters of a RBM."""
12
+
13
+ @abstractmethod
14
+ def __init__(self): ...
15
+
16
+ @abstractmethod
17
+ def __add__(self, other: Self) -> Self:
18
+ """Add the parameters of two RBMs. Useful for interpolation"""
19
+ ...
20
+
21
+ @abstractmethod
22
+ def __mul__(self, other: float) -> Self:
23
+ """Multiplies the parameters of the RBM by a float."""
24
+ ...
25
+
26
+ @abstractmethod
27
+ def sample_visibles(self, chains: dict[str, Tensor], beta: float = 1.0):
28
+ """Sample the visible layer conditionally to the hidden one.
29
+
30
+ Args:
31
+ chains (dict[str, Tensor]): The parallel chains used for sampling.
32
+ beta (float, optional): The inverse temperature. Defaults to 1.0.
33
+
34
+ Returns:
35
+ Chain: The updated chains with sampled hidden states.
36
+ """
37
+ ...
38
+
39
+ @abstractmethod
40
+ def sample_hiddens(self, chains: dict[str, Tensor], beta: float = 1.0):
41
+ """Sample the hidden layer conditionally to the visible one.
42
+
43
+ Args:
44
+ chains (Chain): The parallel chains used for sampling.
45
+ beta (float, optional): The inverse temperature. Defaults to 1.0.
46
+
47
+ Returns:
48
+ Chain: The updated chains with sampled hidden states.
49
+ """
50
+ ...
51
+
52
+ @abstractmethod
53
+ def compute_energy(self, v: Tensor, h: Tensor) -> Tensor:
54
+ """Compute the energy of the RBM on the visible and hidden variables.
55
+
56
+ Args:
57
+ v (Tensor): Visible configurations.
58
+ h (Tensor): Hidden configurations.
59
+
60
+ Returns:
61
+ Tensor: The computed energy.
62
+ """
63
+ ...
64
+
65
+ @abstractmethod
66
+ def compute_energy_visibles(self, v: Tensor) -> Tensor:
67
+ """Returns the marginalized energy of the model computed on the visible configurations
68
+
69
+ Args:
70
+ v (Tensor): Visible configurations
71
+
72
+ Returns:
73
+ Tensor: The computed energy.
74
+ """
75
+ ...
76
+
77
+ @abstractmethod
78
+ def compute_energy_hiddens(self, h: Tensor) -> Tensor:
79
+ """Returns the marginalized energy of the model computed on hidden configurations
80
+
81
+ Args:
82
+ h (Tensor): The computed energy
83
+ """
84
+ ...
85
+
86
+ @abstractmethod
87
+ def init_chains(
88
+ self,
89
+ num_samples: int,
90
+ weights: Optional[Tensor] = None,
91
+ start_v: Optional[Tensor] = None,
92
+ ) -> dict[str, Tensor]:
93
+ """Initialize a Markov chain for the RBM by sampling a uniform distribution on the visible layer
94
+ and sampling the hidden layer according to the visible one.
95
+
96
+ Args:
97
+ num_samples (int): The number of samples to initialize.
98
+ start_v (Tensor, optional): The initial visible states. Defaults to None.
99
+
100
+ Returns:
101
+ Chain: The initialized Markov chain.
102
+
103
+ Notes:
104
+ - If start_v is specified, its number of samples will override the num_samples argument.
105
+ """
106
+ ...
107
+
108
+ @abstractmethod
109
+ def compute_gradient(
110
+ self, data: dict[str, Tensor], chains: dict[str, Tensor], centered: bool = True
111
+ ) -> None:
112
+ """Compute the gradient for each of the parameters and attach it.
113
+
114
+ Args:
115
+ data (Chain): The data state.
116
+ chains (Chain): The parallel chains used for gradient computation.
117
+ centered (bool, optional): Whether to use centered gradients. Defaults to True.
118
+ """
119
+ ...
120
+
121
+ @abstractmethod
122
+ def parameters(self) -> List[Tensor]:
123
+ """Returns a list containing the parameters of the RBM.
124
+
125
+ Returns:
126
+ List[Tensor]: A list containing the weight matrix, visible bias, and hidden bias.
127
+ """
128
+ ...
129
+
130
+ @abstractmethod
131
+ def named_parameters(self) -> dict[str, Tensor]: ...
132
+
133
+ @staticmethod
134
+ @abstractmethod
135
+ def set_named_parameters(named_params: dict[str, Tensor]) -> Self: ...
136
+
137
+ @abstractmethod
138
+ def to(
139
+ self,
140
+ device: Optional[torch.device] = None,
141
+ dtype: Optional[torch.dtype] = None,
142
+ ) -> Self:
143
+ """Move the parameters to the specified device and/or convert them to the specified data type.
144
+
145
+ Args:
146
+ device (Optional[torch.device], optional): The device to move the parameters to.
147
+ Defaults to None.
148
+ dtype (Optional[torch.dtype], optional): The data type to convert the parameters to.
149
+ Defaults to None.
150
+
151
+ Returns:
152
+ RBM: The modified RBM instance.
153
+ """
154
+ ...
155
+
156
+ @abstractmethod
157
+ def clone(
158
+ self, device: Optional[torch.device] = None, dtype: Optional[torch.dtype] = None
159
+ ) -> Self:
160
+ """Create a clone of the RBM instance.
161
+
162
+ Args:
163
+ device (Optional[torch.device], optional): The device for the cloned parameters.
164
+ Defaults to the current device.
165
+ dtype (Optional[torch.dtype], optional): The data type for the cloned parameters.
166
+ Defaults to the current data type.
167
+
168
+ Returns:
169
+ RBM: A new RBM instance with cloned parameters.
170
+ """
171
+ ...
172
+
173
+ @staticmethod
174
+ @abstractmethod
175
+ def init_parameters(
176
+ num_hiddens: int,
177
+ dataset: RBMDataset,
178
+ device: torch.device,
179
+ dtype: torch.dtype,
180
+ var_init: float = 1e-4,
181
+ ) -> Self:
182
+ """Initialize the parameters of the RBM.
183
+
184
+ Args:
185
+ num_hiddens (int): Number of hidden units.
186
+ dataset (RBMDataset): Training dataset.
187
+ device (torch.device): PyTorch device for the parameters.
188
+ dtype (torch.dtype): PyTorch dtype for the parameters.
189
+ var_init (float, optional): Variance of the weight matrix. Defaults to 1e-4.
190
+
191
+ Notes:
192
+ - The number of visible units is induced from the dataset provided.
193
+ - Hidden biases are set to 0.
194
+ - Visible biases are set to the frequencies of the dataset.
195
+ - The weight matrix is initialized with a Gaussian distribution of variance `var_init`.
196
+ """
197
+ ...
198
+
199
+ @abstractmethod
200
+ def num_visibles(self) -> int:
201
+ """Number of visible units"""
202
+ ...
203
+
204
+ @abstractmethod
205
+ def num_hiddens(self) -> int:
206
+ """Number of hidden units"""
207
+ ...
208
+
209
+ @abstractmethod
210
+ def ref_log_z(self) -> float:
211
+ """Reference log partition function with weights set to 0 (except for the visible bias)."""
212
+ ...
213
+
214
+ @abstractmethod
215
+ def independent_model(self) -> Self:
216
+ """Independent model where only local fields are preserved."""
rbms/const.py ADDED
@@ -0,0 +1,4 @@
1
+ import torch
2
+
3
+ LOG_FILE_HEADER = ["empty_col"]
4
+ INT_DTYPE = torch.int32
rbms/custom_fn.py ADDED
@@ -0,0 +1,41 @@
1
+ import torch
2
+ from torch import Tensor
3
+
4
+
5
+ def one_hot(
6
+ x: Tensor, num_classes: int = -1, dtype: torch.dtype = torch.float32
7
+ ) -> Tensor:
8
+ """A one-hot encoding function faster than the PyTorch one working with torch.int32 and returning a float Tensor
9
+
10
+ Args:
11
+ x (Tensor): Input tensor.
12
+ num_classes (int, optional): Number of classes for the one_hot encoding. If negative, then the number of classes is automatically detected.
13
+ dtype (torch.dtype, optional): Dtype of the returned tensor. Defaults to torch.float32
14
+
15
+ Returns
16
+ Tensor: One-hot encoded version of the input tensor.
17
+ """
18
+ if num_classes < 0:
19
+ num_classes = x.max().item() + 1
20
+ res = torch.zeros(x.shape[0], x.shape[1], num_classes, device=x.device, dtype=dtype)
21
+ tmp = torch.meshgrid(
22
+ torch.arange(x.shape[0], device=x.device),
23
+ torch.arange(x.shape[1], device=x.device),
24
+ indexing="ij",
25
+ )
26
+ index = (tmp[0], tmp[1], x)
27
+ values = torch.ones(x.shape[0], x.shape[1], device=x.device, dtype=dtype)
28
+ res.index_put_(index, values)
29
+ return res
30
+
31
+
32
+ def log2cosh(x: Tensor) -> Tensor:
33
+ """Numerically stable version of log(2*cosh(x)).
34
+
35
+ Args:
36
+ x (Tensor): Input tensor.
37
+
38
+ Returns:
39
+ Tensor: Output tensor.
40
+ """
41
+ return torch.abs(x) + torch.log1p(torch.exp(-2 * torch.abs(x)))
@@ -0,0 +1,99 @@
1
+ from pathlib import Path
2
+ from typing import List, Optional
3
+
4
+ import numpy as np
5
+ import torch
6
+
7
+ from rbms.dataset.dataset_class import RBMDataset
8
+ from rbms.dataset.load_fasta import load_FASTA
9
+ from rbms.dataset.load_h5 import load_HDF5
10
+ from rbms.dataset.utils import get_subset_labels
11
+
12
+
13
+ def load_dataset(
14
+ dataset_name: str,
15
+ subset_labels: Optional[List[int]] = None,
16
+ use_weights: bool = False,
17
+ train_size: float = 0.6,
18
+ test_size: Optional[float] = None,
19
+ binarize: bool = True,
20
+ alphabet="protein",
21
+ seed: int = 19023741073419046239412739401234901,
22
+ device: str = "cpu",
23
+ dtype: torch.dtype = torch.float32,
24
+ ):
25
+ rng = np.random.default_rng(seed)
26
+ data = None
27
+ weights = None
28
+ names = None
29
+ labels = None
30
+ is_binary = True
31
+
32
+ dataset_name = Path(dataset_name)
33
+
34
+ match dataset_name.suffix:
35
+ case ".h5":
36
+ data, labels = load_HDF5(filename=dataset_name, binarize=binarize)
37
+ case ".fasta":
38
+ data, weights, names = load_FASTA(
39
+ filename=dataset_name,
40
+ binarize=binarize,
41
+ use_weights=use_weights,
42
+ alphabet=alphabet,
43
+ device=device,
44
+ )
45
+ if not binarize:
46
+ is_binary = False
47
+ case _:
48
+ raise ValueError(
49
+ """
50
+ Dataset could not be loaded as the type is not recognized.
51
+ It should be either:
52
+ - '.h5',
53
+ - '.fasta'
54
+ """
55
+ )
56
+ # Select subset of dataset w.r.t. labels
57
+ if subset_labels is not None and labels is not None:
58
+ data, labels = get_subset_labels(data, labels, subset_labels)
59
+
60
+ if weights is None:
61
+ weights = np.ones(data.shape[0])
62
+ if names is None:
63
+ names = np.arange(data.shape[0])
64
+ if labels is None:
65
+ labels = -np.ones(data.shape[0])
66
+
67
+ # Shuffle dataset
68
+ permutation_index = rng.permutation(data.shape[0])
69
+
70
+ # Split train/test
71
+ train_size = int(train_size * data.shape[0])
72
+ if test_size is not None:
73
+ test_size = int(test_size * data.shape[0])
74
+ else:
75
+ test_size = data.shape[0] - train_size
76
+
77
+ train_dataset = RBMDataset(
78
+ data=data[permutation_index[:train_size]],
79
+ labels=labels[permutation_index[:train_size]],
80
+ weights=weights[permutation_index[:train_size]],
81
+ names=names[permutation_index[:train_size]],
82
+ dataset_name=dataset_name,
83
+ is_binary=is_binary,
84
+ device=device,
85
+ dtype=dtype,
86
+ )
87
+ test_dataset = None
88
+ if test_size > 0:
89
+ test_dataset = RBMDataset(
90
+ data=data[permutation_index[train_size : train_size + test_size]],
91
+ labels=labels[permutation_index[train_size : train_size + test_size]],
92
+ weights=weights[permutation_index[train_size : train_size + test_size]],
93
+ names=names[permutation_index[train_size : train_size + test_size]],
94
+ dataset_name=dataset_name,
95
+ is_binary=is_binary,
96
+ device=device,
97
+ dtype=dtype,
98
+ )
99
+ return train_dataset, test_dataset
@@ -0,0 +1,124 @@
1
+ import gzip
2
+ import textwrap
3
+ from typing import Dict, Union
4
+
5
+ import numpy as np
6
+ import torch
7
+ from torch.utils.data import Dataset
8
+ from tqdm import tqdm
9
+
10
+
11
+ class RBMDataset(Dataset):
12
+ """A dataset class for RBM training and evaluation."""
13
+
14
+ def __init__(
15
+ self,
16
+ data: np.ndarray,
17
+ labels: np.ndarray,
18
+ weights: np.ndarray,
19
+ names: np.ndarray,
20
+ dataset_name: str,
21
+ is_binary: bool,
22
+ device: str = "cuda",
23
+ dtype: torch.dtype = torch.float32,
24
+ ) -> None:
25
+ # names should stay as a np array as its dtype is object
26
+ self.names = names
27
+ self.dataset_name = dataset_name
28
+ self.device = device
29
+ self.dtype = dtype
30
+ self.is_binary = is_binary
31
+ self.data = torch.from_numpy(data).to(device=self.device, dtype=self.dtype)
32
+ # Weights should have shape n_visibles
33
+ self.weights = (
34
+ torch.from_numpy(weights).view(-1).to(device=self.device, dtype=self.dtype)
35
+ )
36
+ # Labels are int
37
+ self.labels = torch.from_numpy(labels).to(device=self.device, dtype=torch.int32)
38
+
39
+ def __len__(self) -> int:
40
+ """Get the number of samples in the dataset.
41
+
42
+ Returns:
43
+ int: The number of samples.
44
+ """
45
+ return self.data.shape[0]
46
+
47
+ def __getitem__(self, index: int) -> Dict[str, Union[np.ndarray, torch.Tensor]]:
48
+ """Get a sample from the dataset.
49
+
50
+ Args:
51
+ index (int): The index of the sample.
52
+
53
+ Returns:
54
+ Dict[str, Union[np.ndarray, torch.Tensor]]: A dictionary containing the sample data, labels, weights, and names.
55
+ """
56
+ return {
57
+ "data": self.data[index],
58
+ "labels": self.labels[index],
59
+ "weights": self.weights[index],
60
+ "names": self.names[index],
61
+ }
62
+
63
+ def __str__(self) -> str:
64
+ """Get a string representation of the dataset.
65
+
66
+ Returns:
67
+ str: The string representation of the dataset.
68
+ """
69
+ return textwrap.dedent(
70
+ f"""
71
+ Dataset: {self.dataset_name}
72
+ Variable type: {"Bernoulli" if self.is_binary else "Potts"}
73
+ Number of samples: {self.data.shape[0]}
74
+ Number of features: {self.data.shape[1]}
75
+ """
76
+ )
77
+
78
+ def get_num_visibles(self) -> int:
79
+ """Get the number of visible units.
80
+
81
+ Returns:
82
+ int: The number of visible units.
83
+ """
84
+ return self.data.shape[1]
85
+
86
+ def get_num_states(self) -> int:
87
+ """Get the number of states.
88
+
89
+ Returns:
90
+ int: The number of states.
91
+ """
92
+ return int(self.data.max() + 1)
93
+
94
+ def get_effective_size(self) -> int:
95
+ """Get the effective size of the dataset.
96
+
97
+ Returns:
98
+ int: The effective size of the dataset.
99
+ """
100
+ return int(self.weights.sum())
101
+
102
+ def get_gzip_entropy(self, mean_size: int = 50, num_samples: int = 100):
103
+ """Compute the gzip entropy of the dataset.
104
+
105
+ Args:
106
+ mean_size (int, optional): The number of samples to average over. Defaults to 50.
107
+ num_samples (int, optional): The number of samples to use for each entropy calculation. Defaults to 100.
108
+
109
+ Returns:
110
+ float: The computed gzip entropy.
111
+ """
112
+
113
+ pbar = tqdm(range(mean_size))
114
+ pbar.set_description("Compute entropy gzip")
115
+ en = np.zeros(mean_size)
116
+ for i in pbar:
117
+ en[i] = len(
118
+ gzip.compress(
119
+ (self.data[torch.randperm(self.data.shape[0])[:num_samples]]).astype(
120
+ int
121
+ )
122
+ )
123
+ )
124
+ return np.mean(en)
@@ -0,0 +1,171 @@
1
+ from pathlib import Path
2
+ from typing import List, Tuple, Union
3
+
4
+ import numpy as np
5
+ import torch
6
+
7
+ ArrayLike = Tuple[np.ndarray, List]
8
+
9
+ TOKENS_PROTEIN = "ACDEFGHIKLMNPQRSTVWY-"
10
+ TOKENS_RNA = "ACGU-"
11
+ TOKENS_DNA = "ACGT-"
12
+
13
+
14
+ def get_tokens(alphabet: str):
15
+ """Load the vocabulary associated to the alphabet type.
16
+ Args:
17
+ alphabet (str): alphabet type (one of 'protein', 'rna', 'dna').
18
+
19
+ Returns:
20
+ str: Vocabulary.
21
+ """
22
+ assert isinstance(alphabet, str), "Argument 'alphabet' must be of type str"
23
+ if alphabet == "protein":
24
+ return TOKENS_PROTEIN
25
+ elif alphabet == "rna":
26
+ return TOKENS_RNA
27
+ elif alphabet == "dna":
28
+ return TOKENS_DNA
29
+ else:
30
+ return alphabet
31
+
32
+
33
+ def encode_sequence(sequence: str, tokens: str) -> np.ndarray:
34
+ """Takes a string sequence in input an returns the numeric encoding.
35
+
36
+ Args:
37
+ sequence (str): Sequence.
38
+ tokens (str): Vocabulary.
39
+
40
+ Returns:
41
+ list: Encoded sequence.
42
+ """
43
+ letter_map = {letter: number for number, letter in enumerate(tokens)}
44
+ return np.array([letter_map[letter] for letter in sequence])
45
+
46
+
47
+ def decode_sequence(sequence: ArrayLike, tokens: str) -> str:
48
+ """Takes a numeric sequence in input an returns the string encoding.
49
+
50
+ Args:
51
+ sequence (ArrayLike): Encoded sequence.
52
+ tokens (str): Vocabulary.
53
+
54
+ Returns:
55
+ list: Decoded sequence.
56
+ """
57
+ return "".join([tokens[aa] for aa in sequence])
58
+
59
+
60
+ def import_from_fasta(fasta_name: Union[str, Path]) -> Tuple[np.ndarray, np.ndarray]:
61
+ """Import data from a fasta file.
62
+
63
+ Args:
64
+ fasta_name (Union[str, Path]): Path to the fasta file.
65
+
66
+ Raises:
67
+ RuntimeError: The file is not in fasta format.
68
+
69
+ Returns:
70
+ Tuple[list, list]: headers, sequences.
71
+ """
72
+ sequences = []
73
+ names = []
74
+ seq = ""
75
+ with open(fasta_name, "r", encoding="utf-8") as f:
76
+ first_line = f.readline()
77
+ if not first_line.startswith(">"):
78
+ raise RuntimeError(f"The file {fasta_name} is not in a fasta format.")
79
+ f.seek(0)
80
+ for line in f:
81
+ if not line.strip():
82
+ continue
83
+ if line.startswith(">"):
84
+ if seq:
85
+ sequences.append(seq)
86
+ header = line[1:].strip().replace(" ", "_")
87
+ names.append(header)
88
+ seq = ""
89
+ else:
90
+ seq += line.strip()
91
+ if seq:
92
+ sequences.append(seq)
93
+ return np.array(names), np.array(sequences)
94
+
95
+
96
+ def write_fasta(
97
+ fname: str,
98
+ headers: ArrayLike,
99
+ sequences: ArrayLike,
100
+ numeric_input: bool = False,
101
+ remove_gaps: bool = False,
102
+ alphabet: str = "protein",
103
+ ):
104
+ """Generate a fasta file with the input sequences.
105
+
106
+ Args:
107
+ fname (str): Name of the output fasta file.
108
+ headers (ArrayLike): List of sequences' headers.
109
+ sequences (ArrayLike): List of sequences.
110
+ numeric_input (bool, optional): Whether the sequences are in numeric (encoded) format or not. Defaults to False.
111
+ remove_gaps (bool, optional): If True, removes the gap from the alignment. Defaults to False.
112
+ alphabet (str, optional): Selects the type of sequences. Possible chooses are ("protein", "rna"). Defaults to "protein".
113
+
114
+ Raises:
115
+ RuntimeError: The alphabet is not a valid choice.
116
+ """
117
+ tokens = get_tokens(alphabet)
118
+
119
+ if numeric_input:
120
+ # Decode the sequences
121
+ seqs_decoded = np.vectorize(decode_sequence, signature="(m), () -> ()")(
122
+ sequences, tokens
123
+ )
124
+ else:
125
+ seqs_decoded = sequences.copy()
126
+ if remove_gaps:
127
+ seqs_decoded = np.vectorize(lambda s: s.replace("-", ""))(seqs_decoded)
128
+
129
+ with open(fname, "w", encoding="utf-8") as f:
130
+ for name_seq, seq in zip(headers, seqs_decoded):
131
+ f.write(">" + name_seq + "\n")
132
+ f.write(seq)
133
+ f.write("\n")
134
+
135
+
136
+ def compute_weights(
137
+ data: ArrayLike, th: float = 0.8, device: torch.device = "cpu"
138
+ ) -> np.ndarray:
139
+ """Computes the weight to be assigned to each sequence 's' in 'data' as 1 / n_clust, where 'n_clust' is the number of sequences
140
+ that have a sequence identity with 's' >= th.
141
+
142
+ Args:
143
+ data (ArrayLike): Encoded input dataset.
144
+ th (float, optional): Sequence identity threshold for the clustering. Defaults to 0.8.
145
+ device (torch.device): Device.
146
+
147
+ Returns:
148
+ np.ndarray: Array with the weights of the sequences.
149
+ """
150
+ device = torch.device(device)
151
+ data = torch.tensor(data, device=device)
152
+ assert len(data.shape) == 2, "'data' must be a 2-dimensional array"
153
+ _, L = data.shape
154
+
155
+ def get_sequence_weight(s: torch.Tensor, data: torch.Tensor, L: int, th: float):
156
+ seq_id = torch.sum(s == data, dim=1) / L
157
+ n_clust = torch.sum(seq_id >= th)
158
+ return 1.0 / n_clust
159
+
160
+ weights = torch.vstack([get_sequence_weight(s, data, L, th) for s in data])
161
+ return weights.cpu().numpy()
162
+
163
+
164
+ def validate_alphabet(sequences: ArrayLike, tokens: str):
165
+ all_char = "".join(sequences)
166
+ tokens_data = "".join(sorted(set(all_char)))
167
+ sorted_tokens = "".join(sorted(tokens))
168
+ if sorted_tokens != tokens_data:
169
+ raise KeyError(
170
+ f"The chosen alphabet is incompatible with the Multi-Sequence Alignment. The missing tokens are: {[c for c in tokens_data if c not in sorted_tokens]}"
171
+ )