Spaces:
Build error
Build error
File size: 2,182 Bytes
783053f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
from typing import *
import numpy as np
import random
def get_random_sample(search_space: dict, p: Union[List[float], None] = None):
"""Recuperate a random sample
Args:
search_space (dict): A dictionary defining the search space
Raises:
ValueError: 'min' and 'max' can only be numbers
KeyError: Only the following keys can be provided {'min', 'max'}, {'value'}, {'values'} or {'values', 'p'}
Returns:
Union[int, float, str]: The random sample
"""
keys = set(search_space)
if keys == set(['min', 'max']):
assert search_space['min'] < search_space['max']
if isinstance(search_space['min'], int) and isinstance(search_space['max'], int):
return random.randint(search_space['min'], search_space['max'])
elif isinstance(search_space['min'], float) or isinstance(search_space, float):
return random.uniform(search_space['min'], search_space['max'])
else:
raise ValueError("You can only provide int or float values with min max!")
elif keys == set(['value']):
return search_space['value']
elif keys.issubset(set(['values'])):
p = None
if 'p' in keys: p = search_space['p']
return np.random.choice(search_space['values'], size = (1), p = p)[0]
else:
raise KeyError("You didn't provide right keys! Try between: {'min', 'max'}, {'value'}, {'values'} or {'values', 'p'}")
def get_random_samples(search_spaces: dict):
"""Recuperate random samples from a dictionary of search spaces
Args:
search_spaces (dict): A dictionary where the keys are the hyperparameter names and the values are the search spaces
Returns:
dict: A dictionary where the keys are the hyperparameter names and the values are the sampled values from the search spaces
"""
samples = {}
for search_space in search_spaces:
samples[search_space] = get_random_sample(search_spaces[search_space])
return samples
|