From 565dd0f695b46b49b9ec32874339b979b1c9d341 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Fri, 20 Oct 2023 21:09:08 -0600 Subject: Fix weights generation --- neural_network.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'neural_network.py') 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). -- cgit v1.2.3