def test_ND(): "test various tensor size random" TEST_INPUTS = [ [SingleCrossover1D, (2, 4), 0.5], [SingleCrossover2D, (2, 4, 4), (0.5, 0.5)], [SingleCrossover3D, (2, 4, 4, 4), (0.5, 0.5, 0.5)], ] for inputs in TEST_INPUTS: OP = inputs[0] pop_shape = inputs[1] mutations_probability = inputs[2] population_fraction = 1 population = B.randint(0, 1024, pop_shape) # eager RM = OP(population_fraction=population_fraction, mutations_probability=mutations_probability) population = RM(population) assert B.is_tensor(population) assert population.shape == pop_shape # graph RM = OP(population_fraction=population_fraction, mutations_probability=mutations_probability) population = RM._call_from_graph(population) assert B.is_tensor(population) assert population.shape == pop_shape
def test_non_tensor_input(): shape = (2, 4) population = [[1, 2, 3, 4], [1, 2, 3, 4]] inputs = Input(shape) inputs.assign(population) res = inputs.get() assert B.is_tensor(res)
def test_ND(): "test various tensor size random" TEST_INPUTS = [ [RandomMutations1D, (2, 4), 0.5], [RandomMutations2D, (2, 4, 4), (0.5, 0.5)], [RandomMutations3D, (2, 4, 4, 4), (0.5, 0.5, 0.5)], ] for inputs in TEST_INPUTS: OP = inputs[0] pop_shape = inputs[1] mutations_probability = inputs[2] max_gene_value = 10 min_gene_value = 0 population_fraction = 1 min_mutation_value = 1 max_mutation_value = 1 population = B.randint(0, max_gene_value, pop_shape) # eager RM = OP(population_fraction=population_fraction, mutations_probability=mutations_probability, min_gene_value=min_gene_value, max_gene_value=max_gene_value, min_mutation_value=min_mutation_value, max_mutation_value=max_mutation_value) population = RM(population) assert B.is_tensor(population) assert population.shape == pop_shape # graph RM = OP(population_fraction=population_fraction, mutations_probability=mutations_probability, min_gene_value=min_gene_value, max_gene_value=max_gene_value, min_mutation_value=min_mutation_value, max_mutation_value=max_mutation_value) population = RM._call_from_graph(population) assert B.is_tensor(population) assert population.shape == pop_shape
def __call__(self, ops): # boxing inputs if needed ops = box(ops) # graph computation or eager? if self.debug: input_types = [type(op) for op in ops] self.print_debug('inputs type:%s' % (input_types)) if issubclass(type(ops[0]), OP): # graph mode self.print_debug('graph mode') input_shapes = [] for op in ops: assert issubclass(type(op), OP) self.input_ops.append(op) input_shapes.append(op.get_output_shapes()) self.compute_output_shape(input_shapes) return self else: # eager mode self.print_debug('eager mode') # check inputs are valis for op in ops: if not B.is_tensor(op): raise ValueError("Expecting list(tensors) or a tensor") # input shapes input_shapes = [] for op in ops: input_shapes.append(op.shape) self.compute_output_shape(input_shapes) # compute concrete results results = self.call(ops) # unbox result if needed return unbox(results)
def test_mutation2d_graph_mode(): "make sure the boxing / unboxing works in graph mode" pop_shape = (2, 4, 4) max_gene_value = 10 min_gene_value = 0 population_fraction = 1 mutations_probability = (0.5, 0.5) min_mutation_value = 1 max_mutation_value = 1 population = B.randint(0, max_gene_value, pop_shape) RM = RandomMutations2D(population_fraction=population_fraction, mutations_probability=mutations_probability, min_gene_value=min_gene_value, max_gene_value=max_gene_value, min_mutation_value=min_mutation_value, max_mutation_value=max_mutation_value, debug=True) population = RM._call_from_graph(population) assert B.is_tensor(population) assert population.shape == pop_shape
def test_mutation2d_eager(): pop_shape = (2, 4, 4) max_gene_value = 10 min_gene_value = 0 population_fraction = 1 mutations_probability = (0.5, 0.5) min_mutation_value = 1 max_mutation_value = 1 population = B.randint(0, max_gene_value, pop_shape) # save original original_population = copy(population) cprint('[Initial genepool]', 'blue') cprint(original_population, 'blue') RM = RandomMutations2D(population_fraction=population_fraction, mutations_probability=mutations_probability, min_gene_value=min_gene_value, max_gene_value=max_gene_value, min_mutation_value=min_mutation_value, max_mutation_value=max_mutation_value, debug=True) population = RM(population) cprint('\n[Mutated genepool]', 'yellow') cprint(population, 'yellow') cprint('\n[Diff]', 'magenta') diff = population - original_population cprint(diff, 'magenta') assert B.is_tensor(population) assert population.shape == pop_shape assert B.max(diff) <= max_mutation_value for chromosome in diff: assert B.sum(chromosome) == 4