for index, elem in enumerate(array): wheel.add(elem, weights[index]) samples = {} draws = 10000 for _ in range(draws): sample = wheel.spin() if sample in samples: samples[sample] += 1 else: samples[sample] = 1 print("\nArray samples ({} draws):".format(draws)) for key in sorted(samples.keys()): print("\t", key, ":", samples[key]) print("\n===[ Random key and value from table ]===\n") table = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"} print("Table:") for key in sorted(table.keys()): print("\t", key, ":", table[key]) print("key:", Rand.key(table)) print("val:", Rand.val(table)) if __name__ == '__main__': utest.run(test_random_functions)
import cortex.utest as utest import cortex.statistics as Stat def test_stat(_type=Stat.MAType.Simple): stat = Stat.SMAStat( "Test stats") if _type == Stat.MAType.Simple else Stat.EMAStat() for i in range(1, 100): stat.update(i) stat.print() if __name__ == '__main__': utest.run(test_stat, _type=Stat.MAType.Simple) utest.run(test_stat, _type=Stat.MAType.Exponential)
import cortex.utest as utest import torch def test_torch_allclose(_add = 1e-6, _rtol = 1e-7, _atol = 1e-8): tensor1 = torch.ones(3,3) tensor2 = torch.ones(3,3) print("Tensor1:\n", tensor1) print("Tensor2:\n", tensor2) print("Relative tolerance:", _rtol) print("Absolute tolerance:", _atol) print("After adding", _add, "to element (0,0)") tensor2[0][0] += _add print("Tensor1 == Tensor2:", tensor1.allclose(tensor2, _rtol, _atol)) if __name__ == '__main__': utest.run(test_torch_allclose, 1e-6) utest.run(test_torch_allclose, 1e-7) utest.run(test_torch_allclose, 1e-8)
import cortex.utest as utest import cortex.cortex as ctx def test_set_layer_kernel_size(_shape=[10, 0, 0]): print("Initial layer shape:", _shape) ctx.Net.Init.Layers = [ctx.Layer.Def(_shape)] net = ctx.Net() net.layers[0].print() if __name__ == '__main__': utest.run(test_set_layer_kernel_size) utest.run(test_set_layer_kernel_size, [10, 3, 0]) utest.run(test_set_layer_kernel_size, [10, 0, 3]) utest.run(test_set_layer_kernel_size, [10, 3, 3])
import cortex.utest as utest import torch def test_tensor_slices(): tensor = torch.randn(5, 3, 3) new_tensor = torch.Tensor() print(tensor) print(new_tensor) slices = [slice(0, 2), slice(3, None)] for slice_index in range(len(slices)): if slice_index == 0: new_tensor = tensor[slices[slice_index]] else: new_tensor = torch.cat((new_tensor, tensor[slices[slice_index]])) print(tensor) print(new_tensor) if __name__ == '__main__': utest.run(test_tensor_slices)
print( "======================[ Original network ]======================") original_net.print() print( "======================[ Mutated network ]=======================") net.print() model1 = net.to('cpu') model2 = original_net.to('cpu') input1 = torch.randn(ctx.TrainBatchSize, *ctx.Net.Input.Shape) input2 = torch.zeros(input1.size()) input2.data = input1.data print("Input1:", input1, ", size:", input1.size()) print("Input2:", input2, ", size:", input2.size()) output1 = model1(input1) output2 = model2(input2) assert (utest.pass_fail(torch.allclose(output1, output2), "Comparing the two outputs...")) print(output1) print(output2) if __name__ == '__main__': utest.run(test_random_mutations)
import cortex.utest as utest import cortex.functions as Func import cortex.statistics as Stat import math def test_fmap(_function_name, _function, _val = 1.0): print(_function_name + "(" + str(_val) + "):", _function(_val)) if __name__ == '__main__': for enum, f in Func.fmap.items(): if enum == Func.Type.Softmax: vals = [20000, 10.0, 7e-5, 0.0] # vals = [0, 0, 0] min_val = min(vals) print("Minimum:", min_val) vals = [v - min_val for v in vals] print("Vals:", vals) vals = [math.log1p(v) for v in vals] utest.run(test_fmap, enum.name, f, vals) else: utest.run(test_fmap, enum.name, f)
if mutation == 'remove_layer': net.remove_layer() elif mutation == 'remove_node': net.remove_nodes() elif mutation == 'shrink_kernel': net.shrink_kernel() model = net.to('cpu') match = list(model(torch.randn(ctx.Conf.TrainBatchSize, *ctx.cn.Net.Input.Shape)).size()) == [ctx.Conf.TrainBatchSize, len(net.layers[-1].nodes)] if not utest.pass_fail(match, "\tEvaluating the mutated network with random input..."): net.print() for p1 in range(len(nets)): for p2 in range(len(nets)): if p1 != p2: offspring = ctx.cn.Net(_p1 = nets[p1], _p2 = nets[p2], _isolated = True) model = offspring.to('cpu') match = list(model(torch.randn(ctx.Conf.TrainBatchSize, *ctx.cn.Net.Input.Shape)).size()) == [ctx.Conf.TrainBatchSize, len(net.layers[-1].nodes)] if not utest.pass_fail(match, "\tEvaluating the offspring network with random input..."): offspring.print() if __name__ == '__main__': utest.run(test_crossover)
if _mut == 'add_layer': success = net.add_layer() elif _mut == 'remove_layer': success = net.remove_layer() elif _mut == 'add_node': success = net.add_nodes() elif _mut == 'remove_node': success = net.remove_nodes() elif _mut == 'grow_kernel': success = net.grow_kernel() elif _mut == 'shrink_kernel': success = net.shrink_kernel() else: print("Invalid mutation type %r" % _mut) return assert(utest.pass_fail(success, "Mutating network...")) net.print('after_mutation.txt', True) model = net.to('cpu') tensor = torch.randn(ctx.TrainBatchSize, *ctx.Net.Input.Shape) print("Input size:", tensor.size()) output = model(tensor) print(output) if __name__ == '__main__': utest.run(test_single_mutation)
import cortex.cortex as ctx import cortex.species as cs import cortex.utest as utest def test_init_population(): cs.Species.Enabled = True ctx.init() if __name__ == '__main__': utest.run(test_init_population)
import cortex.utest as utest import cortex.random as Rand import math def test_random_kernel_size(_max=28, _draws=1000): wheel = Rand.RouletteWheel() for i in range(1, _max): if i % 2 == 1: wheel.add(i, math.exp(-i)) kernels = {} for draw in range(_draws): k = wheel.spin() if k not in kernels.keys(): kernels[k] = 0 kernels[k] += 1 for key in sorted(kernels): print(key, ":", kernels[key]) if __name__ == '__main__': utest.run(test_random_kernel_size)
if len(_dilation) == 0: _dilation = tuple([1] * len(_kernel_size)) layer = layer_types[len(_kernel_size)](_input_shape[0], _output_nodes, _kernel_size, _stride, _padding, _dilation) layer_shape = list(layer.weight.size()) del layer_shape[1] # Input channels print("Output nodes:", _output_nodes) print("Input shape:", _input_shape) print("Kernel size:", _kernel_size) print("Padding:", _padding) print("Striide:", _stride) print("Dilation:", _dilation) if len(_kernel_size) > 0: for dim in range(len(_kernel_size)): layer_shape[dim + 1] = ( _input_shape[dim + 1] + 2 * _padding[dim] - _dilation[dim] * (layer_shape[dim + 1] - 1) - 1) // _stride[dim] + 1 print("Output shape:", layer_shape) if __name__ == '__main__': utest.run(test_layer_output_shape)