deephyper.nas.KSearchSpace#
- class deephyper.nas.KSearchSpace(input_shape, output_shape, batch_size=None, seed=None, *args, **kwargs)[source]#
Bases:
NxSearchSpace
A KSearchSpace represents a search space of neural networks.
>>> import tensorflow as tf >>> from deephyper.nas import KSearchSpace >>> from deephyper.nas.node import ConstantNode, VariableNode >>> from deephyper.nas.operation import operation, Identity >>> Dense = operation(tf.keras.layers.Dense) >>> Dropout = operation(tf.keras.layers.Dropout)
>>> class ExampleSpace(KSearchSpace): ... def build(self): ... # input nodes are automatically built based on `input_shape` ... input_node = self.input_nodes[0] ... # we want 4 layers maximum (Identity corresponds to not adding a layer) ... for i in range(4): ... node = VariableNode() ... self.connect(input_node, node) ... # we add 3 possible operations for each node ... node.add_op(Identity()) ... node.add_op(Dense(100, "relu")) ... node.add_op(Dropout(0.2)) ... input_node = node ... output = ConstantNode(op=Dense(self.output_shape[0])) ... self.connect(input_node, output) ... return self ... >>>
>>> space = ExampleSpace(input_shape=(1,), output_shape=(1,)).build() >>> space.sample().summary()
- Parameters:
- Raises:
InputShapeOfWrongType – [description]
Methods
Add a new node to the search_space.
Build the current graph search space.
Gives the possible choices for each decision variable of the search space.
Create a new connection in the KSearchSpace graph.
create_model
Create the tensors corresponding to the search_space.
create_tensor_aux
Recursive function to create the tensors from the graph.
Denormalize a sequence of normalized indexes to get a sequence of absolute indexes.
plot
Sample a
tf.keras.Model
from the search space.set_ops
Set the operations for each node of each cell of the search_space.
set_output_node
Set the output node of the search_space.
Attributes
depth
input
longest_path
Returns the maximum number of operations accross all VariableNodes of the struct.
Iterator of MimeNodes of the search_space.
Nodes of the current KSearchSpace.
Returns the number of VariableNodes in the current Structure.
output
Size of the search space define by the search_space
Iterator of VariableNodes of the search_space.
- add_node(node)#
Add a new node to the search_space.
- Parameters:
node (Node) – node to add to the search_space.
- Raises:
TypeError – if ‘node’ is not an instance of Node.
NodeAlreadyAdded – if ‘node’ has already been added to the search_space.
- abstract build()#
Build the current graph search space.
- choices()[source]#
Gives the possible choices for each decision variable of the search space.
- Returns:
A list of tuple where each element corresponds to a discrete variable represented by
(low, high)
.- Return type:
- connect(node1, node2)#
Create a new connection in the KSearchSpace graph.
The edge created corresponds to : node1 -> node2.
- Parameters:
- Raises:
StructureHasACycle – if the new edge is creating a cycle.
- denormalize(indexes)#
Denormalize a sequence of normalized indexes to get a sequence of absolute indexes. Useful when you want to compare the number of different search_spaces.
- Parameters:
indexes (Iterable) – a sequence of normalized indexes.
- Returns:
A list of absolute indexes corresponding to operations choosen with relative indexes of indexes.
- Return type:
- property max_num_ops#
Returns the maximum number of operations accross all VariableNodes of the struct.
- Returns:
maximum number of Operations for a VariableNode in the current Structure.
- Return type:
- property mime_nodes#
Iterator of MimeNodes of the search_space.
- Returns:
iterator of MimeNodes of the search_space.
- Return type:
(Iterator(MimeNode))
- property nodes#
Nodes of the current KSearchSpace.
- Returns:
nodes of the current KSearchSpace.
- Return type:
iterator
- property num_nodes#
Returns the number of VariableNodes in the current Structure.
- Returns:
number of VariableNodes in the current Structure.
- Return type:
- sample(choice=None)[source]#
Sample a
tf.keras.Model
from the search space.- Parameters:
choice (list, optional) – A list of decision for the operations of this search space. Defaults to None, will generate a random sample.
- Returns:
A Tensorflow Keras model.
- Return type:
tf.keras.Model
- property size#
Size of the search space define by the search_space
- property variable_nodes#
Iterator of VariableNodes of the search_space.
- Returns:
generator of VariablesNodes of the search_space.
- Return type:
(Iterator(VariableNode))