This commit is contained in:
eynard 2021-12-22 22:08:20 +01:00
parent 7733de01d2
commit 151343b7bd
11 changed files with 22 additions and 61 deletions

View File

@ -51,11 +51,8 @@ class network:
self.activations.append(_input) self.activations.append(_input)
#activation function application #activation function application
#for i in range(len(_input)):
# _input[i] = network.__sigmoid(_input)
_input = network.__sigmoid(_input) _input = network.__sigmoid(_input)
#On peut comparer la performance si on recalcul plus tard
if (__storeValues): if (__storeValues):
self.outputs.append(_input) self.outputs.append(_input)
@ -110,18 +107,14 @@ class network:
errorSumsBiases = [np.zeros(layer.shape) for layer in self.biases] errorSumsBiases = [np.zeros(layer.shape) for layer in self.biases]
self.__errors = [np.zeros(len(layer)) for layer in self.weights] self.__errors = [np.zeros(len(layer)) for layer in self.weights]
#rempli self.activations et self.outputs #Rempli self.activations et self.outputs
self.process(_input, True) self.process(_input, True)
self.__desiredOutput = desiredOutput self.__desiredOutput = desiredOutput
#A optimiser
for layerNumber in range(len(errorSumsWeights)-1, -1, -1): for layerNumber in range(len(errorSumsWeights)-1, -1, -1):
for neuronNumber in range(len(errorSumsWeights[layerNumber])): for neuronNumber in range(len(errorSumsWeights[layerNumber])):
errorSumsBiases[layerNumber][neuronNumber] += self.__Error(layerNumber, neuronNumber) 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]) errorSumsWeights[layerNumber][neuronNumber] = np.dot(errorSumsBiases[layerNumber][neuronNumber],self.outputs[layerNumber])
total = 0 total = 0
@ -133,27 +126,6 @@ class network:
errorSumsBiases[layerNumber] = np.multiply(errorSumsBiases[layerNumber], -(learningRate/len(inputBatch))) errorSumsBiases[layerNumber] = np.multiply(errorSumsBiases[layerNumber], -(learningRate/len(inputBatch)))
self.biases[layerNumber] = np.add(self.biases[layerNumber], errorSumsBiases[layerNumber]) 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): if (visualize):
ani = animation.ArtistAnimation(fig, vizualisationData, interval=100) ani = animation.ArtistAnimation(fig, vizualisationData, interval=100)
plt.show() plt.show()
@ -172,9 +144,6 @@ class network:
upperLayerLinksSum += self.weights[layer+1][upperLayerNeuron][neuron] * self.__errors[layer+1][upperLayerNeuron] upperLayerLinksSum += self.weights[layer+1][upperLayerNeuron][neuron] * self.__errors[layer+1][upperLayerNeuron]
return network.__sigmoid(self.activations[layer][neuron], derivative=True) * upperLayerLinksSum return network.__sigmoid(self.activations[layer][neuron], derivative=True) * upperLayerLinksSum
#def __PartialDerivative(self, layer, neuron, weight):
# return self.__Error(layer, neuron) * self.outputs[layer][weight]
def accuracy(self, inputs, desiredOutputs): def accuracy(self, inputs, desiredOutputs):
if (type(inputs) != list or type(desiredOutputs) != list): if (type(inputs) != list or type(desiredOutputs) != list):
raise TypeError("The inputs and desired outputs must be lists of numpy arrays !") raise TypeError("The inputs and desired outputs must be lists of numpy arrays !")

14
test.py
View File

@ -1,14 +0,0 @@
import numpy as np
from sobek.network import network
test = network(16, 16, 8, 4)
"""
for y in test.weights:
print(y, end="\n\n")
for y in test.biases:
print(y, end="\n\n")"""
#print(network.__reLu(8))
print(test.process(np.random.default_rng(42).random((16))))

View File

@ -1,7 +1,11 @@
import tkinter import tkinter
from PIL import Image, ImageDraw from PIL import Image, ImageDraw
from sobek.network import network
import numpy as np import numpy as np
from sys import path
path.insert(1, "..")
from sobek.network import network
class Sketchpad(tkinter.Canvas): class Sketchpad(tkinter.Canvas):
def __init__(self, parent, predictionLabel, **kwargs, ): def __init__(self, parent, predictionLabel, **kwargs, ):

View File

@ -1,7 +1,10 @@
import numpy as np import numpy as np
from sobek.network import network
import gzip import gzip
import time import time
from sys import path
path.insert(1, "..")
from sobek.network import network
print("--- Data loading ---") print("--- Data loading ---")

View File

@ -1,6 +1,8 @@
import numpy as np import numpy as np
from sobek.network import network
import gzip import gzip
from sys import path
path.insert(1, "..")
from sobek.network import network
print("--- Data loading ---") print("--- Data loading ---")

View File

@ -1,5 +1,7 @@
import numpy as np import numpy as np
import random import random
from sys import path
path.insert(1, "..")
from sobek.network import network from sobek.network import network
random.seed() random.seed()

View File

@ -1,7 +1,9 @@
import numpy as np import numpy as np
import random import random
from sobek.network import network
import time import time
from sys import path
path.insert(1, "..")
from sobek.network import network
random.seed() random.seed()
@ -33,20 +35,12 @@ for i in range(nbRep):
startTime = time.perf_counter() startTime = time.perf_counter()
#for j in range(10000):
# inputs = []
# desiredOutputs = []
#if (j%1000 == 0):
# print(j)
# myNetwork.train(test, result, learningRate)
myNetwork.train(test, result, learningRate, len(test), 10000, visualize=False) myNetwork.train(test, result, learningRate, len(test), 10000, visualize=False)
endTime = time.perf_counter() endTime = time.perf_counter()
learningTime += endTime - startTime learningTime += endTime - startTime
learningTime = learningTime / nbRep learningTime = learningTime / nbRep
test = [] test = []
result = [] result = []
test.append(np.zeros(2)) test.append(np.zeros(2))

View File

@ -1,5 +1,6 @@
import numpy as np import numpy as np
import random from sys import path
path.insert(1, "..")
from sobek.network import network from sobek.network import network
myNetwork = network(2, 1) myNetwork = network(2, 1)