import numpy as np from scipy.special import expit from utils import random_array from dataset_utils import DatasetUtils 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) """ Train the neural network. It loads the dataset contained in ./data, converts each image into a numpy array and uses that data for training. Algorithm: 1-. Feedforward the input matrix. 2-. Calculate the output layer error. 3-. Adjust the weights of the output layer via gradient descent. 3-. Calculate the hidden layer error by backpropagating the output layer error. 4-. Adjust the weights of the hidden layer via gradient descent. """ def train(self): pass """ Guess the letter contained in the image file pointed by input_image (a path). """ 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. """ def save(self): pass """ Load the weights from a csv file. """ def load(self, weights_file: str): pass """ Feedforwarding. """ def _predict(self, input_layer: np.array): 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) """ Apply the sigmoid function to a given layer """ 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))]