def test_scope(self): with HyperSpace().as_default(): id1 = Identity() id2 = Identity(name='named_id') id3 = Identity() id4 = Identity(name='named_id_2') assert id1.name == 'Module_Identity_1' assert id1.id == 'Module_Identity_1' assert id2.name == 'named_id' assert id2.id == 'ID_named_id' assert id3.name == 'Module_Identity_2' assert id3.id == 'Module_Identity_2' assert id4.name == 'named_id_2' assert id4.id == 'ID_named_id_2' hp1 = Int(0, 100) hp2 = Real(0, 10.0) hp3 = Choice([1, 2, 3, 4]) assert hp1.name == 'Param_Int_1' assert hp1.id == 'Param_Int_1' assert hp2.name == 'Param_Real_1' assert hp2.id == 'Param_Real_1' assert hp3.name == 'Param_Choice_1' assert hp3.id == 'Param_Choice_1'
def test_basic_ops(self): with HyperSpace().as_default() as space: id1 = HyperInput() id2 = HyperInput() id3 = Identity() id3([id1, id2]) id4 = Identity() id4(id3) id5 = Identity() id5(id3) graph_inputs = space.get_inputs() id3_inputs = space.get_inputs(id3) id4_inputs = space.get_inputs(id4) assert graph_inputs == [id1, id2] assert id3_inputs == [id1, id2] assert id4_inputs == [id3] graph_outputs = space.get_outputs() id1_outputs = space.get_outputs(id1) id3_outputs = space.get_outputs(id3) assert graph_outputs == [id4, id5] assert id1_outputs == [id3] assert id3_outputs == [id4, id5] space.disconnect(id3, id5) id3_outputs = space.get_outputs(id3) assert id3_outputs == [id4] assert len(space.modules) == 5 assert len(space.edges) == 3
def test_traverse_subgraph(self): space = HyperSpace() with space.as_default(): id1 = Identity() id2 = Identity() id3 = Identity()([id1, id2]) id31 = Identity()(id3) id32 = Identity()(id31) id4 = Identity()(id32) id5 = Identity()(id32) id11 = Identity() id31(id11) id_isolated = Identity() start = space.get_sub_graph_inputs(id4) assert start == [id1, id2, id11] id_list = [] def get_id(m): id_list.append(m.id) return True space.traverse(get_id, direction='forward', start_modules=start) assert id_list == ['Module_Identity_1', 'Module_Identity_2', 'Module_Identity_8', 'Module_Identity_3', 'Module_Identity_4', 'Module_Identity_5', 'Module_Identity_6', 'Module_Identity_7'] start = space.get_sub_graph_outputs(id1) assert start == [id4, id5] id_list = [] space.traverse(get_id, direction='backward', start_modules=start) assert id_list == ['Module_Identity_6', 'Module_Identity_7', 'Module_Identity_5', 'Module_Identity_4', 'Module_Identity_3', 'Module_Identity_8', 'Module_Identity_1', 'Module_Identity_2']
def get_space(): space = HyperSpace() with space.as_default(): p1 = Int(1, 100) p2 = Choice(['a', 'b', 'c']) p3 = Bool() p4 = Real(0.0, 1.0) id1 = Identity(p1=p1) id2 = Identity(p2=p2)(id1) id3 = Identity(p3=p3)(id2) id4 = Identity(p4=p4)(id3) return space
def get_space(): space = HyperSpace() with space.as_default(): id1 = Identity(p1=Choice(['a', 'b']), p2=Int(1, 100), p3=Real(0, 1.0)) return space
def get_space_with_dynamic(self): space = HyperSpace() with space.as_default(): input1 = HyperInput() input2 = HyperInput() id1 = Identity(p1=Choice([1, 2]), p2=Int(1, 100)) id2 = Identity(p3=Real(0, 1)) id3 = Identity(p4=Dynamic(lambda p5: p5 * 3, p5=Choice([2, 4, 8]))) def new_module(m): dm1 = Identity(dp1=Choice(['a', 'b'])) return dm1, dm1 id4 = ConnectionSpace(dynamic_fn=new_module, p6=Choice(['f', 'g'])) id5 = Identity() id6 = Identity() id7 = Identity() id1([input1, id7]) id2([input1, input2]) id3([input2, id1, id2]) id4([input1, id2]) id5(input2) id6(id3) id7([id4, id5]) return space
def func_space(func): space = HyperSpace() with space.as_default(): params = { name: copy.copy(v) for name, v in zip(func.__code__.co_varnames, func.__defaults__) if isinstance(v, ParameterSpace) } for _, v in params.items(): v.attach_to_space(space, v.name) input = HyperInput() id1 = Identity(**params)(input) space.set_outputs([id1]) return space
def get_space(self): space = HyperSpace() with space.as_default(): input1 = HyperInput() input2 = HyperInput() id1 = Identity(p1=Choice([1, 2]), p2=Int(1, 100)) id2 = Identity(p3=Real(0, 1)) id3 = Identity(p4=Dynamic(lambda p5: p5 * 3, p5=Choice([2, 4, 8]))) id4 = Identity() id5 = Identity() id6 = Identity() id7 = Identity() id1([input1, id7]) id2([input1, input2]) id3([input2, id1, id2]) id4([input1, id2]) id5(input2) id6(id3) id7([id4, id5]) return space
def get_space(): space = HyperSpace() with space.as_default(): id1 = Identity(p1=Int(0, 10), p2=Choice(['a', 'b'])) id2 = Identity(p3=Real(0., 1.), p4=Bool())(id1) return space
def new_module(m): dm1 = Identity(dp1=Choice(['a', 'b'])) return dm1, dm1
def test_get_subnet_inputs(self): space = HyperSpace() with space.as_default(): id1 = Identity() id2 = Identity()(id1) assert space.get_sub_graph_inputs(id2) == [id1] space = HyperSpace() with space.as_default(): id1 = Identity() id2 = Identity() id3 = Identity()([id1, id2]) assert space.get_sub_graph_inputs(id3) == [id1, id2] space = HyperSpace() with space.as_default(): id1 = Identity() id2 = Identity()(id1) id3 = Identity()(id1) assert space.get_sub_graph_inputs(id3) == [id1] space = HyperSpace() with space.as_default(): id1 = Identity() id2 = Identity()(id1) id3 = Identity()(id2) assert space.get_sub_graph_inputs(id3) == [id1] space = HyperSpace() with space.as_default(): id1 = Identity() id2 = Identity()(id1) id3 = Identity()(id2) id4 = Identity()(id3) assert space.get_sub_graph_inputs(id3) == [id1] assert space.get_sub_graph_inputs(id4) == [id1] space = HyperSpace() with space.as_default(): id1 = Identity() id2 = Identity()(id1) id3 = Identity() id4 = Identity()([id2, id3]) assert space.get_sub_graph_inputs(id4) == [id1, id3] space = HyperSpace() with space.as_default(): id1 = Identity() id2 = Identity() id3 = Identity()([id1, id2]) id4 = Identity()(id3) id5 = Identity()(id3) assert space.get_sub_graph_inputs(id3) == [id1, id2] assert space.get_sub_graph_inputs(id4) == [id1, id2] assert space.get_sub_graph_inputs(id5) == [id1, id2] assert space.get_sub_graph_inputs(id1) == [id1, id2] assert space.get_sub_graph_inputs(id2) == [id1, id2] space = HyperSpace() with space.as_default(): id1 = Identity() id2 = Identity() id3 = Identity()([id1, id2]) id31 = Identity()(id3) id32 = Identity()(id31) id4 = Identity()(id32) id5 = Identity()(id32) assert space.get_sub_graph_inputs(id3) == [id1, id2] assert space.get_sub_graph_inputs(id4) == [id1, id2] assert space.get_sub_graph_inputs(id5) == [id1, id2] assert space.get_sub_graph_inputs(id1) == [id1, id2] assert space.get_sub_graph_inputs(id2) == [id1, id2] space = HyperSpace() with space.as_default(): id1 = Identity() id2 = Identity() id3 = Identity()([id1, id2]) id31 = Identity()(id3) id32 = Identity()(id31) id4 = Identity()(id32) id5 = Identity()(id32) id11 = Identity() id31(id11) assert space.get_sub_graph_inputs(id3) == [id1, id2, id11] assert space.get_sub_graph_inputs(id4) == [id1, id2, id11] assert space.get_sub_graph_inputs(id5) == [id1, id2, id11] assert space.get_sub_graph_inputs(id1) == [id1, id2, id11] assert space.get_sub_graph_inputs(id2) == [id1, id2, id11]