summaryrefslogtreecommitdiff
path: root/neural_network.py
diff options
context:
space:
mode:
Diffstat (limited to 'neural_network.py')
-rw-r--r--neural_network.py25
1 files changed, 17 insertions, 8 deletions
diff --git a/neural_network.py b/neural_network.py
index 90fd304..68592f5 100644
--- a/neural_network.py
+++ b/neural_network.py
@@ -1,6 +1,7 @@
import numpy as np
from scipy.special import expit
from utils import random_array
+from dataset_utils import DatasetUtils
CYRILLIC_ALPHABET = ['I', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ë', 'Ж', 'З',
'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С',
@@ -11,11 +12,10 @@ CYRILLIC_ALPHABET = ['I', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ë', 'Ж', 'З',
class NeuralNetwork:
def __init__(self, learning_rate: float, input_resolution: int) -> None:
self.learning_rate = learning_rate
- self.output_layer_size = len(CYRILLIC_ALPHABET)
- self.input_layer_size = input_resolution ** 2
- self.hidden_layer_size = round((self.input_layer_size + self.output_layer_size) / 2)
- self._hidden_weights = random_array(self.hidden_layer_size, self.input_layer_size)
- self._output_weights = random_array(self.output_layer_size, self.hidden_layer_size)
+ self.input_layer_size = input_resolution
+ self.hidden_layer_size = len(CYRILLIC_ALPHABET)
+ self._hidden_weights = np.random.rand(input_resolution, self.hidden_layer_size)
+ self._output_weights = np.random.rand(input_resolution)
"""
@@ -36,8 +36,10 @@ class NeuralNetwork:
Guess the letter contained in the image file pointed by
input_image (a path).
"""
- def guess(self, input_image: str) -> str:
- pass
+ def guess(self, input_image: np.array) -> str:
+ output_layer = self._predict(input_image)
+
+ return self.out(output_layer)
"""
Save the weights to a csv file.
@@ -58,7 +60,7 @@ class NeuralNetwork:
hidden_layer_inputs = np.dot(input_layer, self._hidden_weights)
hidden_layer_outputs = self._get_layer_output(hidden_layer_inputs)
output_layer_inputs = np.dot(self._output_weights, hidden_layer_outputs)
-
+
# The output layer outputs. (Final output of the neural network).
return self._get_layer_output(output_layer_inputs)
@@ -67,3 +69,10 @@ class NeuralNetwork:
"""
def _get_layer_output(self, layer: np.array):
return expit(layer)
+
+ """
+ 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))]