- 首先定义超参数的名称和取值范围,
- 然后调用itertools.product,可以生成所有超参数的排列组合。
ref: https://docs.python.org/3/library/itertools.html#itertools.product
import itertools
import subprocess
# === define paras ==================
para_names = ['layer_n', 'activition', 'seed']
layer_n = [1, 2, 3, 4, 5, 6]
activition = ['tanh', 'sigmod', 'relu']
seed = [11, 17, 19]
# calc cases number
i = 1
nums = sum(1 for _ in itertools.product(layer_n, activition, seed))
print(f'==== we have {nums} cases in total', '===' * 3)
# === run all case ===========
for values in itertools.product(layer_n, activition, seed):
print(f' *** {i} / {nums} ', '***' * 3)
cmd = f'python mnist_cnn.py'
for p, v in zip(para_names, values):
cmd += f' --{p}={v}'
print(cmd)
# p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
# print(p.stdout.read().decode('utf8'))
i += 1
输出:
==== we have 54 cases in total =========
*** 1 / 54 *********
python mnist_cnn.py --layer_n=1 --activition=tanh --seed=11
*** 2 / 54 *********
python mnist_cnn.py --layer_n=1 --activition=tanh --seed=17
*** 3 / 54 *********
python mnist_cnn.py --layer_n=1 --activition=tanh --seed=19
*** 4 / 54 *********
python mnist_cnn.py --layer_n=1 --activition=sigmod --seed=11
*** 5 / 54 *********
python mnist_cnn.py --layer_n=1 --activition=sigmod --seed=17
*** 6 / 54 *********
...
*** 53 / 54 *********
python mnist_cnn.py --layer_n=6 --activition=relu --seed=17
*** 54 / 54 *********
python mnist_cnn.py --layer_n=6 --activition=relu --seed=19
网友评论