Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2537 from activeloopai/add_seed_to_random_split
Allow user-control of random seeds
- Loading branch information
Showing
13 changed files
with
133 additions
and
12 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
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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import numpy as np | ||
from typing import Optional | ||
|
||
class DeeplakeRandom(object): | ||
def __new__(cls): | ||
if not hasattr(cls, 'instance'): | ||
cls.instance = super(DeeplakeRandom, cls).__new__(cls) | ||
cls.instance.internal_seed = None | ||
cls.instance.indra_api = None | ||
return cls.instance | ||
|
||
|
||
def seed(self, seed: Optional[int] = None): | ||
if isinstance(seed, Optional[int]): | ||
self.internal_seed = seed | ||
if self.indra_api is None: | ||
from deeplake.enterprise.convert_to_libdeeplake import import_indra_api_silent | ||
self.indra_api = import_indra_api_silent() | ||
if self.indra_api is not None: | ||
self.indra_api.set_seed(self.internal_seed) | ||
else: | ||
raise TypeError(f"provided seed type `{type(seed)}` is increect seed must be an integer") | ||
|
||
def get_seed(self) -> Optional[int]: | ||
return self.internal_seed |
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,8 +1,13 @@ | ||
import numpy as np | ||
from deeplake.core.seed import DeeplakeRandom | ||
|
||
|
||
|
||
def shuffle(ds): | ||
"""Returns a shuffled wrapper of a given Dataset.""" | ||
prev_state = np.random.get_state() | ||
np.random.seed(DeeplakeRandom().get_seed()) | ||
idxs = np.arange(len(ds)) | ||
np.random.shuffle(idxs) | ||
np.random.set_state(prev_state) | ||
return ds[idxs.tolist()] |
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
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