def test_instantiation_Conv2D_Pool2D_Flatten_Dense(self): for i in range(10): batch = 1 _data = TypeShape( DFloat, Shape((DimNames.BATCH, batch), (DimNames.HEIGHT, 32), (DimNames.WIDTH, 32), (DimNames.CHANNEL, 3))) _target = TypeShape( DFloat, Shape( (DimNames.BATCH, batch), (DimNames.UNITS, 10), )) outputs = {'out0': _target} IOLabel.DS = 'DS' inputs = {IOLabel.DS: (IOLabel.DATA, _data, 'Dataset')} functions = [Conv2D, Pooling2D, Flatten, Dense] NN = NeuralNetwork( **{ NeuralNetwork.arg_INPUTS: inputs, NeuralNetwork.arg_OUTPUT_TARGETS: outputs, NeuralNetwork.arg_FUNCTIONS: functions }) self.assertIsNotNone(NN) pb = NN.get_pb() state = NN.__getstate__() f_ids = dict([(_id, None) for _, _id in NN.inputs.values()]) for _f in NN.functions: f_ids[_f.id_name] = _f for _f in NN.functions: for _f_input, (other_output, other_id) in _f.inputs.items(): if other_id not in f_ids: self.assertTrue(False) stack = [f_id for _, f_id in NN.output_mapping.values()] required_ids = set() while stack: f_id = stack.pop() required_ids.add(f_id) f_ = f_ids.get(f_id) if f_ is not None: stack.extend([f_id for _, f_id in f_.inputs.values()]) self.assertSetEqual(required_ids, set(f_ids.keys())) NN_pb = NeuralNetwork.__new__(NeuralNetwork) NN_pb.__setstate__(pb) self.assertIsNot(NN, NN_pb) NN_state = NeuralNetwork.__new__(NeuralNetwork) NN_state.__setstate__(state) self.assertIsNot(NN, NN_state) NN_mut = NN.mutate(100) self.assertIsNot(NN, NN_mut) self.assertNotEqual(NN, NN_mut) NN_mut = NN.mutate(0) self.assertIsNot(NN, NN_mut) self.assertNotEqual(NN, NN_mut)
def test_instantiation_USD_ONTS_Dense_Merge(self): for i in range(10): batch = 1 _data = TypeShape( DFloat, Shape((DimNames.BATCH, batch), (DimNames.UNITS, 20))) IOLabel.DS1 = 'DS1' IOLabel.DS2 = 'DS2' inputs = { IOLabel.DS1: (IOLabel.DATA, _data, 'Dataset'), IOLabel.DS2: (IOLabel.DATA, _data, 'Dataset') } outShape = Shape((DimNames.BATCH, batch), (DimNames.UNITS, 10)) outShape1 = Shape((DimNames.BATCH, batch), (DimNames.UNITS, 15)) outputs = { 'out0': TypeShape(DFloat, outShape), 'out1': TypeShape(DFloat, outShape1) } functions = [Merge, Dense] NN = NeuralNetwork( **{ NeuralNetwork.arg_INPUTS: inputs, NeuralNetwork.arg_OUTPUT_TARGETS: outputs, NeuralNetwork.arg_FUNCTIONS: functions, NeuralNetwork.arg_MAX_BRANCH: 2 }) self.assertIsNotNone(NN) pb = NN.get_pb() state = NN.__getstate__() NN_pb = NeuralNetwork.__new__(NeuralNetwork) NN_pb.__setstate__(pb) self.assertIsNot(NN, NN_pb) NN_state = NeuralNetwork.__new__(NeuralNetwork) NN_state.__setstate__(state) self.assertIsNot(NN, NN_state) NN_mut = NN.mutate(1)[0] self.assertEqual(pb, NN.get_pb()) self.assertIsNot(NN, NN_mut) self.assertNotEqual(NN, NN_mut) f_ids = dict([(_id, None) for _, _id in NN_mut.inputs.values()]) for _f in NN_mut.functions: f_ids[_f.id_name] = _f for _f in NN_mut.functions: for _f_input, (other_output, other_id) in _f.inputs.items(): if other_id not in f_ids: self.assertTrue(False) stack = [f_id for _, f_id in NN_mut.output_mapping.values()] required_ids = set() while stack: f_id = stack.pop() required_ids.add(f_id) f_ = f_ids.get(f_id) if f_ is not None: stack.extend([f_id for _, f_id in f_.inputs.values()]) self.assertSetEqual(required_ids, set(f_ids.keys())) NN_mut = NN.mutate(1)[0] self.assertEqual(pb, NN.get_pb()) self.assertIsNot(NN, NN_mut) self.assertNotEqual(NN, NN_mut) f_ids = dict([(_id, None) for _, _id in NN_mut.inputs.values()]) for _f in NN_mut.functions: f_ids[_f.id_name] = _f for _f in NN_mut.functions: for _f_input, (other_output, other_id) in _f.inputs.items(): if other_id not in f_ids: self.assertTrue(False) stack = [f_id for _, f_id in NN_mut.output_mapping.values()] required_ids = set() while stack: f_id = stack.pop() required_ids.add(f_id) f_ = f_ids.get(f_id) if f_ is not None: stack.extend([f_id for _, f_id in f_.inputs.values()]) self.assertSetEqual(required_ids, set(f_ids.keys())) NN_mut = NN.mutate(0)[0] NN_mut._id_name = NN._id_name self.assertNotEqual(NN, NN_mut)