Exemplo n.º 1
0
def clean_layers(connection):
    """
    Clean layers connections and format transform them into one format.
    Also this function validate layers connections.

    Parameters
    ----------
    connection : list, tuple or object
        Layers connetion in different formats.

    Returns
    -------
    object
        Cleaned layers connection.
    """

    if is_list_of_integers(connection):
        connection = generate_layers(list(connection))

    if isinstance(connection, tuple):
        connection = list(connection)

    islist = isinstance(connection, list)
    layer_types = (layers.BaseLayer, LayerConnection)

    if islist and isinstance(connection[0], layer_types):
        chain_connection = connection.pop()
        for layer in reversed(connection):
            chain_connection = LayerConnection(layer, chain_connection)
        connection = chain_connection

    if not isinstance(connection.input_layer, layers.Input):
        raise NetworkConnectionError("First layer must be layers.Input class "
                                     "instance.")

    all_layers = list(connection)

    if any(isinstance(layer, layers.Input) for layer in all_layers[1:]):
        raise NetworkConnectionError("Only the first layer can be instance "
                                     "of layers.Input class.")

    return connection
Exemplo n.º 2
0
    def __init__(self, connection, **options):
        if len(connection) != 2:
            raise ValueError("This network should contains two layers.")

        if is_list_of_integers(connection):
            input_layer_size, output_layer_size = connection
            connection = Input(input_layer_size) > Step(output_layer_size)

        if not isinstance(connection, LayerConnection):
            raise ValueError("Invalid network connection structure.")

        if not isinstance(connection.output_layer, Step):
            raise NetworkConnectionError(
                "Final layer should contains step activation function "
                "(``layers.Step`` class instance).")

        super(BaseLinearNetwork, self).__init__(connection, **options)
Exemplo n.º 3
0
def clean_layers(connection):
    """ Clean layers connections and format transform them into one format.
    Also this function validate layers connections.

    Parameters
    ----------
    connection : list, tuple or object
        Layers connetion in different formats.

    Returns
    -------
    object
        Cleaned layers connection.
    """

    if is_list_of_integers(connection):
        connection = generate_layers(list(connection))

    if isinstance(connection, tuple):
        connection = list(connection)

    islist = isinstance(connection, list)

    if islist and isinstance(connection[0], BaseLayer):
        chain_connection = connection.pop()
        for layer in reversed(connection):
            chain_connection = LayerConnection(layer, chain_connection)
        connection = chain_connection

    elif islist and isinstance(connection[0], LayerConnection):
        pass

    if not isinstance(connection.output_layer, Output):
        raise NetworkConnectionError("Final layer must be Output class "
                                     "instance.")

    return connection
Exemplo n.º 4
0
 def relate_to(self, right_layer):
     raise NetworkConnectionError("Can't create connection "
                                  "from output layer")