def create_cell_mlp(input_nodes): """Create a cell with convolution. Args: input_nodes (list(Node)): a list of input_nodes for this cell. Returns: Cell: the corresponding cell. """ cell = Cell(input_nodes) n1 = ConstantNode(name='N1') cell.graph.add_edge(input_nodes[0], n1) # fixed input connection n1.set_op(op=Flatten()) n2 = VariableNode('N2') n2.add_op(Identity()) n2.add_op(Dense(units=10)) n2.add_op(Dense(units=50)) n2.add_op(Dense(units=100)) n2.add_op(Dense(units=200)) n2.add_op(Dense(units=250)) n2.add_op(Dense(units=500)) n2.add_op(Dense(units=750)) n2.add_op(Dense(units=1000)) n3 = VariableNode('N3') n3.add_op(Identity()) n3.add_op(Activation(activation='relu')) n3.add_op(Activation(activation='tanh')) n3.add_op(Activation(activation='sigmoid')) n4 = VariableNode('N4') n4.add_op(Identity()) n4.add_op(Dropout(rate=0.5)) n4.add_op(Dropout(rate=0.4)) n4.add_op(Dropout(rate=0.3)) n4.add_op(Dropout(rate=0.2)) n4.add_op(Dropout(rate=0.1)) n4.add_op(Dropout(rate=0.05)) block = Block() block.add_node(n1) block.add_node(n2) block.add_node(n3) block.add_node(n4) block.add_edge(n1, n2) block.add_edge(n2, n3) block.add_edge(n3, n4) cell.add_block(block) cell.set_outputs() return cell
def set_outputs(self, node=None): """Set output node of the current cell. node (Node, optional): Defaults to None will create a Concatenation node for the last axis. """ if node is None: stacked_nodes = self.get_blocks_output() output_node = ConstantNode(name=f'Cell_{self.num}_Output') output_node.set_op( Concatenate(self.graph, output_node, stacked_nodes)) else: output_node = node self.output = output_node
def create_mlp_block(cell, input_node): # first node of block n1 = ConstantNode(op=Dense(1000, tf.nn.relu), name='N1') cell.graph.add_edge(input_node, n1) # fixed input of current block # second node of block n2 = ConstantNode(op=Dense(1000, tf.nn.relu), name='N2') n3 = ConstantNode(op=Dense(1000, tf.nn.relu), name='N3') block = Block() block.add_node(n1) block.add_node(n2) block.add_node(n3) block.add_edge(n1, n2) block.add_edge(n2, n3) return block
def create_structure(input_shape=[(2, ), (2, ), (2, )], output_shape=(1, ), num_cell=8, *args, **kwargs): network = KerasStructure(input_shape, output_shape) #, output_op=AddByPadding) input_nodes = network.input_nodes # CELL 1 cell1 = create_cell_1(input_nodes) network.add_cell(cell1) # CELL Middle inputs_skipco = [ input_nodes, input_nodes[0], input_nodes[1], input_nodes[2], cell1.output ] pred_cell = cell1 n = num_cell for i in range(n): cell_i = Cell(input_nodes + [cell1.output]) block1 = create_mlp_block(cell_i, pred_cell.output) cell_i.add_block(block1) cnode = VariableNode(name='SkipCo') nullNode = ConstantNode(op=Tensor([]), name='None') cnode.add_op(Connect(cell_i.graph, nullNode, cnode)) # SAME for inpt in inputs_skipco: cnode.add_op(Connect(cell_i.graph, inpt, cnode)) block2 = Block() block2.add_node(cnode) cell_i.add_block(block2) # set_cell_output_add(cell2) cell_i.set_outputs() network.add_cell(cell_i) # prep. for next iter inputs_skipco.append(cell_i.output) pred_cell = cell_i # CELL LAST cell_last = Cell([pred_cell.output]) block1 = create_mlp_block(cell_last, pred_cell.output) cell_last.add_block(block1) # set_cell_output_add(cell3) cell_last.set_outputs() network.add_cell(cell_last) return network
def set_ops(self, indexes): """ Set the operations for each node of each cell of the structure. Args: indexes (list): element of list can be float in [0, 1] or int. output_node (ConstantNode): the output node of the Structure. """ cursor = 0 for c in self.struct: num_nodes = c.num_nodes c.set_ops(indexes[cursor:cursor+num_nodes]) cursor += num_nodes self.graph.add_nodes_from(c.graph.nodes()) self.graph.add_edges_from(c.graph.edges()) output_nodes = get_output_nodes(self.graph) if len(output_nodes) == 1: node = ConstantNode(op=Identity(), name='Structure_Output') self.graph.add_node(node) self.graph.add_edge(output_nodes[0], node) else: node = ConstantNode(name='Structure_Output') node.set_op(self.output_op(self.graph, node, output_nodes)) self.output_node = node
def __init__(self, input_shape, output_shape, output_op=None, *args, **kwargs): """A KerasStructure represents a search space of neural networks. Args: input_shape (list(tuple(int))): list of shapes of all inputs output_shape (tuple(int)): shape of output output_op (Operation): operation which merges outputs of cells Raises: RuntimeError: [description] """ self.graph = nx.DiGraph() if type(input_shape) is tuple: # we have only one input tensor here op = Tensor(keras.layers.Input(input_shape, name="input_0")) self.input_nodes = [ConstantNode(op=op, name='Input_0')] elif type(input_shape) is list and all(map(lambda x: type(x) is tuple, input_shape)): # we have a list of input tensors here self.input_nodes = list() for i in range(len(input_shape)): op = Tensor(keras.layers.Input(input_shape[i], name=f"input_{i}")) inode = ConstantNode(op=op, name=f'Input_{i}') self.input_nodes.append(inode) else: raise RuntimeError(f"input_shape must be either of type 'tuple' or 'list(tuple)' but is of type '{type(input_shape)}'!") self.__output_shape = output_shape self.output_node = None self.output_op = Concatenate if output_op is None else output_op self.struct = [] self.map_sh2int = {} self._model = None
def create_structure(input_shape=[(2, ), (2, ), (2, )], output_shape=(1, ), *args, **kwargs): network = KerasStructure(input_shape, output_shape) #, output_op=AddByPadding) input_nodes = network.input_nodes # CELL 1 cell1 = create_cell_1(input_nodes) network.add_cell(cell1) # CELL 2 cell2 = Cell(input_nodes + [cell1.output]) block1 = create_mlp_block(cell2, cell1.output) cell2.add_block(block1) cnode = VariableNode(name='SkipCo') nullNode = ConstantNode(op=Tensor([]), name='None') cnode.add_op(Connect(cell2.graph, nullNode, cnode)) # SAME cnode.add_op(Connect(cell2.graph, input_nodes[0], cnode)) cnode.add_op(Connect(cell2.graph, input_nodes[1], cnode)) cnode.add_op(Connect(cell2.graph, input_nodes[2], cnode)) cnode.add_op(Connect(cell2.graph, cell1.output, cnode)) cnode.add_op(Connect(cell2.graph, input_nodes, cnode)) cnode.add_op(Connect(cell2.graph, [input_nodes[0], input_nodes[1]], cnode)) cnode.add_op(Connect(cell2.graph, [input_nodes[1], input_nodes[2]], cnode)) cnode.add_op(Connect(cell2.graph, [input_nodes[0], input_nodes[2]], cnode)) block2 = Block() block2.add_node(cnode) cell2.add_block(block2) # set_cell_output_add(cell2) cell2.set_outputs() network.add_cell(cell2) # CELL 3 cell3 = Cell([cell2.output]) block1 = create_mlp_block(cell3, cell2.output) cell3.add_block(block1) # set_cell_output_add(cell3) cell3.set_outputs() network.add_cell(cell3) return network
def create_mlp_block(cell, input_node, skipco_inputs): # first block with one node block1 = Block() n1 = VariableNode('N1') create_mlp_node(n1) cell.graph.add_edge(input_node, n1) # fixed input of current block block1.add_node(n1) # second block for skipconnection block2 = Block() nullNode = ConstantNode(op=Tensor([]), name='None') cnode = VariableNode(name='SkipCo') cnode.add_op(Connect(cell.graph, nullNode, cnode)) # SAME for n in skipco_inputs: cnode.add_op(Connect(cell.graph, n, cnode)) block2.add_node(cnode) cell.add_block(block1) cell.add_block(block2) return n1 # return dense to append it in the skipco_inputs list
def create_mlp_block(cell, input_node): block = Block() # first node of block n1 = VariableNode('N1') create_mlp_node(n1) cell.graph.add_edge(input_node, n1) # fixed input of current block block.add_node(n1) # second node of block n2 = VariableNode('N2') create_mlp_node(n2) block.add_node(n2) addNode1 = ConstantNode(name='Merging') addNode1.set_op(AddByPadding()) # edge created here block.add_node(addNode1) block.add_edge(n1, n2) block.add_edge(n2, addNode1) block.add_edge(n1, addNode1) # residual connection n3 = VariableNode('N3') create_mlp_node(n3) block.add_node(n3) block.add_edge(addNode1, n3) addNode2 = ConstantNode(name='Merging') addNode2.set_op(AddByPadding()) # edge created here block.add_node(addNode2) block.add_edge(n3, addNode2) block.add_edge(addNode1, addNode2) cell.add_block(block) return n1
def create_cell_1(input_nodes): """Create a cell with convolution. Args: input_nodes (list(Node)): a list of input_nodes for this cell. Returns: Cell: the corresponding cell. """ cell = Cell(input_nodes) input_dose1 = input_nodes[0] input_rnaseq = input_nodes[1] input_drug1descriptor = input_nodes[2] input_drug1fingerprints = input_nodes[3] def create_block_3_nodes(input_node): # first node of block n1 = VariableNode('N1') create_mlp_node(n1) cell.graph.add_edge(input_node, n1) # fixed input of current block # second node of block n2 = VariableNode('N2') create_mlp_node(n2) # third node of the block n3 = VariableNode('N3') create_mlp_node(n3) block = Block() block.add_node(n1) block.add_node(n2) block.add_node(n3) block.add_edge(n1, n2) block.add_edge(n2, n3) return block, (n1, n2, n3) # BLOCK FOR: dose1 n = ConstantNode(op=Identity(), name='N1', ) cell.graph.add_edge(input_dose1, n) block0 = Block() block0.add_node(n) cell.add_block(block0) # BLOCK FOR: rnaseq block3, _ = create_block_3_nodes(input_rnaseq) cell.add_block(block3) # BLOCK FOR: drug1.descriptor block4, _ = create_block_3_nodes(input_drug1descriptor) cell.add_block(block4) # BLOCK FOR: drug1.fingerprints block5, _ = create_block_3_nodes(input_drug1fingerprints) cell.add_block(block5) # set_cell_output_add(cell) cell.set_outputs() return cell
def set_cell_output_add(cell): addNode = ConstantNode(name='Merging') addNode.set_op(AddByPadding(cell.graph, addNode, cell.get_blocks_output())) cell.set_outputs(node=addNode)
def create_cell_conv(input_nodes): """Create a cell with convolution. Args: input_nodes (list(Node)): a list of input_nodes for this cell. Returns: Cell: the corresponding cell. """ cell = Cell(input_nodes) n1 = ConstantNode(op=Conv1D(filter_size=20, num_filters=128), name='N1') cell.graph.add_edge(input_nodes[0], n1) # fixed input connection n2 = ConstantNode(op=Activation(activation='relu'), name='N2') n3 = ConstantNode(op=MaxPooling1D(pool_size=1, padding='same'), name='N3') n4 = ConstantNode(op=Conv1D(filter_size=10, num_filters=128),name='N4') n5 = ConstantNode(op=Activation(activation='relu'), name='N5') n6 = ConstantNode(op=MaxPooling1D(pool_size=10, padding='same'), name='N6') n7 = ConstantNode(op=Flatten(), name='N7') n8 = ConstantNode(op=Dense(units=200), name='N8') n9 = ConstantNode(op=Activation(activation='relu'), name='N9') n10 = ConstantNode(op=Dropout(rate=0.1), name='N10') n11 = ConstantNode(op=Dense(units=20), name='N11') n12 = ConstantNode(op=Activation(activation='relu'), name='N12') n13 = ConstantNode(op=Dropout(rate=0.1), name='N13') block = Block() block.add_node(n1) block.add_node(n2) block.add_node(n3) block.add_node(n4) block.add_node(n5) block.add_node(n6) block.add_node(n7) block.add_node(n8) block.add_node(n9) block.add_node(n10) block.add_node(n11) block.add_node(n12) block.add_node(n13) block.add_edge(n1, n2) block.add_edge(n2, n3) block.add_edge(n3, n4) block.add_edge(n4, n5) block.add_edge(n5, n6) block.add_edge(n6, n7) block.add_edge(n7, n8) block.add_edge(n8, n9) block.add_edge(n9, n10) block.add_edge(n10, n11) block.add_edge(n11, n12) block.add_edge(n12, n13) cell.add_block(block) cell.set_outputs() return cell