summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-10-20 21:09:08 -0600
committerHombreLaser <sebastian-440@live.com>2023-10-20 21:09:08 -0600
commit565dd0f695b46b49b9ec32874339b979b1c9d341 (patch)
tree26be0e3ac5bd836ca019958adb7078cca534d914
parent882fa5726e1c2e02ad2ba543087f204f10694e8a (diff)
Fix weights generation
-rw-r--r--main.py6
-rw-r--r--neural_network.py24
2 files changed, 19 insertions, 11 deletions
diff --git a/main.py b/main.py
index 9ca0a13..a44fbcb 100644
--- a/main.py
+++ b/main.py
@@ -1,12 +1,12 @@
-from PIL import Image
-from dataset_utils import DatasetUtils
+from dataset import Dataset
from neural_network import NeuralNetwork
LEARNING_RATE = .5
INPUT_RESOLUTION = 278
+
def main():
- data = DatasetUtils()
+ data = Dataset()
image = data.get_image('А/5a2f3c19c27bb.png')
neural_network = NeuralNetwork(LEARNING_RATE, INPUT_RESOLUTION)
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).