deephyper.nas

class deephyper.nas.KSearchSpace(input_shape, output_shape, batch_size=None, seed=None, *args, **kwargs)[source]

Bases: deephyper.nas._nx_search_space.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
  • input_shape (list(tuple(int))) – list of shapes of all inputs.

  • output_shape (tuple(int)) – shape of output.

  • batch_size (list(tuple(int))) – batch size of the input layer. If input_shape is defining a list of inputs, batch_size should also define a list of inputs.

Raises

InputShapeOfWrongType – [description]

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

list

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

list

property depth
property input
property longest_path
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

int

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

int

property output
plot(path)
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))

class deephyper.nas.NxSearchSpace(seed=None, **kwargs)[source]

Bases: abc.ABC

A NxSearchSpace is an search_space based on a networkx graph.

add_node(node)[source]

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()[source]

Build the current graph search space.

abstract 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

list

connect(node1, node2)[source]

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)[source]

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

list

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

int

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

int

plot(path)[source]
abstract 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))