Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_create_multiple_inputs_with_one_vnode(self):
from deephyper.search.nas.model.space.struct import DirectStructure
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 = DirectStructure([(5, ), (5, )], (1, ))
merge = ConstantNode()
merge.set_op(Concatenate(struct, merge, struct.input_nodes))
vnode1 = VariableNode()
struct.connect(merge, vnode1)
vnode1.add_op(Dense(1))
struct.set_ops([0])
struct.create_model()
arch = AutoKSearchSpace(input_shape, output_shape, regression=True)
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 __init__(self, input_shape, output_shape, output_op=None, *args, **kwargs):
super().__init__()
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
arch = AutoKSearchSpace(input_shape, output_shape, regression=True)
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
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 = self.get_output_nodes()
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 set_output_node(self, graph, output_nodes):
"""Set the output node of the search_space.
Args:
graph (nx.DiGraph): graph of the search_space.
output_nodes (Node): nodes of the current search_space without successors.
Returns:
Node: output node of the search_space.
"""
if len(output_nodes) == 1:
node = ConstantNode(op=Identity(), name='Structure_Output')
graph.add_node(node)
graph.add_edge(output_nodes[0], node)
else:
node = ConstantNode(name='Structure_Output')
op = Concatenate(self, output_nodes)
node.set_op(op=op)
return node
def __init__(self, input_shape, output_shape, output_op=None, *args, **kwargs):
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
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], 2), 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)
return struct
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