-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility to split dict in batches
- Loading branch information
1 parent
728b59b
commit 13ccae5
Showing
2 changed files
with
56 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
Created by Maxim Ziatdinov (email: [email protected]) | ||
""" | ||
|
||
from typing import Union, Dict, Type | ||
from typing import Union, Dict, Type, List | ||
|
||
import jax | ||
import jax.numpy as jnp | ||
|
@@ -51,6 +51,36 @@ def split_in_batches(X_new: Union[onp.ndarray, jnp.ndarray], | |
return X_split | ||
|
||
|
||
def split_dict(data: Dict[str, jnp.ndarray], chunk_size: int | ||
) -> List[Dict[str, jnp.ndarray]]: | ||
"""Splits a dictionary of arrays into a list of smaller dictionaries. | ||
Args: | ||
data: Dictionary containing numpy arrays. | ||
chunk_size: Desired size of the smaller arrays. | ||
Returns: | ||
List of dictionaries with smaller numpy arrays. | ||
""" | ||
|
||
# Get the length of the arrays | ||
N = len(next(iter(data.values()))) | ||
|
||
# Calculate number of chunks | ||
num_chunks = int(onp.ceil(N / chunk_size)) | ||
|
||
# Split the dictionary | ||
result = [] | ||
for i in range(num_chunks): | ||
start_idx = i * chunk_size | ||
end_idx = min((i+1) * chunk_size, N) | ||
|
||
chunk = {key: value[start_idx:end_idx] for key, value in data.items()} | ||
result.append(chunk) | ||
|
||
return result | ||
|
||
|
||
def get_haiku_dict(kernel_params: Dict[str, jnp.ndarray]) -> Dict[str, Dict[str, jnp.ndarray]]: | ||
""" | ||
Extracts weights and biases from viDKL dictionary into a separate | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters