pg.hyper.DynamicEvaluationContext

Accessible via pg.hyper.DynamicEvaluationContext.

class DynamicEvaluationContext(where=None, require_hyper_name=False, per_thread=True, dna_spec=None)[source]

Bases: object

Context for dynamic evaluation of hyper primitives.

Example:

import pyglove as pg

# Define a function that implicitly declares a search space.
def foo():
  return pg.oneof(range(-10, 10)) ** 2 + pg.oneof(range(-10, 10)) ** 2

# Define the search space by running the `foo` once.
search_space = pg.hyper.DynamicEvaluationContext()
with search_space.collect():
  _ = foo()

# Create a search algorithm.
search_algorithm = pg.evolution.regularized_evolution(
    pg.evolution.mutators.Uniform(), population_size=32, tournament_size=16)

# Define the feedback loop.
best_foo, best_reward = None, None
for example, feedback in pg.sample(
    search_space, search_algorithm, num_examples=100):
  # Call to `example` returns a context manager
  # under which the `program` is connected with
  # current search algorithm decisions.
  with example():
    reward = foo()
  feedback(reward)
  if best_reward is None or best_reward < reward:
    best_foo, best_reward = example, reward

Methods:

add_decision_point(hyper_primitive)

Registers a parameter with current context and return its first value.

apply(decisions)

Context manager for applying decisions.

collect()

A context manager for collecting hyper primitives within this context.

evaluate(hyper_primitive)

Evaluates a hyper primitive based on current decisions.

Attributes:

dna_spec

Returns the DNASpec of the search space defined so far.

hyper_dict

Returns collected hyper primitives as a dict.

is_external

Returns True if the search space is defined by an external DNASpec.

per_thread

Returns True if current context collects/applies decisions per thread.

add_decision_point(hyper_primitive)[source]

Registers a parameter with current context and return its first value.

apply(decisions)

Context manager for applying decisions.

Example:

def fun():
  return pg.oneof([1, 2, 3]) + pg.oneof([4, 5, 6])

context = DynamicEvaluationContext()
with context.collect():
  fun()

with context.apply([0, 1]):
  # Will print 6 (1 + 5).
  print(fun())
Parameters:

decisions – A DNA or a list of numbers or strings as decisions for currrent search space.

Yields:

None

collect()

A context manager for collecting hyper primitives within this context.

Example:

context = DynamicEvaluationContext()
with context.collect():
  x = pg.oneof([1, 2, 3]) + pg.oneof([4, 5, 6])

# Will print 1 + 4 = 5. Meanwhile 2 hyper primitives will be registered
# in the search space represented by the context.
print(x)
Yields:

The hyper dict representing the search space.

property dna_spec: DNASpec[source]

Returns the DNASpec of the search space defined so far.

evaluate(hyper_primitive)[source]

Evaluates a hyper primitive based on current decisions.

property hyper_dict: Dict | None[source]

Returns collected hyper primitives as a dict.

None if current context is controlled by an external DNASpec.

property is_external: bool[source]

Returns True if the search space is defined by an external DNASpec.

property per_thread: bool[source]

Returns True if current context collects/applies decisions per thread.