PT21-22-Reseau-Neurones/sobek/network.py

205 lines
9.7 KiB
Python
Raw Normal View History

2021-12-22 21:35:06 +01:00
import random
2021-12-02 17:34:04 +01:00
import numpy as np
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):
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
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!")
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!")
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-18 20:57:44 +01:00
if (__storeValues):
self.activations.append(_input)
#activation function application
2021-12-18 20:57:44 +01:00
#for i in range(len(_input)):
# _input[i] = network.__sigmoid(_input)
_input = network.__sigmoid(_input)
#On peut comparer la performance si on recalcul plus tard
2021-12-18 20:57:44 +01:00
if (__storeValues):
self.outputs.append(_input)
2021-12-16 17:06:51 +01:00
return _input
2021-12-09 18:50:57 +01:00
2021-12-16 17:06:51 +01:00
2021-12-22 21:35:06 +01:00
def train(self, inputs, desiredOutputs, learningRate, batchSize, epochs=1, visualize=False):
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 !")
if (visualize == False):
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-22 21:35:06 +01:00
if (visualize):
vizualisationData = []
fig, graph = plt.subplots()
2021-12-22 21:35:06 +01:00
for epoch in range(epochs):
randomState = random.getstate()
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-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):
vizualisationFrame[x][y] = self.process(np.array([float(x), float(y)]))
vizualisationData.append([graph.imshow(vizualisationFrame, animated=True)])
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]
#rempli self.activations et self.outputs
self.process(_input, True)
self.__desiredOutput = desiredOutput
#A optimiser
for layerNumber in range(len(errorSumsWeights)-1, -1, -1):
for neuronNumber in range(len(errorSumsWeights[layerNumber])):
errorSumsBiases[layerNumber][neuronNumber] += self.__Error(layerNumber, neuronNumber)
#for weightNumber in range(len(errorSumsWeights[layerNumber][neuronNumber])):
#print("layer : " + str(layerNumber) + " neuron : " + str(neuronNumber) + " weight : " + str(weightNumber))
#errorSumsWeights[layerNumber][neuronNumber][weightNumber] += self.__PartialDerivative(layerNumber, neuronNumber, weightNumber)
#errorSumsWeights[layerNumber][neuronNumber][weightNumber] = errorSumsBiases[layerNumber][neuronNumber] * self.outputs[layerNumber][weightNumber]
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])
#print(self.__biases)
"""
for layerNumber in range(len(errorSumsWeights)):
for neuronNumber in range(len(errorSumsWeights[layerNumber])):
errorSumsBiases[layerNumber][neuronNumber] = errorSumsBiases[layerNumber][neuronNumber] / len(inputBatch)
total += errorSumsBiases[layerNumber][neuronNumber]
self.biases[layerNumber][neuronNumber] -= learningRate * errorSumsBiases[layerNumber][neuronNumber]
for weightNumber in range(len(errorSumsWeights[layerNumber][neuronNumber])):
#Probablement faisable avec une multiplication de matrices
errorSumsWeights[layerNumber][neuronNumber][weightNumber] = errorSumsWeights[layerNumber][neuronNumber][weightNumber] / len(inputBatch)
#total += errorSumsWeights[layerNumber][neuronNumber][weightNumber]
#Probablement faisable avec une somme de matrices
self.weights[layerNumber][neuronNumber][weightNumber] -= learningRate * errorSumsWeights[layerNumber][neuronNumber][weightNumber]
#print("Error : " + str(total))"""
if (visualize):
ani = animation.ArtistAnimation(fig, vizualisationData, interval=100)
plt.show()
2021-12-14 10:44:48 +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])
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-22 21:35:06 +01:00
#def __PartialDerivative(self, layer, neuron, weight):
# return self.__Error(layer, neuron) * self.outputs[layer][weight]
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)
def saveToFile(self, fileName):
2021-12-22 21:35:06 +01:00
with open(fileName, "wb") as file:
pickle.dump(self, file)
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)