Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from netron.solvers import Solver
from netron.grid import NeuralNetGrid
from sklearn.grid_search import ParameterSampler, ParameterGrid
import random
import itertools
import hashlib
import time
class RandomSearch(Solver):
# If we sample more than this number of already seen networks
# consecutively, then skip to the next network size
STRUCT_DUP_THRESHOLD = 100
def initialize(self, params_sample_size, structure_sample_size):
self.params_sample_size = params_sample_size
self.structure_sample_size = structure_sample_size
self.seen_structures = set()
def random_product(self, *args, **kwds):
"Random selection from itertools.product(*args, **kwds)"
pools = map(tuple, args) * kwds.get('repeat', 1)
return tuple(random.choice(pool) for pool in pools)
def create_network_structures(self, layers, layers_num, input_shape):
"""Returns all combinations of given set of layers with given set of sizes"""
from netron.solvers import Solver
from netron.grid import NeuralNetGrid
from sklearn.grid_search import ParameterGrid
import itertools
class GridSearch(Solver):
def create_network_structures(self, layers, layers_num, input_shape):
"""Returns all combinations of given set of layers with given set of sizes"""
for i in layers_num:
for net_struct in itertools.product(layers, repeat=i):
fixed_net_struct = self.model_factory.fix_or_skip(net_struct, input_shape)
if fixed_net_struct:
yield fixed_net_struct
else:
print "skipping invalid structure: %s" % "->".join(net_struct)
continue
def generate_models(self, input_shape, output_dim):
loss_type = self.grid.params_grid["loss"][0]
for layers in self.create_network_structures(self.grid.params_grid["layers"], self.grid.params_grid["layer_nums"], input_shape):
print "Current network: %s" % "->".join(layers)