2021-12-22 21:35:06 +01:00
|
|
|
import random
|
2021-12-02 17:34:04 +01:00
|
|
|
import numpy as np
|
2022-03-10 15:09:20 +01:00
|
|
|
import matplotlib
|
2021-12-22 21:35:06 +01:00
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import matplotlib.animation as animation
|
|
|
|
import pickle
|
2021-12-02 17:34:04 +01:00
|
|
|
|
|
|
|
class network:
|
|
|
|
|
|
|
|
def __init__(self, inputLayerSize, *layerSizes):
|
2021-12-03 15:10:27 +01:00
|
|
|
if type(inputLayerSize) != int:
|
|
|
|
raise TypeError("The input layer size must be an int!")
|
|
|
|
|
2021-12-18 20:57:44 +01:00
|
|
|
self.weights = []
|
2021-12-09 18:50:57 +01:00
|
|
|
self.__inputLayerSize = inputLayerSize
|
|
|
|
oldLayerSize = inputLayerSize
|
2021-12-02 17:34:04 +01:00
|
|
|
for layerSize in layerSizes:
|
2021-12-18 20:57:44 +01:00
|
|
|
self.weights.append( np.random.randn(layerSize, oldLayerSize) )
|
2021-12-09 18:50:57 +01:00
|
|
|
oldLayerSize = layerSize
|
2021-12-18 20:57:44 +01:00
|
|
|
self.biases = [np.random.randn(layerSize) for layerSize in layerSizes]
|
2021-12-02 17:34:04 +01:00
|
|
|
|
2021-12-14 10:44:48 +01:00
|
|
|
def __reLu(value, derivative=False):
|
|
|
|
if (derivative):
|
2021-12-18 20:57:44 +01:00
|
|
|
return 0 if (value < 0) else 1
|
2021-12-02 17:34:04 +01:00
|
|
|
return max(0, value)
|
|
|
|
|
2021-12-14 10:44:48 +01:00
|
|
|
def __sigmoid(value, derivative=False):
|
|
|
|
if (derivative):
|
2021-12-14 22:26:11 +01:00
|
|
|
return network.__sigmoid(value) * (1 - network.__sigmoid(value))
|
2021-12-18 20:57:44 +01:00
|
|
|
return 1.0/(1.0+np.exp(-value))
|
2021-12-14 10:44:48 +01:00
|
|
|
|
2021-12-15 16:15:16 +01:00
|
|
|
def process(self, _input, __storeValues=False):
|
|
|
|
if type(_input) != np.ndarray:
|
2021-12-22 21:35:06 +01:00
|
|
|
raise TypeError("The input must be a numpy array!")
|
2021-12-15 16:15:16 +01:00
|
|
|
if _input.size != self.__inputLayerSize:
|
2021-12-22 21:35:06 +01:00
|
|
|
raise ValueError("The input vector has the wrong size! " + str(_input.size) + " != " + str(self.__inputLayerSize))
|
2021-12-18 20:57:44 +01:00
|
|
|
if _input.dtype != np.float64:
|
|
|
|
print(_input.dtype)
|
|
|
|
raise TypeError("The input vector must contain floats!")
|
2021-12-14 22:26:11 +01:00
|
|
|
|
2021-12-15 16:15:16 +01:00
|
|
|
if (__storeValues):
|
2021-12-16 17:06:51 +01:00
|
|
|
self.activations = []
|
|
|
|
self.outputs = []
|
2021-12-18 20:57:44 +01:00
|
|
|
self.outputs.append(_input)
|
2021-12-03 15:10:27 +01:00
|
|
|
|
2021-12-18 20:57:44 +01:00
|
|
|
for layerWeights, layerBias in zip(self.weights, self.biases):
|
2021-12-16 17:06:51 +01:00
|
|
|
|
2021-12-18 20:57:44 +01:00
|
|
|
_input = np.dot(layerWeights, _input)
|
|
|
|
_input = np.add(_input, layerBias)
|
2021-12-14 22:26:11 +01:00
|
|
|
|
2021-12-18 20:57:44 +01:00
|
|
|
if (__storeValues):
|
|
|
|
self.activations.append(_input)
|
2021-12-14 22:26:11 +01:00
|
|
|
|
2021-12-16 23:05:27 +01:00
|
|
|
#activation function application
|
2021-12-18 20:57:44 +01:00
|
|
|
_input = network.__sigmoid(_input)
|
2021-12-14 22:26:11 +01:00
|
|
|
|
2021-12-18 20:57:44 +01:00
|
|
|
if (__storeValues):
|
|
|
|
self.outputs.append(_input)
|
2021-12-16 17:06:51 +01:00
|
|
|
|
2021-12-15 16:15:16 +01:00
|
|
|
return _input
|
2021-12-09 18:50:57 +01:00
|
|
|
|
2021-12-16 17:06:51 +01:00
|
|
|
|
|
|
|
|
2022-01-06 12:13:22 +01:00
|
|
|
def train(self, inputs, desiredOutputs, learningRate, batchSize, epochs=1, accuracyInputs=False, accuracyDesiredOutputs=False, visualize=False):
|
2021-12-22 21:35:06 +01:00
|
|
|
if (type(inputs) != list or type(desiredOutputs) != list):
|
|
|
|
raise TypeError("The inputs and desired outputs must be lists of numpy arrays !")
|
2021-12-18 20:57:44 +01:00
|
|
|
if (len(inputs) != len(desiredOutputs)):
|
2021-12-22 21:35:06 +01:00
|
|
|
raise ValueError("The inputs and desired outputs lists must have the same amount of data ! " + str(len(inputs)) + " != " + str(len(desiredOutputs)))
|
|
|
|
if (len(inputs) == 0):
|
|
|
|
raise ValueError("The list is empty !")
|
2022-01-06 12:13:22 +01:00
|
|
|
if (visualize != False):
|
2021-12-22 21:35:06 +01:00
|
|
|
if (self.__inputLayerSize != 2):
|
|
|
|
raise ValueError("Visualization is only possible for 2 inputs networks")
|
|
|
|
if (len(self.weights[-1]) != 1):
|
|
|
|
raise ValueError("Visualization is only possible for 1 output networks")
|
2021-12-16 17:06:51 +01:00
|
|
|
|
2021-12-22 21:35:06 +01:00
|
|
|
errorSumsWeights = []
|
|
|
|
errorSumsBiases = []
|
2021-12-16 23:05:27 +01:00
|
|
|
|
2021-12-22 21:35:06 +01:00
|
|
|
if (visualize):
|
|
|
|
vizualisationData = []
|
|
|
|
fig, graph = plt.subplots()
|
2021-12-16 23:05:27 +01:00
|
|
|
|
2021-12-22 21:35:06 +01:00
|
|
|
for epoch in range(epochs):
|
|
|
|
randomState = random.getstate()
|
2021-12-16 23:05:27 +01:00
|
|
|
|
2021-12-22 21:35:06 +01:00
|
|
|
random.shuffle(inputs)
|
2021-12-16 17:06:51 +01:00
|
|
|
|
2021-12-22 21:35:06 +01:00
|
|
|
random.setstate(randomState)
|
2021-12-18 20:57:44 +01:00
|
|
|
|
2021-12-22 21:35:06 +01:00
|
|
|
random.shuffle(desiredOutputs)
|
2021-12-16 23:05:27 +01:00
|
|
|
|
2021-12-22 21:35:06 +01:00
|
|
|
if (visualize and epoch%10 == 0):
|
|
|
|
vizualisationFrame = np.empty((30, 30))
|
|
|
|
for x in range(30):
|
|
|
|
for y in range(30):
|
2022-03-10 15:09:20 +01:00
|
|
|
vizualisationFrame[x][y] = self.process(np.array([float(x)/30, float(y)/30]))
|
2021-12-22 21:35:06 +01:00
|
|
|
vizualisationData.append([graph.imshow(vizualisationFrame, animated=True)])
|
2021-12-16 23:05:27 +01:00
|
|
|
|
2021-12-22 21:35:06 +01:00
|
|
|
inputBatches = [inputs[j:j+batchSize] for j in range(0, len(inputs), batchSize)]
|
|
|
|
desiredOutputsBatches = [desiredOutputs[j:j+batchSize] for j in range(0, len(inputs), batchSize)]
|
2021-12-16 17:06:51 +01:00
|
|
|
|
2021-12-22 21:35:06 +01:00
|
|
|
for inputBatch, desiredOutputsBatch in zip(inputBatches, desiredOutputsBatches):
|
|
|
|
|
|
|
|
for _input, desiredOutput in zip(inputBatch, desiredOutputsBatch):
|
|
|
|
|
|
|
|
errorSumsWeights = [np.zeros(layer.shape) for layer in self.weights]
|
|
|
|
errorSumsBiases = [np.zeros(layer.shape) for layer in self.biases]
|
|
|
|
self.__errors = [np.zeros(len(layer)) for layer in self.weights]
|
|
|
|
|
2021-12-22 22:08:20 +01:00
|
|
|
#Rempli self.activations et self.outputs
|
2021-12-22 21:35:06 +01:00
|
|
|
self.process(_input, True)
|
2021-12-22 22:08:20 +01:00
|
|
|
|
2021-12-22 21:35:06 +01:00
|
|
|
self.__desiredOutput = desiredOutput
|
|
|
|
|
|
|
|
for layerNumber in range(len(errorSumsWeights)-1, -1, -1):
|
|
|
|
for neuronNumber in range(len(errorSumsWeights[layerNumber])):
|
|
|
|
errorSumsBiases[layerNumber][neuronNumber] += self.__Error(layerNumber, neuronNumber)
|
|
|
|
errorSumsWeights[layerNumber][neuronNumber] = np.dot(errorSumsBiases[layerNumber][neuronNumber],self.outputs[layerNumber])
|
|
|
|
|
|
|
|
total = 0
|
|
|
|
|
|
|
|
for layerNumber in range(len(errorSumsWeights)):
|
|
|
|
errorSumsWeights[layerNumber] = np.multiply(errorSumsWeights[layerNumber], -(learningRate/len(inputBatch)))
|
|
|
|
self.weights[layerNumber] = np.add(self.weights[layerNumber], errorSumsWeights[layerNumber])
|
|
|
|
|
|
|
|
errorSumsBiases[layerNumber] = np.multiply(errorSumsBiases[layerNumber], -(learningRate/len(inputBatch)))
|
|
|
|
self.biases[layerNumber] = np.add(self.biases[layerNumber], errorSumsBiases[layerNumber])
|
|
|
|
|
2022-01-06 12:13:22 +01:00
|
|
|
'''errorSum = 0
|
|
|
|
for layer in errorSumsBiases:
|
|
|
|
errorSum += np.sum(np.abs(layer))
|
|
|
|
print(errorSum)'''
|
|
|
|
|
|
|
|
if (accuracyInputs and accuracyDesiredOutputs):
|
|
|
|
print(self.accuracy(accuracyInputs, accuracyDesiredOutputs))
|
|
|
|
|
2021-12-22 21:35:06 +01:00
|
|
|
if (visualize):
|
2022-03-10 15:09:20 +01:00
|
|
|
ani = animation.ArtistAnimation(fig, vizualisationData, interval=100, repeat_delay=1000)
|
2021-12-22 21:35:06 +01:00
|
|
|
plt.show()
|
2021-12-14 10:44:48 +01:00
|
|
|
|
2021-12-14 22:26:11 +01:00
|
|
|
def __Error(self, layer, neuron):
|
2021-12-16 17:06:51 +01:00
|
|
|
if (self.__errors[layer][neuron] == 0 ):
|
2021-12-18 20:57:44 +01:00
|
|
|
self.__errors[layer][neuron] = self.__ErrorFinalLayer(neuron) if (layer == len(self.weights)-1) else self.__ErrorHiddenLayer(layer, neuron)
|
2021-12-16 17:06:51 +01:00
|
|
|
return self.__errors[layer][neuron]
|
2021-12-14 10:44:48 +01:00
|
|
|
|
|
|
|
def __ErrorFinalLayer(self, neuron):
|
2021-12-18 20:57:44 +01:00
|
|
|
return network.__sigmoid(self.activations[-1][neuron], derivative=True) * (self.outputs[-1][neuron] - self.__desiredOutput[neuron])
|
2021-12-14 22:26:11 +01:00
|
|
|
|
|
|
|
def __ErrorHiddenLayer(self, layer, neuron):
|
|
|
|
upperLayerLinksSum = 0
|
2021-12-18 20:57:44 +01:00
|
|
|
for upperLayerNeuron in range(len(self.weights[layer+1])):
|
|
|
|
upperLayerLinksSum += self.weights[layer+1][upperLayerNeuron][neuron] * self.__errors[layer+1][upperLayerNeuron]
|
|
|
|
return network.__sigmoid(self.activations[layer][neuron], derivative=True) * upperLayerLinksSum
|
2021-12-14 22:26:11 +01:00
|
|
|
|
2021-12-22 21:35:06 +01:00
|
|
|
def accuracy(self, inputs, desiredOutputs):
|
|
|
|
if (type(inputs) != list or type(desiredOutputs) != list):
|
|
|
|
raise TypeError("The inputs and desired outputs must be lists of numpy arrays !")
|
|
|
|
if (len(inputs) != len(desiredOutputs)):
|
|
|
|
raise ValueError("The inputs and desired outputs lists must have the same amount of data !")
|
|
|
|
if (len(inputs) == 0):
|
|
|
|
raise ValueError("The list is empty !")
|
|
|
|
sum = 0
|
|
|
|
for i in range(len(desiredOutputs)):
|
|
|
|
if (np.argmax(desiredOutputs[i]) == np.argmax(self.process(inputs[i]))):
|
|
|
|
sum += 1
|
|
|
|
return sum/len(desiredOutputs)
|
2021-12-18 23:26:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def saveToFile(self, fileName):
|
2021-12-22 21:35:06 +01:00
|
|
|
with open(fileName, "wb") as file:
|
|
|
|
pickle.dump(self, file)
|
2021-12-18 23:26:57 +01:00
|
|
|
|
|
|
|
def loadFromFile(self, fileName):
|
2021-12-22 21:35:06 +01:00
|
|
|
with open(fileName, "rb") as file:
|
|
|
|
fromNetwork = pickle.load(file)
|
|
|
|
self.weights = fromNetwork.weights
|
|
|
|
self.biases = fromNetwork.biases
|
|
|
|
self.__inputLayerSize = fromNetwork.__inputLayerSize
|
|
|
|
|
|
|
|
def networkFromFile(fileName):
|
|
|
|
with open(fileName, "rb") as file:
|
|
|
|
return pickle.load(file)
|