deephyper.nas.space package

Subpackages

Submodules

deephyper.nas.space.auto_keras_search_space module

class deephyper.nas.space.auto_keras_search_space.AutoKSearchSpace(input_shape, output_shape, regression: bool, *args, **kwargs)[source]

Bases: deephyper.nas.space.keras_search_space.KSearchSpace

An AutoKSearchSpace represents a search space of neural networks.

Parameters
  • input_shape (list(tuple(int))) – list of shapes of all inputs.

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

  • regression (bool) – if True the output will be a simple tf.keras.layers.Dense(output_shape[0]) layer as the output layer. if False the output will be tf.keras.layers.Dense(output_shape[0], activation='softmax').

Raises

InputShapeOfWrongType – [description]

create_model()[source]

Create the tensors corresponding to the search_space.

Returns

The output tensor.

set_output_node(graph, output_nodes)[source]

Set the output node of the search_space.

Parameters
  • graph (nx.DiGraph) – graph of the search_space.

  • output_nodes (Node) – nodes of the current search_space without successors.

Returns

output node of the search_space.

Return type

Node

deephyper.nas.space.keras_search_space module

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

Bases: deephyper.nas.space.nx_search_space.NxSearchSpace

A KSearchSpace represents a search space of neural networks.

>>> from tensorflow.keras.utils import plot_model
>>> from deephyper.nas.space import KSearchSpace
>>> from deephyper.nas.space.node import VariableNode, ConstantNode
>>> from deephyper.nas.space.op.op1d import Dense
>>> struct = KSearchSpace((5, ), (1, ))
>>> vnode = VariableNode()
>>> struct.connect(struct.input_nodes[0], vnode)
>>> vnode.add_op(Dense(10))
>>> vnode.add_op(Dense(20))
>>> output_node = ConstantNode(op=Dense(1))
>>> struct.connect(vnode, output_node)
>>> struct.set_ops([0])
>>> model = struct.create_model()
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]

create_model()[source]

Create the tensors corresponding to the search_space.

Returns

A keras.Model for the current search_space with the corresponding set of operations.

property depth
property longest_path
set_ops(indexes)[source]

Set the operations for each node of each cell of the search_space.

Parameters

indexes (list) – element of list can be float in [0, 1] or int.

Raises

WrongSequenceToSetOperations – raised when ‘indexes’ is of a wrong length.

set_output_node(graph, output_nodes)[source]

Set the output node of the search_space.

Parameters
  • graph (nx.DiGraph) – graph of the search_space.

  • output_nodes (Node) – nodes of the current search_space without successors.

Returns

output node of the search_space.

Return type

Node

deephyper.nas.space.node module

class deephyper.nas.space.node.ConstantNode(op=None, name='', *args, **kwargs)[source]

Bases: deephyper.nas.space.node.OperationNode

A ConstantNode represents a node with a fixed operation. It means the agent will not make any new decision for this node. The common use case for this node is to add a tensor in the graph.

>>> import tensorflow as tf
>>> from deephyper.nas.space.node import ConstantNode
>>> from deephyper.nas.space.op.op1d import Dense
>>> cnode = ConstantNode(op=Dense(units=100, activation=tf.nn.relu), name='CNode1')
>>> cnode.op
Dense_100_relu
Parameters
  • op (Operation, optional) – [description]. Defaults to None.

  • name (str, optional) – [description]. Defaults to ‘’.

property op
set_op(op)[source]
class deephyper.nas.space.node.MimeNode(node, name='')[source]

Bases: deephyper.nas.space.node.OperationNode

A MimeNode is a node which reuse an the choice made for an VariableNode, it enable the definition of a Cell based search_space. This node reuse the operation from the mimed VariableNode but only the choice made.

Parameters

node (VariableNode) – the VariableNode to mime.

>>> from deephyper.nas.space.node import VariableNode, MimeNode
>>> from deephyper.nas.space.op.op1d import Dense
>>> vnode = VariableNode()
>>> vnode.add_op(Dense(10))
>>> vnode.add_op(Dense(20))
>>> mnode = MimeNode(vnode)
>>> mnode.add_op(Dense(30))
>>> mnode.add_op(Dense(40))
>>> vnode.set_op(0)
>>> vnode.op
Dense_10
>>> mnode.op
Dense_30
add_op(op)[source]
property num_ops
property op
property ops
set_op()[source]
class deephyper.nas.space.node.MirrorNode(node)[source]

Bases: deephyper.nas.space.node.OperationNode

A MirrorNode is a node which reuse an other, it enable the reuse of keras layers. This node will not add operations to choose.

Parameters

node (Node) – The targeted node to mirror.

>>> from deephyper.nas.space.node import VariableNode, MirrorNode
>>> from deephyper.nas.space.op.op1d import Dense
>>> vnode = VariableNode()
>>> vnode.add_op(Dense(10))
>>> vnode.add_op(Dense(20))
>>> mnode = MirrorNode(vnode)
>>> vnode.set_op(0)
>>> vnode.op
Dense_10
>>> mnode.op
Dense_10
property op
class deephyper.nas.space.node.Node(name='', *args, **kwargs)[source]

Bases: object

This class represents a node of a graph

Parameters

name (str) – node name.

create_tensor(*args, **kwargs)[source]
property id
num = 0
property op
static verify_operation(op)[source]
class deephyper.nas.space.node.OperationNode(name='', *args, **kwargs)[source]

Bases: deephyper.nas.space.node.Node

create_tensor(inputs=None, train=True, seed=None, **kwargs)[source]
class deephyper.nas.space.node.VariableNode(name='')[source]

Bases: deephyper.nas.space.node.OperationNode

This class represents a node of a graph where you have a set of possible operations. It means the agent will have to act to choose one of these operations.

>>> import tensorflow as tf
>>> from deephyper.nas.space.node import VariableNode
>>> vnode = VariableNode("VNode1")
>>> from deephyper.nas.space.op.op1d import Dense
>>> vnode.add_op(Dense(
... units=10,
... activation=tf.nn.relu))
>>> vnode.num_ops
1
>>> vnode.add_op(Dense(
... units=1000,
... activation=tf.nn.tanh))
>>> vnode.num_ops
2
>>> vnode.set_op(0)
>>> vnode.op.units
10
Parameters

name (str) – node name.

add_op(op)[source]
denormalize(index)[source]

Denormalize a normalized index to get an absolute indexes. Useful when you want to compare the number of different search_spaces.

Parameters

indexes (float|int) – a normalized index.

Returns

An absolute indexes corresponding to the operation choosen with the relative index of index.

Return type

int

get_op(index)[source]
property num_ops
property op
property ops
set_op(index)[source]

deephyper.nas.space.nx_search_space module

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

Bases: object

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.

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.

create_tensor_aux(g, n, train=None)[source]

Recursive function to create the tensors from the graph.

Parameters
  • g (nx.DiGraph) – a graph

  • n (nx.Node) – a node

  • train (bool) – True if the network is built for training, False if the network is built for validation/testing (for example False will deactivate Dropout).

Returns

the tensor represented by n.

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

draw_graphviz(path)[source]
get_output_nodes()[source]

Get nodes of ‘graph’ without successors.

Returns

the nodes without successors of a DiGraph.

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

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))

Module contents

class deephyper.nas.space.AutoKSearchSpace(input_shape, output_shape, regression: bool, *args, **kwargs)[source]

Bases: deephyper.nas.space.keras_search_space.KSearchSpace

An AutoKSearchSpace represents a search space of neural networks.

Parameters
  • input_shape (list(tuple(int))) – list of shapes of all inputs.

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

  • regression (bool) – if True the output will be a simple tf.keras.layers.Dense(output_shape[0]) layer as the output layer. if False the output will be tf.keras.layers.Dense(output_shape[0], activation='softmax').

Raises

InputShapeOfWrongType – [description]

create_model()[source]

Create the tensors corresponding to the search_space.

Returns

The output tensor.

set_output_node(graph, output_nodes)[source]

Set the output node of the search_space.

Parameters
  • graph (nx.DiGraph) – graph of the search_space.

  • output_nodes (Node) – nodes of the current search_space without successors.

Returns

output node of the search_space.

Return type

Node

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

Bases: deephyper.nas.space.nx_search_space.NxSearchSpace

A KSearchSpace represents a search space of neural networks.

>>> from tensorflow.keras.utils import plot_model
>>> from deephyper.nas.space import KSearchSpace
>>> from deephyper.nas.space.node import VariableNode, ConstantNode
>>> from deephyper.nas.space.op.op1d import Dense
>>> struct = KSearchSpace((5, ), (1, ))
>>> vnode = VariableNode()
>>> struct.connect(struct.input_nodes[0], vnode)
>>> vnode.add_op(Dense(10))
>>> vnode.add_op(Dense(20))
>>> output_node = ConstantNode(op=Dense(1))
>>> struct.connect(vnode, output_node)
>>> struct.set_ops([0])
>>> model = struct.create_model()
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]

create_model()[source]

Create the tensors corresponding to the search_space.

Returns

A keras.Model for the current search_space with the corresponding set of operations.

property depth
property longest_path
set_ops(indexes)[source]

Set the operations for each node of each cell of the search_space.

Parameters

indexes (list) – element of list can be float in [0, 1] or int.

Raises

WrongSequenceToSetOperations – raised when ‘indexes’ is of a wrong length.

set_output_node(graph, output_nodes)[source]

Set the output node of the search_space.

Parameters
  • graph (nx.DiGraph) – graph of the search_space.

  • output_nodes (Node) – nodes of the current search_space without successors.

Returns

output node of the search_space.

Return type

Node

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

Bases: object

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.

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.

create_tensor_aux(g, n, train=None)[source]

Recursive function to create the tensors from the graph.

Parameters
  • g (nx.DiGraph) – a graph

  • n (nx.Node) – a node

  • train (bool) – True if the network is built for training, False if the network is built for validation/testing (for example False will deactivate Dropout).

Returns

the tensor represented by n.

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

draw_graphviz(path)[source]
get_output_nodes()[source]

Get nodes of ‘graph’ without successors.

Returns

the nodes without successors of a DiGraph.

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

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.space.SpaceFactory[source]

Bases: abc.ABC

abstract classmethod build(input_shape, output_shape, **kwargs)deephyper.nas.space.keras_search_space.KSearchSpace[source]

Return a search space.

check_op_list(space: deephyper.nas.space.keras_search_space.KSearchSpace, ops: list)list[source]
plot_model(input_shape, output_shape, ops: list = [], fname: str = 'random_model.png', show_shapes: bool = True, **kwargs)None[source]
plot_space(input_shape, output_shape, ops: list = [], fname: str = 'space.dot', **kwargs)None[source]
test(input_shape, output_shape, **kwargs)[source]