Skip to content

architecture

get_hidden_sizes(_L_in, l1, n=10)

Generates a list of hidden sizes for a neural network with a given input size. Starting with size l1, the list is generated by dividing the input size by 2 until the minimum size is reached.

Parameters:

Name Type Description Default
_L_in int

input size.

required
l1 int

number of neurons in the first hidden layer.

required
n int

number of hidden sizes to generate.

10

Returns:

Type Description
list

list of hidden sizes.

Examples:

>>> from spotpython.hyperparameters.architecture import get_hidden_sizes
    _L_in = 10
    l1 = 20
    n = 4
    get_hidden_sizes(_L_in, l1, n)
    [20, 10, 10, 5]
Source code in spotpython/hyperparameters/architecture.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def get_hidden_sizes(_L_in, l1, n=10) -> list:
    """
    Generates a list of hidden sizes for a neural network with a given input size.
    Starting with size l1, the list is generated by dividing the input size by 2 until the minimum size is reached.

    Args:
        _L_in (int):
            input size.
        l1 (int):
            number of neurons in the first hidden layer.
        n (int):
            number of hidden sizes to generate.

    Returns:
        (list):
            list of hidden sizes.

    Examples:
        >>> from spotpython.hyperparameters.architecture import get_hidden_sizes
            _L_in = 10
            l1 = 20
            n = 4
            get_hidden_sizes(_L_in, l1, n)
            [20, 10, 10, 5]
    """
    if l1 < 4:
        raise ValueError("l1 must be at least 4")
    n_low = _L_in // 4
    n_high = max(l1, 2 * n_low)
    hidden_sizes = generate_div2_list(n_high, n_low)
    # keep only the first n values of hidden_sizes list
    if len(hidden_sizes) > n:
        hidden_sizes = hidden_sizes[:n]
    return hidden_sizes