How to use the dlib.function_spec function in dlib

To help you get started, we’ve selected a few dlib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tsoernes / dca / dca / runners / dlib_runner.py View on Github external
f"{prev_best[0]}@{list(zip(saved_params, prev_best[1:]))}")
            # Switching params being optimized over would throw off DLIB.
            # Have to assert that they are equal instead.
            # What happens if you introduce another variable in addition to the previously?
            # E.g. initialize dlib with evals over (eps, beta) then specify bounds for
            # (eps, beta, gamma)?

            # Restore saved params and settings if they differ from current/specified
            if params != saved_params:
                logger.error(
                    f"Saved params {saved_params} differ from currently specified "
                    f"{params}. Using saved.")
                params = saved_params
            raw_spec = cmp_and_choose('bounds', old_raw_spec,
                                      (is_int, lo_bounds, hi_bounds))
            spec = dlib.function_spec(
                bound1=raw_spec[1], bound2=raw_spec[2], is_integer=raw_spec[0])
            eps = cmp_and_choose('solver_epsilon', info['solver_epsilon'], eps)
            noise_mag = cmp_and_choose('relative_noise_magnitude',
                                       info['relative_noise_magnitude'], noise_mag)
            _, pp = compare_pps(info['pp'], pp)
            optimizer = dlib.global_function_search(
                [spec],
                initial_function_evals=[old_evals],
                relative_noise_magnitude=noise_mag)
        except FileNotFoundError:
            spec = dlib.function_spec(
                bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
            optimizer = dlib.global_function_search(spec)
            optimizer.set_relative_noise_magnitude(noise_mag)
        optimizer.set_solver_epsilon(eps)
        # Becomes populated with results as simulations finished
github tsoernes / dca / dca / runner.py View on Github external
# Restore saved params and settings if they differ from current/specified
            saved_params = info['params']
            if saved_params != params:
                self.logger.error(
                    f"Saved params {saved_params} differ from specified ones {params}; using saved"
                )
                # TODO could check if bounds match as well
                params = saved_params
            saved_solver_epsilon = info['solver_epsilon']
            if saved_solver_epsilon != solver_epsilon:
                self.logger.error(
                    f"Saved solver_epsilon {saved_solver_epsilon} differ from"
                    " specified one {solver_epsilon}, using specified")
                # solver_epsilon = saved_solver_epsilon
            _, self.pp = compare_pps(info['pp'], self.pp)
            spec = dlib.function_spec(
                bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
            optimizer = dlib.global_function_search(
                [spec],
                initial_function_evals=[evals],
                relative_noise_magnitude=info['relative_noise_magnitude'])
            self.logger.error(f"Restored {len(evals)} trials, prev best: "
                              f"{prev_best[0]}@{list(zip(saved_params, prev_best[1:]))}")
        except FileNotFoundError:
            spec = dlib.function_spec(
                bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
            optimizer = dlib.global_function_search(spec)
            optimizer.set_relative_noise_magnitude(relative_noise_magnitude)
        optimizer.set_solver_epsilon(solver_epsilon)

        result_queue = Queue()
        simproc = partial(dlib_proc, self.stratclass, self.pp, params, result_queue,
github tsoernes / gfsopt / gfsopt / gfsopt.py View on Github external
if len(params) != len(is_int):
                    raise ValueError(
                        f"Params {params} and spec {raw_spec} are of different length"
                    )
                eps = _cmp_and_choose('solver_epsilon', info['solver_epsilon'], eps)
                noise_mag = _cmp_and_choose(
                    'relative_noise_magnitude', info['relative_noise_magnitude'],
                    noise_mag
                )
                _, pp = _compare_pps(info['pp'], pp)
            except FileNotFoundError:
                # Create a new file
                pass
        eps = 0.0005 if eps is None else eps
        noise_mag = 0.001 if noise_mag is None else noise_mag
        spec = dlib.function_spec(bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
        if old_evals:
            optimizer = dlib.global_function_search(
                [spec],
                initial_function_evals=[old_evals],
                relative_noise_magnitude=noise_mag
            )
        else:
            optimizer = dlib.global_function_search(
                [spec]
            )
            optimizer.set_relative_noise_magnitude(noise_mag)
        optimizer.set_solver_epsilon(eps)

        self.pp, self.params, self.optimizer, self.spec = pp, params, optimizer, spec
        self.eps, self.noise_mag, self.is_int = eps, noise_mag, is_int
        self.fname, self.save = fname, save
github tsoernes / gfsopt / gfsopt / gfsopt.py View on Github external
results[:, 0] is result/loss
        results[:, 1:] is [param1, param2, ...]
      spec: (is_integer, lower, upper)
        where each element is list of length M
      info: dict with keys
        params, solver_epsilon, relative_noise_magnitude, pp

    Assumes only 1 function is optimized over

    Returns
    (dlib.function_spec, [dlib.function_eval], dict, prev_best)
      where prev_best: np.array[result, param1, param2, ...]
    """
    raw_spec, raw_results, info = _load_raw(fname)
    is_integer, lo_bounds, hi_bounds = raw_spec
    spec = dlib.function_spec(bound1=lo_bounds, bound2=hi_bounds, is_integer=is_integer)
    evals = []
    prev_best = raw_results[np.argmax(raw_results, axis=0)[0]]
    for raw_result in raw_results:
        x = list(raw_result[1:])
        result = dlib.function_evaluation(x=x, y=raw_result[0])
        evals.append(result)
    return raw_spec, spec, evals, info, prev_best
github tsoernes / dca / dca / runner.py View on Github external
if saved_solver_epsilon != solver_epsilon:
                self.logger.error(
                    f"Saved solver_epsilon {saved_solver_epsilon} differ from"
                    " specified one {solver_epsilon}, using specified")
                # solver_epsilon = saved_solver_epsilon
            _, self.pp = compare_pps(info['pp'], self.pp)
            spec = dlib.function_spec(
                bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
            optimizer = dlib.global_function_search(
                [spec],
                initial_function_evals=[evals],
                relative_noise_magnitude=info['relative_noise_magnitude'])
            self.logger.error(f"Restored {len(evals)} trials, prev best: "
                              f"{prev_best[0]}@{list(zip(saved_params, prev_best[1:]))}")
        except FileNotFoundError:
            spec = dlib.function_spec(
                bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
            optimizer = dlib.global_function_search(spec)
            optimizer.set_relative_noise_magnitude(relative_noise_magnitude)
        optimizer.set_solver_epsilon(solver_epsilon)

        result_queue = Queue()
        simproc = partial(dlib_proc, self.stratclass, self.pp, params, result_queue,
                          self.pp['avg_runs'])
        evals = [None] * n_sims

        def save():
            finished_evals = optimizer.get_function_evaluations()[1][0]
            dlib_save(spec, finished_evals, params, solver_epsilon,
                      relative_noise_magnitude, self.pp, fname)
            self.logger.error("Saved progress")
github tsoernes / dca / dca / hopt_utils.py View on Github external
results[:, 0] is result/loss
        results[:, 1:] is [param1, param2, ...]
      spec: (is_integer, lower, upper)
        where each element is list of length M
      info: dict with keys
        params, solver_epsilon, relative_noise_magnitude, pp

    Assumes only 1 function is optimized over

    Return
    (dlib.function_spec, [dlib.function_eval], dict, prev_best)
      where prev_best: np.array[result, param1, param2, ...]
    """
    raw_spec, raw_results, info = dlib_load_raw(fname)
    is_integer, lo_bounds, hi_bounds = raw_spec
    spec = dlib.function_spec(bound1=lo_bounds, bound2=hi_bounds, is_integer=is_integer)
    evals = []
    prev_best = raw_results[np.argmax(raw_results, axis=0)[0]]
    for raw_result in raw_results:
        x = list(raw_result[1:])
        result = dlib.function_evaluation(x=x, y=raw_result[0])
        evals.append(result)
    return raw_spec, spec, evals, info, prev_best
github tsoernes / dca / dca / runners / dlib_runner.py View on Github external
f"{params}. Using saved.")
                params = saved_params
            raw_spec = cmp_and_choose('bounds', old_raw_spec,
                                      (is_int, lo_bounds, hi_bounds))
            spec = dlib.function_spec(
                bound1=raw_spec[1], bound2=raw_spec[2], is_integer=raw_spec[0])
            eps = cmp_and_choose('solver_epsilon', info['solver_epsilon'], eps)
            noise_mag = cmp_and_choose('relative_noise_magnitude',
                                       info['relative_noise_magnitude'], noise_mag)
            _, pp = compare_pps(info['pp'], pp)
            optimizer = dlib.global_function_search(
                [spec],
                initial_function_evals=[old_evals],
                relative_noise_magnitude=noise_mag)
        except FileNotFoundError:
            spec = dlib.function_spec(
                bound1=lo_bounds, bound2=hi_bounds, is_integer=is_int)
            optimizer = dlib.global_function_search(spec)
            optimizer.set_relative_noise_magnitude(noise_mag)
        optimizer.set_solver_epsilon(eps)
        # Becomes populated with results as simulations finished
        result_queue = Queue()
        simproc = partial(dlib_proc, self.stratclass, pp, params, result_queue)
        # Becomes populated with evaluation objects to be set later
        evals = [None] * n_sims
        # Becomes populates with losses. When n_avg losses for a particular
        # set of params are ready, their mean is set for the correponding eval.
        results = [[] for _ in range(n_sims)]

        def save_evals():
            """Store results of finished evals to file; print best eval"""
            finished_evals = optimizer.get_function_evaluations()[1][0]