def output_from(self, input_states: iter) -> tuple: """Return output states responding to given input neuron states. states: iterable of booleans, giving the state of input neurons. return: iterable of booleans, giving the state of output neurons. """ # Atoms creation: # - define the neural network # - add an atom up/1 or down/1 foreach input neuron according to its state input_atoms = self.neural_network + ''.join( 'up(' + str(idn) + ').' # position in list gives the input neuron id for idn, is_up in enumerate(input_states, start=MINIMAL_NEURON_ID) if is_up) LOGGER.debug('INPUT ATOMS: "' + input_atoms + '"') # ASP solver call model = solving.model_from(input_atoms, FILE_ASP_RUNNING) LOGGER.debug('OUTPUT ATOMS: ' + str(model)) up_outputs = set( int(atom.split('(')[1].strip(')')) for atom in model if atom.startswith('up')) min_output_neuron_id = self.min_output_neuron_id ret = tuple( idx in up_outputs for idx in range(min_output_neuron_id, self.maximal_neuron_id + 1)) LOGGER.debug('OUTPUT STATES: ' + str(ret)) return ret
def output_from(self, input_states:iter) -> tuple: """Return output states responding to given input neuron states. states: iterable of booleans, giving the state of input neurons. return: iterable of booleans, giving the state of output neurons. """ # Atoms creation: # - define the neural network # - add an atom up/1 or down/1 foreach input neuron according to its state input_atoms = self.neural_network + ''.join( 'up(' + str(idn) + ').' # position in list gives the input neuron id for idn, is_up in enumerate(input_states, start=MINIMAL_NEURON_ID) if is_up ) LOGGER.debug('INPUT ATOMS: "' + input_atoms + '"') # ASP solver call model = solving.model_from(input_atoms, FILE_ASP_RUNNING) LOGGER.debug('OUTPUT ATOMS: ' + str(model)) up_outputs = set(int(atom.split('(')[1].strip(')')) for atom in model if atom.startswith('up')) min_output_neuron_id = self.min_output_neuron_id ret = tuple(idx in up_outputs for idx in range(min_output_neuron_id, self.maximal_neuron_id+1)) LOGGER.debug('OUTPUT STATES: ' + str(ret)) return ret
def cleaned(network_atoms: str) -> str: """Return a cleaned version of given neural network to remove useless neuron, give an orientation to edges,...""" model = solving.model_from(network_atoms, FILE_ASP_CLEANING) assert model is not None, 'cleaning network lead to non existing model' return '.'.join(model) + ('.' if len(model) else '')
def _run(self, atoms): return model_from(atoms, FILE_ASP_RUNNING)
def _clean(self, atoms): return model_from(atoms, FILE_ASP_CLEANING)
def cleaned(network_atoms:str) ->str: """Return a cleaned version of given neural network to remove useless neuron, give an orientation to edges,...""" model = solving.model_from(network_atoms, FILE_ASP_CLEANING) assert model is not None, 'cleaning network lead to non existing model' return '.'.join(model) + ('.' if len(model) else '')