Skip to content

architecture

generate_div2_list(input_size, n_min, max_repeats=2)

Generates a list of integers starting from n and repeatedly dividing by 2 until n_min is reached. Each integer is repeated a number of times, starting from 1 and increasing by 1 with each division, up to a maximum of 4 repeats.

Parameters:

Name Type Description Default
input_size int

The starting integer.

required
n_min int

The minimum integer value to stop the division.

required
max_repeats int

The maximum number of times an integer can be repeated. Default is 2.

2

Returns:

Name Type Description
list list

A list of integers with the described pattern.

Examples:

>>> from spotpython.hyperparameters.architecture import generate_div2_list
    for n in range(5, 21):
        print(generate_div2_list(input_size=n, n_min=5))
    [5]
    [6]
    [7]
    [8]
    [9]
    [10, 5, 5]
    [11, 5, 5]
    [12, 6, 6]
    [13, 6, 6]
    [14, 7, 7]
    [15, 7, 7]
    [16, 8, 8]
    [17, 8, 8]
    [18, 9, 9]
    [19, 9, 9]
    [20, 10, 10, 5, 5]
Source code in spotpython/hyperparameters/architecture.py
 4
 5
 6
 7
 8
 9
10
11
12
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
47
48
49
def generate_div2_list(input_size, n_min, max_repeats=2) -> list:
    """
    Generates a list of integers starting from `n` and repeatedly dividing by 2 until `n_min` is reached.
    Each integer is repeated a number of times, starting from 1 and increasing by 1 with each division,
    up to a maximum of 4 repeats.

    Args:
        input_size (int): The starting integer.
        n_min (int): The minimum integer value to stop the division.
        max_repeats (int): The maximum number of times an integer can be repeated. Default is 2.

    Returns:
        list: A list of integers with the described pattern.

    Examples:
        >>> from spotpython.hyperparameters.architecture import generate_div2_list
            for n in range(5, 21):
                print(generate_div2_list(input_size=n, n_min=5))
            [5]
            [6]
            [7]
            [8]
            [9]
            [10, 5, 5]
            [11, 5, 5]
            [12, 6, 6]
            [13, 6, 6]
            [14, 7, 7]
            [15, 7, 7]
            [16, 8, 8]
            [17, 8, 8]
            [18, 9, 9]
            [19, 9, 9]
            [20, 10, 10, 5, 5]
    """
    result = []
    current = input_size
    repeats = 1
    while current >= n_min:
        result.extend([current] * min(repeats, max_repeats))
        current = current // 2
        repeats = repeats + 1
    # if result is an empty list, add n_min to it
    if not result:
        result.append(n_min)
    return result

get_hidden_sizes(_L_in, l1, max_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
max_n int

maximum number of hidden sizes to generate. Default is 10.

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, max_n=n)
    [20, 10, 10, 5]
Source code in spotpython/hyperparameters/architecture.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def get_hidden_sizes(_L_in, l1, max_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.
        max_n (int):
            maximum number of hidden sizes to generate. Default is 10.

    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, max_n=n)
            [20, 10, 10, 5]
    """
    if l1 < 4:
        l1 = 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) > max_n:
        hidden_sizes = hidden_sizes[:max_n]
    return hidden_sizes

get_three_layers(_L_in, l1)

Calculate three layers based on input values.

Parameters:

Name Type Description Default
_L_in float

The input value to be multiplied.

required
l1 float

The multiplier for the layers.

required

Returns:

Name Type Description
list list

A list containing three calculated layers [a, b, c] where: - a = 3 * l1 * _L_in - b = 2 * l1 * _L_in - c = l1 * _L_in

Examples:

>>> from spotpython.hyperparameters.architecture import get_three_layers
    _L_in = 10
    l1 = 20
    get_three_layers(_L_in, l1)
    [600, 400, 200]
Source code in spotpython/hyperparameters/architecture.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def get_three_layers(_L_in, l1) -> list:
    """
    Calculate three layers based on input values.

    Args:
        _L_in (float): The input value to be multiplied.
        l1 (float): The multiplier for the layers.

    Returns:
        list: A list containing three calculated layers [a, b, c] where:
            - a = 3 * l1 * _L_in
            - b = 2 * l1 * _L_in
            - c = l1 * _L_in

    Examples:
        >>> from spotpython.hyperparameters.architecture import get_three_layers
            _L_in = 10
            l1 = 20
            get_three_layers(_L_in, l1)
            [600, 400, 200]
    """
    a = 2 * l1 * _L_in
    b = l1 * _L_in
    c = ceil(l1 / 2) * _L_in
    return [a, b, a, b, b, c, c]