diff options
author | HombreLaser <sebastian-440@live.com> | 2023-10-20 21:16:45 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2023-10-20 21:16:45 -0600 |
commit | f403d428c336811af66c78b1b79652c82dbca8d4 (patch) | |
tree | 7c87ee557c13d0afdf786355e51bbefc039dfeb5 | |
parent | 565dd0f695b46b49b9ec32874339b979b1c9d341 (diff) |
Tidy up the NeuralNetwork class
-rw-r--r-- | neural_network.py | 28 |
1 files 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 """ @@ -56,12 +56,20 @@ class NeuralNetwork: 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))] |