-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate seeds before adding graph components
This means that if you do set the seed for the top-level Network in your model, adding or removing graphs will not affect the seed for the Ensembles in your model. Fixes #855
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import nengo | ||
import nengo.utils.numpy as npext | ||
import numpy as np | ||
|
||
|
||
def define_all_seeds(net, seeds=None): | ||
if seeds is None: | ||
seeds = {} | ||
|
||
if net.seed is None: | ||
if net not in seeds: | ||
# this only happens at the very top level | ||
seeds[net] = np.random.randint(npext.maxint) | ||
rng = np.random.RandomState(seed=seeds[net]) | ||
else: | ||
rng = np.random.RandomState(seed=net.seed) | ||
|
||
# let's use the same algorithm as the builder, just to be consistent | ||
sorted_types = sorted(net.objects, key=lambda t: t.__name__) | ||
for obj_type in sorted_types: | ||
for obj in net.objects[obj_type]: | ||
# generate a seed for each item, so that manually setting a seed | ||
# for a particular item doesn't change the generated seed for | ||
# other items | ||
generated_seed = rng.randint(npext.maxint) | ||
if obj.seed is None: | ||
seeds[obj] = generated_seed | ||
|
||
for subnet in net.networks: | ||
define_all_seeds(subnet, seeds) | ||
|
||
return seeds |