def _cls_setstate(self, _function): """ For class dependent attribute retrieval override this method! :param state: FunctionProto :return: None """ self.attr = dict([pb2attr(attr) for attr in _function.attr])
def __copy__(self): result = self.__class__.__new__(self.__class__) result._id_name = self._id_name result.input_mapping = dict(self.input_mapping) result.variables = [v.__copy__() for v in self.variables] result.attr = dict([pb2attr(attr2pb(key, value)) for key, value in self.attr.items()]) return result
def __setstate__(self, state): def build_function(pb): result = Function.__new__(Function) result.__setstate__(pb) return result if isinstance(state, str) or isinstance(state, bytes): _nn = NeuralNetworkProto() _nn.ParseFromString(state) elif isinstance(state, NeuralNetworkProto): _nn = state else: return self._id_name = _nn.id_name self.output_targets = dict([pb2val(v) for v in _nn.output_ntss.v]) self.function_cls = [ Function.getClassByName(f_cls) for f_cls in _nn.function_cls ] self._inputs = dict([(ioMP.in_label, (ioMP.out_label, ioMP.df_id_name)) for ioMP in _nn.input_mapping]) self.output_mapping = dict([(ioMP.in_label, (ioMP.out_label, ioMP.df_id_name)) for ioMP in _nn.output_mapping]) self.functions = [build_function(_f) for _f in _nn.functions] self.attr = dict([pb2attr(attr) for attr in _nn.attr]) self.input_mapping = self.attr.pop(self.arg_INPUTS)
def __setstate__(self, state): if isinstance(state, str) or isinstance(state, bytes): _initializer = InitializerProto() _initializer.ParseFromString(state) elif isinstance(state, InitializerProto): _initializer = state else: return for sub_cls in Initializer.__subclasses__(): if sub_cls.__name__ == _initializer.cls_name: self.__class__ = sub_cls break self.attr = dict([pb2attr(attr) for attr in _initializer.attr])
def __setstate__(self, state): if isinstance(state, str) or isinstance(state, bytes): _regularisation = RegularisationProto() _regularisation.ParseFromString(state) elif isinstance(state, RegularisationProto): _regularisation = state else: return for sub_cls in Regularisation.__subclasses__(): if sub_cls.__name__ == _regularisation.cls_name: self.__class__ = sub_cls break self.attr = dict([pb2attr(attr) for attr in _regularisation.attr])
def __setstate__(self, state): if isinstance(state, str) or isinstance(state, bytes): _cmp = CompareClassProto() _cmp.ParseFromString(state) elif isinstance(state, CompareClassProto): _cmp = state else: return attr = dict([pb2attr(pb) for pb in state.attr]) self.primary_objective = attr.get(self.arg_PRIMARY_OBJECTIVE, Accuracy.ID) self.primary_alpha = attr.get(self.arg_PRIMARY_ALPHA, 1) self.primary_threshold = attr.get(self.arg_PRIMARY_THRESHOLD, .6) self.secondary_objectives = attr.get(self.arg_SECONDARY_OBJECTIVES, {})
def recombine(self, other): result = WeightAgnosticIndividual.__new__(WeightAgnosticIndividual) result.metrics = dict() result.attr = dict( [pb2attr(attr2pb(key, value)) for key, value in self.attr.items()]) result._data_nts = { label: (nts.__copy__(), id_name) for label, (nts, id_name) in self._data_nts.items() } result._losses = list(self._losses) result.loss = self.loss result._networks = self.network.recombine(other.network) result.network = result._networks[0] result._id_name = self.getNewName() return [result]
def mutate(self, prob): result = ClassifierIndividualOPACDG.__new__(ClassifierIndividualOPACDG) result.metrics = dict() result.attr = dict( [pb2attr(attr2pb(key, value)) for key, value in self.attr.items()]) result._data_nts = { label: (nts.__copy__(), id_name) for label, (nts, id_name) in self._data_nts.items() } result._losses = list(self._losses) result.loss = self.loss result.network = self.network.mutate(prob=prob)[0] result._networks = [result.network] result._id_name = self.getNewName() return [result]
def __setstate__(self, state): if isinstance(state, str) or isinstance(state, bytes): _dataset = DatasetProto() _dataset.ParseFromString(state) elif isinstance(state, DatasetProto): _dataset = state else: return cls_name = _dataset.cls_name try: self.__class__ = DatasetInterface.getClassByName(cls_name) except: pass self._id_name = _dataset.name_val attr_d = dict([pb2attr(attr) for attr in _dataset.attr_val]) self.restore_attributes(attr_d)
def __setstate__(self, state): if isinstance(state, str) or isinstance(state, bytes): _nn = NeuralNetworkProto() _nn.ParseFromString(state) elif isinstance(state, NeuralNetworkProto): _nn = state else: return self._id_name = _nn.id_name self.function_cls = [Function.getClassByName(f_cls) for f_cls in _nn.function_cls] self.output_targets = dict([pb2val(v) for v in _nn.output_ntss.v]) self._inputs = dict([(ioMP.in_label, (ioMP.out_label, ioMP.df_id_name)) for ioMP in _nn.input_mapping]) self.output_mapping = dict([(ioMP.in_label, (ioMP.out_label, ioMP.df_id_name)) for ioMP in _nn.output_mapping]) self.functions = [Function.get_instance(_f) for _f in _nn.functions] self.variable_pool = dict() for _v in _nn.variables: v_ = Variable.__new__(Variable) v_.__setstate__(_v) self.variable_pool[v_.name] = self.variable_pool.get(v_.name, []) + [v_] self.attr = dict([pb2attr(attr) for attr in _nn.attr])
def _cls_setstate(self, state): """ Individual specific reconstruction from ProtobufIndividual object or serialised ProtobufIndividual object. For class dependent attribute retrieval override this method! :param state: optional result ProtobufIndividual object to modify; if None, one is created :return: ProtobufIndividual object """ if isinstance(state, str) or isinstance(state, bytes): _individual = IndividualProto() _individual.ParseFromString(state) elif isinstance(state, IndividualProto): _individual = state else: return self._id_name = _individual.id_name self.metrics = dict([(m.id_name, m.value) for m in _individual.metrics]) self.attr = dict([ pb2attr(attr) for attr in _individual.attr if attr.name != self.arg_METRICS ])
def distinct_copy(self): result = NeuralNetwork.__new__(NeuralNetwork) result._id_name = self.id_name result.function_cls = list(self.function_cls) result.output_targets = dict([(label, value.__copy__()) for label, value in self.output_targets.items()]) result._inputs = dict(self._inputs) result.output_mapping = dict(self.output_mapping) result.functions = list() function_mapping = dict() for _f in self.functions: new_f = Function.__new__(Function) new_f.__setstate__(_f.get_pb()) new_f._id_name = new_f.getNewName(new_f) result.functions.append(new_f) function_mapping[_f.id_name] = new_f.id_name for _f in result.functions: for key, (v1, old_f_id) in _f.input_mapping.items(): _f.input_mapping[key] = (v1, function_mapping.get(old_f_id, old_f_id)) for in_label, (out_label, f_id) in result.output_mapping.items(): result.output_mapping[in_label] = (out_label, function_mapping.get(f_id, f_id)) result.variable_pool = dict([(key, list(value_l)) for key, value_l in self.variable_pool.items()]) result.attr = dict([pb2attr(attr) for attr in [attr2pb(key, value) for key, value in self.attr.items()]]) return result
def __copy__(self): result = WeightAgnosticNeuralNetwork.__new__( WeightAgnosticNeuralNetwork) result.input_mapping = { key: (l0, v.__copy__(), l1) for key, (l0, v, l1) in self.input_mapping.items() } result.output_targets = { key: ts.__copy__() for key, ts in self.output_targets.items() } result.function_cls = list(self.function_cls) result._inputs = {k: (v0, v1) for k, (v0, v1) in self._inputs.items()} result.output_mapping = { k: (v0, v1) for k, (v0, v1) in self.output_mapping.items() } result.functions = [f.__copy__() for f in self.functions] result.attr = dict( [pb2attr(attr2pb(key, value)) for key, value in self.attr.items()]) self._id_name = self.getNewName() return result