From f403d428c336811af66c78b1b79652c82dbca8d4 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Fri, 20 Oct 2023 21:16:45 -0600 Subject: Tidy up the NeuralNetwork class --- neural_network.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/neural_network.py b/neural_network.py index 3dbd94d..1408a91 100644 --- a/neural_network.py +++ b/neural_network.py @@ -39,14 +39,14 @@ class NeuralNetwork: input_image (a path). """ def guess(self, input_image: np.array) -> str: - output_layer = self._predict(input_image) + output_layer = self._feedforward(input_image) - return self.out(output_layer) + return self.guessed_char(output_layer) """ Save the weights to a csv file. """ - def save(self): + def save(self, weights_filename): pass """ @@ -55,13 +55,21 @@ class NeuralNetwork: def load(self, weights_file: str): pass + """ + Get the result from a sigmoid matrix (the index with the highest chance + of being the correct answer). + """ + def _guessed_char(self, output_layer: np.array) -> str: + return CYRILLIC_ALPHABET[np.argmax(np.transpose(output_layer))] + """ Feedforwarding. """ - def _predict(self, input_layer: np.array): + def _feedforward(self, input_layer: np.array): hidden_layer_inputs = np.dot(self._hidden_weights, input_layer) hidden_layer_outputs = self._get_layer_output(hidden_layer_inputs) - output_layer_inputs = np.dot(hidden_layer_outputs, self._output_weights) + output_layer_inputs = np.dot(hidden_layer_outputs, + self._output_weights) # The output layer outputs. (Final output of the neural network). return self._get_layer_output(output_layer_inputs) @@ -72,15 +80,11 @@ class NeuralNetwork: def _get_layer_output(self, layer: np.array): return expit(layer) + """ + Generate a random array via an uniform distribution. + """ def _random_array(self, rows: int, columns: int) -> np.array: low = -1 / math.sqrt(rows) high = 1 / math.sqrt(columns) return np.random.uniform(low, high, (rows, columns)) - - """ - Get the result from a sigmoid matrix (the index with the highest chance - of being the correct answer). - """ - def out(self, output_layer: np.array): - return CYRILLIC_ALPHABET[np.argmax(np.transpose(output_layer))] -- cgit v1.2.3