Пример #1
0
import hypergraph as hg
import numpy as np

graph1 = hg.Graph()
with graph1.as_default():
    hg.mark("abc1") << (hg.dump() << "** abc1 **")
    n = hg.mark("abc2") << (hg.dump() << "** abc2 **")

    idx = hg.node(lambda _: np.random.randint(0, 2))
    hg.output() << (hg.select(idx) << ["abc1", "abc2"])

for _ in range(3):
    ctx = hg.ExecutionContext()
    with ctx.as_default():
        print(graph1())
    print("*** end of execution ***")
Пример #2
0
    ]
    fit = hg.call(model.fit_generator) << {
        'generator':
        generator,
        'epochs':
        1,
        'max_queue_size':
        hg.tweak(hg.QUniform(low=2, high=64), name='max_queue_size'),
        'workers':
        hg.tweak(hg.QLogUniform(low=1, high=16), name='workers'),
        'callbacks': [time_history],
        'verbose':
        0
    }
    fit << hg.deps(set_batch_size)
    hg.output() << hg.call1(
        lambda _: time_history.get_total_time()) << hg.deps(fit)


def objective(individual):
    try:
        return hg.run(graph1, tweaks=individual)
    except:
        # in case of exception we penalize the maximum
        return np.inf


history = History()
best = hg.optimize(algo='genetic',
                   graph=graph1,
                   objective=objective,
                   callbacks=[ConsoleLog(), history],
Пример #3
0
import hypergraph as hg


def test1(x, y):
    return x + y


graph1 = hg.Graph(name='g1')
with graph1.as_default():
    hg.mark("x_value") << 5
    hg.output() << (
        hg.invoke() << [test1, {
            'x': hg.node_ref("x_value"),
            'y': 3
        }])
    # Note, in a more realistic scenario, the first param of the invoke, that is the function to be invoked,
    # may be the result of another computation or tweak

print(hg.run(graph1))
Пример #4
0
import hypergraph as hg

graph1 = hg.Graph()
with graph1.as_default():
    hg.output() << (hg.delay(units=2) << hg.input_all())

ctx = hg.ExecutionContext()
with ctx.as_default():
    for i in range(5):
        print(graph1(i))
Пример #5
0
import hypergraph as hg


def test1(x, y):
    return x + y


graph1 = hg.Graph()
with graph1.as_default():
    # Note: the dictionary items are automatically mapped to the arguments
    hg.output() << (hg.call(test1) << {
        'x': 2,
        'y': 3
    })  # another possibility is passing arg by position: [2, 3]

print(graph1())
print(graph1())
hg.ExecutionContext.reset_default()
Пример #6
0
import hypergraph as hg
from hypergraph.genetic import GeneticOperators

graph1 = hg.Graph(name="g1")
with graph1.as_default():
    hg.output() << (hg.permutation(size=3) << list('abcdef'))

# create a population from graph's phenotype
genetic = GeneticOperators(graph1)
print(genetic.phenotype)

population = genetic.create_population(3)
print(population[:2])
# crossover two parent to get a new individual
child = genetic.crossover_uniform_multi_parents(population[:2])
print(child)
genetic.mutations(child, prob=0.5)
# apply mutations to an individual
print(child)

# use an individual as graph's tweaks
print(hg.run(graph1, tweaks=child))
Пример #7
0
import hypergraph as hg
from hypergraph.genetic import GeneticOperators


@hg.decl_tweaks(y=hg.tweaks.Uniform())
@hg.decl_tweaks(z=hg.tweaks.Normal(mean=10))
def test1(x, y, z):
    return x + y + z


graph1 = hg.Graph(name='g1')
with graph1.as_default():
    # Note: the dictionary items are automatically mapped to the arguments
    hg.output() << (hg.call(test1) << {'x': 2})

genetic = GeneticOperators(graph1)
print(genetic.phenotype)
tweaks = genetic.create_population(1)[0]
print(tweaks)

ctx = hg.ExecutionContext(tweaks=tweaks)
with ctx.as_default():
    print(graph1())
Пример #8
0
import hypergraph as hg

graph1 = hg.Graph()
with graph1.as_default():
    hg.var('v1', initial_value='test1')
    #hg.output() << (hg.var('v1') << hg.SetVar('test2'))
    hg.output() << (hg.set_var('v1') << 'test2')

ctx = hg.ExecutionContext()
with ctx.as_default():
    print(graph1())

print(ctx.get_var_value(graph1, 'v1'))
Пример #9
0
import hypergraph as hg

graph1 = hg.Graph()
with graph1.as_default():
    d = hg.input_all().as_dict()
    hg.output() << [d.keys(), d.values(), d.items()]

ctx = hg.ExecutionContext()
with ctx.as_default():
    print(graph1({'a': 1, 'b': 2}))