d3pd
d3pd

Reputation: 8315

How could all combinations of a set of possible arguments of a function be tried and the resultant values be collected?

I have a function of the following form:

def NN(epochs = None, hidden_nodes = None):
    # ... calculations...
    return score

I want to record its returned values over a set of combinations of possible arguments:

epochs = [10, 100]
architecture = [
    [5, 10],
    [5, 10],
    [5, 10]
]

What I mean is that I would want to run the function multiple times using combinations of the sets of arguments specified in a way like the following and to collect the resultant values:

NN(epochs = 10, hidden_nodes = [5])
NN(epochs = 10, hidden_nodes = [10])
NN(epochs = 10, hidden_nodes = [5,   5])
NN(epochs = 10, hidden_nodes = [5,  10])
NN(epochs = 10, hidden_nodes = [10,  5])
NN(epochs = 10, hidden_nodes = [10, 10])
NN(epochs = 10, hidden_nodes = [5,   5,  5])
NN(epochs = 10, hidden_nodes = [5,   5, 10])
NN(epochs = 10, hidden_nodes = [5,  10, 10])
NN(epochs = 10, hidden_nodes = [10, 10, 10])
NN(epochs = 10, hidden_nodes = [5,  10,  5])
...

NN(epochs = 100, hidden_nodes = [ 5])
NN(epochs = 100, hidden_nodes = [10])
NN(epochs = 100, hidden_nodes = [5,   5])
...

What would be a good, readable way to approach a problem like this?

Upvotes: 0

Views: 42

Answers (1)

Žilvinas Rudžionis
Žilvinas Rudžionis

Reputation: 2346

You can use itertools like:

import itertools

epochs = [10, 100]
architecture = [5, 10]

for epock in epochs:
    for nodes_count in xrange(1, 4):
        combinations = itertools.product(architecture, repeat=nodes_count)
        for combination in combinations:
            print epock, list(combination)

output:

10 [5]
10 [10]
10 [5, 5]
10 [5, 10]
10 [10, 5]
10 [10, 10]
10 [5, 5, 5]
10 [5, 5, 10]
10 [5, 10, 5]
10 [5, 10, 10]
10 [10, 5, 5]
10 [10, 5, 10]
10 [10, 10, 5]
10 [10, 10, 10]
100 [5]
100 [10]
100 [5, 5]
100 [5, 10]
100 [10, 5]
100 [10, 10]
100 [5, 5, 5]
100 [5, 5, 10]
100 [5, 10, 5]
100 [5, 10, 10]
100 [10, 5, 5]
100 [10, 5, 10]
100 [10, 10, 5]
100 [10, 10, 10]

Upvotes: 1

Related Questions