summaryrefslogtreecommitdiff
path: root/neural_network.py
diff options
context:
space:
mode:
Diffstat (limited to 'neural_network.py')
-rw-r--r--neural_network.py24
1 files changed, 16 insertions, 8 deletions
diff --git a/neural_network.py b/neural_network.py
index 68592f5..3dbd94d 100644
--- a/neural_network.py
+++ b/neural_network.py
@@ -1,7 +1,7 @@
import numpy as np
+import math
from scipy.special import expit
-from utils import random_array
-from dataset_utils import DatasetUtils
+from dataset import Dataset
CYRILLIC_ALPHABET = ['I', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ë', 'Ж', 'З',
'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С',
@@ -9,14 +9,16 @@ CYRILLIC_ALPHABET = ['I', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ë', 'Ж', 'З',
'Ь', 'Э', 'Ю', 'Я']
"""The neural network class."""
+
+
class NeuralNetwork:
def __init__(self, learning_rate: float, input_resolution: int) -> None:
self.learning_rate = learning_rate
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)
-
+ self._hidden_weights = self._random_array(self.hidden_layer_size,
+ input_resolution)
+ self._output_weights = self._random_array(input_resolution, 1)
"""
Train the neural network. It loads the dataset contained in ./data,
@@ -57,10 +59,10 @@ class NeuralNetwork:
Feedforwarding.
"""
def _predict(self, input_layer: np.array):
- hidden_layer_inputs = np.dot(input_layer, self._hidden_weights)
+ 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(self._output_weights, hidden_layer_outputs)
-
+ 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)
@@ -70,6 +72,12 @@ class NeuralNetwork:
def _get_layer_output(self, layer: np.array):
return expit(layer)
+ 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).