Add product_dict util function

This commit is contained in:
Mattéo Delabre 2019-12-16 11:32:25 -05:00
parent e8e4dcec83
commit 7efcbd3948
Signed by: matteo
GPG Key ID: AE3FBD02DC583ABB
1 changed files with 20 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import itertools
import torch
import matplotlib
from matplotlib.colors import ListedColormap
@ -153,3 +154,22 @@ def plot_constellation(
alpha=0.3,
zorder=8
)
def product_dict(**kwargs):
"""
Compute cartesian product of a set of parameters.
>>> list(product_dict(first=[1, 2, 3], second=['a', 'b']))
[{'first': 1, 'second': 'a'},
{'first': 1, 'second': 'b'},
{'first': 2, 'second': 'a'},
{'first': 2, 'second': 'b'},
{'first': 3, 'second': 'a'},
{'first': 3, 'second': 'b'}]
"""
keys = kwargs.keys()
vals = kwargs.values()
for instance in itertools.product(*vals):
yield dict(zip(keys, instance))