def build_sub_graph(self, input_, num_layers=3): source = prev_input = input_ mirror = False is_input = False if type(source) is ConstantNode: if type(source._op) is Tensor: if "input_" in source._op.tensor.name: is_input = True input_name = source._op.tensor.name input_shape = tuple(source._op.tensor.shape[1:]) if self.shapes_to_vnodes.get(input_shape) is None: self.shapes_to_vnodes[input_shape] = [] else: mirror = True memory = self.shapes_to_vnodes[input_shape][::-1] # look over skip connections within a range of the 3 previous nodes anchor_points = collections.deque([source], maxlen=3) for layer_i in range(num_layers): if not(mirror): vnode = VariableNode() self.add_dense_to_(vnode) if is_input: self.shapes_to_vnodes[input_shape].append(vnode) else: vnode = MirrorNode(memory.pop()) self.ss.connect(prev_input, vnode) # * Cell output prev_node = vnode if layer_i == num_layers-1: return prev_node cmerge = ConstantNode() cmerge.set_op(Concatenate(self.ss, [prev_node])) for anchor in anchor_points: if not(mirror): skipco = VariableNode() if is_input: self.shapes_to_vnodes[input_shape].append(skipco) else: skipco = MimeNode(memory.pop()) skipco.add_op(Zero()) skipco.add_op(Connect(self.ss, anchor)) self.ss.connect(skipco, cmerge) prev_input = cmerge # ! for next iter anchor_points.append(prev_input) return prev_input
def generate_cell(ss, hidden_states, num_blocks=5, strides=1, mime=False): anchor_points = [h for h in hidden_states] boutputs = [] for _ in range(num_blocks): bout = generate_block(ss, anchor_points, strides=1, mime=mime) anchor_points.append(bout) boutputs.append(bout) concat = ConstantNode(op=Concatenate(ss, boutputs, not_connected=True)) return concat
def create_structure(input_shape=[(1, ), (942, ), (5270, ), (2048, )], output_shape=(1, ), num_cells=2, *args, **kwargs): struct = AutoKSearchSpace(input_shape, output_shape, regression=True) input_nodes = struct.input_nodes output_submodels = [input_nodes[0]] for i in range(1, 4): cnode1 = ConstantNode(name='N', op=Dense(1000, tf.nn.relu)) struct.connect(input_nodes[i], cnode1) cnode2 = ConstantNode(name='N', op=Dense(1000, tf.nn.relu)) struct.connect(cnode1, cnode2) vnode1 = VariableNode(name='N3') add_mlp_op_(vnode1) struct.connect(cnode2, vnode1) output_submodels.append(vnode1) merge1 = ConstantNode(name='Merge') # merge1.set_op(Concatenate(struct, merge1, output_submodels)) merge1.set_op(Concatenate(struct, output_submodels)) cnode4 = ConstantNode(name='N', op=Dense(1000, tf.nn.relu)) struct.connect(merge1, cnode4) prev = cnode4 for i in range(num_cells): cnode = ConstantNode(name='N', op=Dense(1000, tf.nn.relu)) struct.connect(prev, cnode) merge = ConstantNode(name='Merge') # merge.set_op(AddByPadding(struct, merge, [cnode, prev])) merge.set_op(AddByPadding(struct, [cnode, prev])) prev = merge return struct
def test_create_multiple_inputs_with_one_vnode(self): from deephyper.nas.space import KSearchSpace from deephyper.nas.space.node import VariableNode, ConstantNode from deephyper.nas.space.op.op1d import Dense from deephyper.nas.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_structure(input_shape=[(1, ), (942, ), (5270, ), (2048, )], output_shape=(1,), num_cells=2, *args, **kwargs): struct = AutoKSearchSpace(input_shape, output_shape, regression=True) input_nodes = struct.input_nodes output_submodels = [input_nodes[0]] for i in range(1, 4): vnode1 = VariableNode('N1') add_mlp_op_(vnode1) struct.connect(input_nodes[i], vnode1) vnode2 = VariableNode('N2') add_mlp_op_(vnode2) struct.connect(vnode1, vnode2) vnode3 = VariableNode('N3') add_mlp_op_(vnode3) struct.connect(vnode2, vnode3) output_submodels.append(vnode3) merge1 = ConstantNode(name='Merge', op=Concatenate(struct, output_submodels)) # merge1.set_op(Concatenate(struct, merge1, output_submodels)) vnode4 = VariableNode('N4') add_mlp_op_(vnode4) struct.connect(merge1, vnode4) prev = vnode4 for i in range(num_cells): vnode = VariableNode(f'N{i+1}') add_mlp_op_(vnode) struct.connect(prev, vnode) merge = ConstantNode(name='Merge', op=AddByPadding(struct, [vnode, prev])) # merge.set_op() prev = merge return struct
def generate_cell(ss, hidden_states, num_blocks=5, strides=1, mime=False, num_filters=8): anchor_points = [h for h in hidden_states] boutputs = [] for i in range(num_blocks): bout = generate_block(ss, anchor_points, strides=1, mime=mime, first=i == 0, num_filters=num_filters) anchor_points.append(bout) boutputs.append(bout) concat = ConstantNode(op=Concatenate(ss, boutputs)) return concat
def build(self, input_shape, output_shape, regression=True, **kwargs): ss = AutoKSearchSpace(input_shape, output_shape, regression=regression) if type(input_shape) is list: vnodes = [] for i in range(len(input_shape)): vn = self.gen_vnode() vnodes.append(vn) ss.connect(ss.input_nodes[i], vn) cn = ConstantNode() cn.set_op(Concatenate(ss, vnodes)) vn = self.gen_vnode() ss.connect(cn, vn) else: vnode1 = self.gen_vnode() ss.connect(ss.input_nodes[0], vnode1) return ss
def build( self, input_shape, output_shape, regression=True, num_layers=10, **kwargs, ): self.ss = AutoKSearchSpace(input_shape, output_shape, regression=regression) self.shapes_to_vnodes = {} sub_graphs_outputs = [] for input_ in self.ss.input_nodes: output_sub_graph = self.build_sub_graph(input_) sub_graphs_outputs.append(output_sub_graph) cmerge = ConstantNode() cmerge.set_op(Concatenate(self.ss, sub_graphs_outputs)) output_sub_graph = self.build_sub_graph(cmerge) return self.ss