0

我有以下形式的功能:

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

我想通过一组可能的参数组合记录它的返回值:

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

我的意思是我想使用以如下方式指定的参数集的组合多次运行该函数并收集结果值:

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

解决此类问题的一种好的、易读的方法是什么?

4

1 回答 1

1

您可以使用 itertools,例如:

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)

输出:

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]
于 2015-12-06T13:42:52.977 回答