def create_search_space( input_shape=(100, ), output_shape=[(1), (100, )], num_layers=5, **kwargs): struct = KSearchSpace(input_shape, output_shape) inp = struct.input_nodes[0] # auto-encoder units = [128, 64, 32, 16, 8, 16, 32, 64, 128] # units = [32, 16, 32] prev_node = inp d = 1 for i in range(len(units)): vnode = VariableNode() vnode.add_op(Identity()) if d == 1 and units[i] < units[i + 1]: d = -1 # print(min(1, units[i]), ' - ', max(1, units[i])+1) for u in range(min(2, units[i]), max(2, units[i]) + 1, 2): vnode.add_op(Dense(u, tf.nn.relu)) latente_space = vnode else: # print(min(units[i], units[i+d]), ' - ', max(units[i], units[i+d])+1) for u in range(min(units[i], units[i + d]), max(units[i], units[i + d]) + 1, 2): vnode.add_op(Dense(u, tf.nn.relu)) struct.connect(prev_node, vnode) prev_node = vnode out2 = ConstantNode(op=Dense(100, name="output_1")) struct.connect(prev_node, out2) # regressor prev_node = latente_space # prev_node = inp for _ in range(num_layers): vnode = VariableNode() for i in range(16, 129, 16): vnode.add_op(Dense(i, tf.nn.relu)) struct.connect(prev_node, vnode) prev_node = vnode out1 = ConstantNode(op=Dense(1, name="output_0")) struct.connect(prev_node, out1) return struct
def test_create_one_vnode_with_wrong_output_shape(self): from deephyper.search.nas.model.space import KSearchSpace struct = KSearchSpace((5, ), (1, )) from deephyper.search.nas.model.space.node import VariableNode vnode = VariableNode() struct.connect(struct.input_nodes[0], vnode) from deephyper.search.nas.model.space.op.op1d import Dense vnode.add_op(Dense(10)) struct.set_ops([0]) with pytest.raises(WrongOutputShape): struct.create_model()
def create_search_space(input_shape=( 8, 5, ), output_shape=( 8, 5, ), num_layers=10, *args, **kwargs): arch = KSearchSpace(input_shape, output_shape, regression=True) source = prev_input = arch.input_nodes[0] # look over skip connections within a range of the 2 previous nodes anchor_points = collections.deque([source], maxlen=2) for _ in range(num_layers): vnode = VariableNode() add_lstm_seq_(vnode) arch.connect(prev_input, vnode) # * Cell output cell_output = vnode cmerge = ConstantNode() cmerge.set_op(AddByProjecting(arch, [cell_output], activation='relu')) # cmerge.set_op(Concatenate(arch, [cell_output])) for anchor in anchor_points: skipco = VariableNode() skipco.add_op(Tensor([])) skipco.add_op(Connect(arch, anchor)) arch.connect(skipco, cmerge) # ! for next iter prev_input = cmerge anchor_points.append(prev_input) # prev_input = cell_output cnode = ConstantNode() add_lstm_oplayer_(cnode, 5) arch.connect(prev_input, cnode) return arch
def test_create_multiple_inputs_with_wrong_output_shape(self): from deephyper.search.nas.model.space import KSearchSpace from deephyper.search.nas.model.space.node import VariableNode from deephyper.search.nas.model.space.op.op1d import Dense struct = KSearchSpace([(5, ), (5, )], (1, )) struct.set_ops([]) with pytest.raises(WrongOutputShape): struct.create_model()
def test_create_one_vnode(self): from deephyper.search.nas.model.space import KSearchSpace struct = KSearchSpace((5, ), (1, )) from deephyper.search.nas.model.space.node import VariableNode vnode = VariableNode() struct.connect(struct.input_nodes[0], vnode) from deephyper.search.nas.model.space.op.op1d import Dense vnode.add_op(Dense(1)) struct.set_ops([0]) falias = 'test_keras_search_spaceure' struct.draw_graphviz(f'{falias}.dot') model = struct.create_model() from tensorflow.keras.utils import plot_model plot_model(model, to_file=f'{falias}.png', show_shapes=True)
def test_create_multiple_inputs_with_one_vnode(self): from deephyper.search.nas.model.space import KSearchSpace from deephyper.search.nas.model.space.node import VariableNode, ConstantNode from deephyper.search.nas.model.space.op.op1d import Dense from deephyper.search.nas.model.space.op.merge import Concatenate struct = KSearchSpace([(5, ), (5, )], (1, )) merge = ConstantNode() merge.set_op(Concatenate(struct, struct.input_nodes)) vnode1 = VariableNode() struct.connect(merge, vnode1) vnode1.add_op(Dense(1)) struct.set_ops([0]) struct.create_model()
def create_search_space( input_shape=(2, ), output_shape=(3, ), *args, **kwargs): ss = KSearchSpace(input_shape, output_shape) x = ss.input_nodes[0] out_xor = ConstantNode(op=Dense(1), name="XOR") ss.connect(x, out_xor) out_and = ConstantNode(op=Dense(1), name="AND") ss.connect(x, out_and) out_or = ConstantNode(op=Dense(1), name="OR") ss.connect(x, out_or) out = ConstantNode(name="OUT") out.set_op(Concatenate(ss, stacked_nodes=[out_xor, out_and, out_or])) return ss
def create_search_space(input_shape=(2,), output_shape=(3,), *args, **kwargs): ss = KSearchSpace(input_shape, output_shape) x = ss.input_nodes[0] # z = ConstantNode(op=Dense(4, activation="relu"), name="Z") # ss.connect(x, z) # x = z out_xor = ConstantNode(op=Dense(1, activation="sigmoid"), name="out_XOR") out_and = ConstantNode(op=Dense(1, activation="sigmoid"), name="out_AND") out_or = ConstantNode(op=Dense(1, activation="sigmoid"), name="out_OR") in_xor = VariableNode(name="in_XOR") in_xor.add_op(Concatenate(ss, [x])) # 0 in_xor.add_op(Concatenate(ss, [x, out_and])) # 1 in_xor.add_op(Concatenate(ss, [x, out_or])) # 2 in_xor.add_op(Concatenate(ss, [x, out_and, out_or])) # 3 ss.connect(in_xor, out_xor) in_and = VariableNode(name="in_AND") in_and.add_op(Concatenate(ss, [x])) in_and.add_op(Concatenate(ss, [x, out_xor])) in_and.add_op(Concatenate(ss, [x, out_or])) in_and.add_op(Concatenate(ss, [x, out_xor, out_or])) ss.connect(in_and, out_and) in_or = VariableNode(name="in_OR") in_or.add_op(Concatenate(ss, [x])) in_or.add_op(Concatenate(ss, [x, out_xor])) in_or.add_op(Concatenate(ss, [x, out_and])) in_or.add_op(Concatenate(ss, [x, out_xor, out_and])) ss.connect(in_or, out_or) out = ConstantNode(name="OUT") out.set_op(Concatenate(ss, stacked_nodes=[out_xor, out_and, out_or])) return ss
def create_search_space( input_shape=[(10,), (10,)], output_shape=(10,), num_layers=10, *args, **kwargs ): ss = KSearchSpace(input_shape, output_shape) input1 = ss.input_nodes[0] input2 = ss.input_nodes[1] output_nodes = [] for source = prev_input = arch.input_nodes[0] # look over skip connections within a range of the 3 previous nodes anchor_points = collections.deque([source], maxlen=3) for _ in range(num_layers): vnode = VariableNode() add_dense_to_(vnode) arch.connect(prev_input, vnode) # * Cell output cell_output = vnode cmerge = ConstantNode() cmerge.set_op(AddByProjecting(arch, [cell_output], activation="relu")) for anchor in anchor_points: skipco = VariableNode() skipco.add_op(Tensor([])) skipco.add_op(Connect(arch, anchor)) arch.connect(skipco, cmerge) # ! for next iter prev_input = cmerge anchor_points.append(prev_input) return arch
def create_search_space(input_shape=None, output_shape=None, num_mpnn_cells=3, num_dense_layers=2, **kwargs): """Create a search space containing multiple Keras architectures Args: input_shape (list): the input shapes, e.g. [(3, 4), (5, 2)]. output_shape (tuple): the output shape, e.g. (12, ). num_mpnn_cells (int): the number of MPNN cells. num_dense_layers (int): the number of Dense layers. Returns: A search space containing multiple Keras architectures """ data = kwargs['data'] if data == 'qm7': input_shape = [(8 + 1, 75), (8 + 1 + 10 + 1, 2), (8 + 1 + 10 + 1, 14), (8 + 1, ), (8 + 1 + 10 + 1, )] output_shape = (1, ) elif data == 'qm8': input_shape = [(9 + 1, 75), (9 + 1 + 14 + 1, 2), (9 + 1 + 14 + 1, 14), (9 + 1, ), (9 + 1 + 14 + 1, )] output_shape = (16, ) elif data == 'qm9': input_shape = [(9 + 1, 75), (9 + 1 + 16 + 1, 2), (9 + 1 + 16 + 1, 14), (9 + 1, ), (9 + 1 + 16 + 1, )] output_shape = (12, ) elif data == 'freesolv': input_shape = [(24 + 1, 75), (24 + 1 + 25 + 1, 2), (24 + 1 + 25 + 1, 14), (24 + 1, ), (24 + 1 + 25 + 1, )] output_shape = (1, ) elif data == 'esol': input_shape = [(55 + 1, 75), (55 + 1 + 68 + 1, 2), (55 + 1 + 68 + 1, 14), (55 + 1, ), (55 + 1 + 68 + 1, )] output_shape = (1, ) elif data == 'lipo': input_shape = [(115 + 1, 75), (115 + 1 + 236 + 1, 2), (115 + 1 + 236 + 1, 14), (115 + 1, ), (115 + 1 + 236 + 1, )] output_shape = (1, ) arch = KSearchSpace(input_shape, output_shape, regression=True) source = prev_input = arch.input_nodes[0] prev_input1 = arch.input_nodes[1] prev_input2 = arch.input_nodes[2] prev_input3 = arch.input_nodes[3] prev_input4 = arch.input_nodes[4] # look over skip connections within a range of the 3 previous nodes anchor_points = collections.deque([source], maxlen=3) count_gcn_layers = 0 count_dense_layers = 0 for _ in range(num_mpnn_cells): graph_attn_cell = VariableNode() mpnn_cell(graph_attn_cell) # arch.connect(prev_input, graph_attn_cell) arch.connect(prev_input1, graph_attn_cell) arch.connect(prev_input2, graph_attn_cell) arch.connect(prev_input3, graph_attn_cell) arch.connect(prev_input4, graph_attn_cell) cell_output = graph_attn_cell cmerge = ConstantNode() cmerge.set_op(AddByProjecting(arch, [cell_output], activation="relu")) for anchor in anchor_points: skipco = VariableNode() skipco.add_op(Tensor([])) skipco.add_op(Connect(arch, anchor)) arch.connect(skipco, cmerge) prev_input = cmerge anchor_points.append(prev_input) count_gcn_layers += 1 global_pooling_node = VariableNode() gather_cell(global_pooling_node) arch.connect(prev_input, global_pooling_node) prev_input = global_pooling_node flatten_node = ConstantNode() flatten_node.set_op(Flatten()) arch.connect(prev_input, flatten_node) prev_input = flatten_node for _ in range(num_dense_layers): dense_node = ConstantNode() dense_node.set_op(Dense(32, activation='relu')) arch.connect(prev_input, dense_node) prev_input = dense_node count_dense_layers += 1 output_node = ConstantNode() output_node.set_op(Dense(output_shape[0], activation='linear')) arch.connect(prev_input, output_node) return arch
def create_search_space(input_shape = None, output_shape = None, num_mpnn_layers=3, num_dense_layers=2, **kwargs): """ A function to create keras search sapce Args: input_shape: list of tuples output_shape: a tuple num_mpnn_layers: int, number of graph massage passing neural network layers num_dense_layers: int, number of dense layers **kwargs: data: str, the dataset name Returns: arch: keras architecture """ data = kwargs['data'] if data == 'qm7': input_shape = [(9+1, 75), (9+1+16+1, 2), (9+1+16+1, 14), (9+1, ), (9+1+16+1, )] output_shape = (1, ) arch = KSearchSpace(input_shape, output_shape, regression=True) source = prev_input = arch.input_nodes[0] prev_input1 = arch.input_nodes[1] prev_input2 = arch.input_nodes[2] prev_input3 = arch.input_nodes[3] prev_input4 = arch.input_nodes[4] # look over skip connections within a range of the 3 previous nodes anchor_points = collections.deque([source], maxlen=3) count_mpnn_layers = 0 count_dense_layers = 0 for _ in range(num_mpnn_layers): graph_attn_cell = VariableNode() add_mpnn_to_(graph_attn_cell) # arch.connect(prev_input, graph_attn_cell) arch.connect(prev_input1, graph_attn_cell) arch.connect(prev_input2, graph_attn_cell) arch.connect(prev_input3, graph_attn_cell) arch.connect(prev_input4, graph_attn_cell) cell_output = graph_attn_cell cmerge = ConstantNode() cmerge.set_op(AddByProjecting(arch, [cell_output], activation="relu")) for anchor in anchor_points: skipco = VariableNode() skipco.add_op(Tensor([])) skipco.add_op(Connect(arch, anchor)) arch.connect(skipco, cmerge) prev_input = cmerge anchor_points.append(prev_input) count_mpnn_layers += 1 global_pooling_node = VariableNode() add_global_pooling_to_(global_pooling_node) arch.connect(prev_input, global_pooling_node) # result from graph conv (?, 23, ?) --> Global pooling (?, 23) prev_input = global_pooling_node flatten_node = ConstantNode() flatten_node.set_op(Flatten()) arch.connect(prev_input, flatten_node) # result from graph conv (?, 23) --> Flatten prev_input = flatten_node for _ in range(num_dense_layers): dense_node = ConstantNode() dense_node.set_op(Dense(32, activation='relu')) arch.connect(prev_input, dense_node) prev_input = dense_node count_dense_layers += 1 output_node = ConstantNode() output_node.set_op(Dense(output_shape[0], activation='linear')) arch.connect(prev_input, output_node) return arch
def test_create(self): from deephyper.search.nas.model.space import KSearchSpace KSearchSpace((5, ), (1, ))
def create_search_space( input_shape=(2, ), output_shape=(5, ), *args, **kwargs): ss = KSearchSpace(input_shape, output_shape) x = ss.input_nodes[0] nunits = 10 hid_2 = ConstantNode(op=Dense(nunits, "relu"), name="hid_2") out_2 = ConstantNode(op=Dense(1), name="out_2") ss.connect(hid_2, out_2) hid_3 = ConstantNode(op=Dense(nunits, "relu"), name="hid_3") out_3 = ConstantNode(op=Dense(1), name="out_3") ss.connect(hid_3, out_3) hid_4 = ConstantNode(op=Dense(nunits, "relu"), name="hid_4") out_4 = ConstantNode(op=Dense(1), name="out_4") ss.connect(hid_4, out_4) hid_5 = ConstantNode(op=Dense(nunits, "relu"), name="hid_5") out_5 = ConstantNode(op=Dense(1), name="out_5") ss.connect(hid_5, out_5) hid_6 = ConstantNode(op=Dense(nunits, "relu"), name="hid_6") out_6 = ConstantNode(op=Dense(1), name="out_6") ss.connect(hid_6, out_6) # L1 DEPENDENT ON DATA OVERALL in_2 = VariableNode(name="in_2") in_2.add_op(Concatenate(ss, [x])) in_2.add_op(Concatenate(ss, [x, out_3])) in_2.add_op(Concatenate(ss, [x, out_4])) in_2.add_op(Concatenate(ss, [x, out_5])) in_2.add_op(Concatenate(ss, [x, out_6])) ss.connect(in_2, hid_2) # L2 DEPENDANT ON DATA, L1 AND L3 in_3 = VariableNode(name="in_3") in_3.add_op(Concatenate(ss, [x, out_2])) in_3.add_op(Concatenate(ss, [x, out_4])) ss.connect(in_3, hid_3) # L3 DEPENDANT ON DATA, L1 L2 AND WIDTH in_4 = VariableNode(name="in_4") in_4.add_op(Concatenate(ss, [x])) in_4.add_op(Concatenate(ss, [x, out_2])) in_4.add_op(Concatenate(ss, [x, out_3])) in_4.add_op(Concatenate(ss, [x, out_5])) in_4.add_op(Concatenate(ss, [x, out_6])) in_4.add_op(Concatenate(ss, [x, out_2, out_3])) in_4.add_op(Concatenate(ss, [x, out_2, out_5])) in_4.add_op(Concatenate(ss, [x, out_2, out_6])) in_4.add_op(Concatenate(ss, [x, out_3, out_5])) in_4.add_op(Concatenate(ss, [x, out_3, out_6])) in_4.add_op(Concatenate(ss, [x, out_5, out_6])) in_4.add_op(Concatenate(ss, [x, out_3, out_5, out_6])) in_4.add_op(Concatenate(ss, [x, out_2, out_5, out_6])) in_4.add_op(Concatenate(ss, [x, out_2, out_3, out_6])) in_4.add_op(Concatenate(ss, [x, out_2, out_3, out_5])) in_4.add_op(Concatenate(ss, [x, out_2, out_3, out_5, out_6])) ss.connect(in_4, hid_4) # HEIGHT DEPENDANT ON ALL MEASURES COMBINED AND DATA in_5 = VariableNode(name="in_5") in_5.add_op(Concatenate(ss, [x])) in_5.add_op(Concatenate(ss, [x, out_2])) in_5.add_op(Concatenate(ss, [x, out_3])) in_5.add_op(Concatenate(ss, [x, out_4])) in_5.add_op(Concatenate(ss, [x, out_6])) in_5.add_op(Concatenate(ss, [x, out_2, out_3])) in_5.add_op(Concatenate(ss, [x, out_2, out_4])) in_5.add_op(Concatenate(ss, [x, out_2, out_6])) in_5.add_op(Concatenate(ss, [x, out_3, out_4])) in_5.add_op(Concatenate(ss, [x, out_3, out_6])) in_5.add_op(Concatenate(ss, [x, out_4, out_6])) in_5.add_op(Concatenate(ss, [x, out_3, out_4, out_6])) in_5.add_op(Concatenate(ss, [x, out_2, out_4, out_6])) in_5.add_op(Concatenate(ss, [x, out_2, out_3, out_6])) in_5.add_op(Concatenate(ss, [x, out_2, out_3, out_4])) in_5.add_op(Concatenate(ss, [x, out_2, out_3, out_4, out_6])) ss.connect(in_5, hid_5) # WIDTH DEPENDANT ON DATA AND CROSS SECTION in_6 = VariableNode(name="in_6") in_6.add_op(Concatenate(ss, [x])) in_6.add_op(Concatenate(ss, [x, out_2])) in_6.add_op(Concatenate(ss, [x, out_3])) in_6.add_op(Concatenate(ss, [x, out_4])) in_6.add_op(Concatenate(ss, [x, out_5])) ss.connect(in_6, hid_6) out = ConstantNode(name="OUT") out.set_op( Concatenate(ss, stacked_nodes=[out_2, out_3, out_4, out_5, out_6])) return ss