Last active
June 24, 2023 07:57
-
-
Save henry0312/c0d84c679ab9583f4500ce1653ebb889 to your computer and use it in GitHub Desktop.
k-fold cross validation with Optuna
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
class LocalObjective(object): | |
def __init__(self, params): | |
self.params = dict(params) | |
def __call__(self, trial): | |
# fit | |
return loss | |
class GlobalObjective(object): | |
def __init__(self, k): | |
self.k = k | |
self.local_studies = [optuna.create_study() for _ in range(k)] | |
def __call__(self, trial): | |
params # sample params | |
for study in self.local_studies: | |
study.optimize(LocalObjective(params), n_trials=1) | |
# store loss | |
# calculate loss | |
return loss | |
def main(): | |
study = optuna.create_study() | |
global_obj = GlobalObjective(k=5) | |
study.optimize(global_obj, n_trials=100) | |
if __name__ == '__main__': | |
main() |
Did any of you get this error?
OSError: [Errno 24] Too many open files
Edit (fixed)
If you use torch.utils.data.DataLoader
do not set the attribute persistent_workers=True
, it will cause the aforementioned error (or any related, for instance if using a optuna.storages.JournalStorage
you may get OSError: [Errno 24] Too many open files: './example.journal'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this code, very interesting repurpose of Optuna mechanisms!