Un bout de l'entrainement (erreur + partial derivative 1)

This commit is contained in:
eynard 2021-12-14 22:26:11 +01:00
parent a96908daf4
commit 7e9a933548

View File

@ -23,7 +23,7 @@ class network:
def __sigmoid(value, derivative=False): def __sigmoid(value, derivative=False):
if (derivative): if (derivative):
return __sigmoid(value) * (1 - __sigmoid(value)) return network.__sigmoid(value) * (1 - network.__sigmoid(value))
return 1/(1+np.exp(-value)) return 1/(1+np.exp(-value))
def process(self, input, storeValues=False): def process(self, input, storeValues=False):
@ -34,23 +34,46 @@ class network:
if input.dtype != np.float64: if input.dtype != np.float64:
raise TypeError("The input vector must contain floats!") raise TypeError("The input vector must contain floats!")
if (storeValues):
self.activations = []
self.outputs = []
for layerWeights, bias in zip(self.__weights, self.__biases): for layerWeights, bias in zip(self.__weights, self.__biases):
input = np.matmul(input, layerWeights) input = np.matmul(input, layerWeights)
input = np.add(input, bias) input = np.add(input, bias)
if (storeValues):
self.activations.append(input)
#reLu application #reLu application
with np.nditer(input, op_flags=['readwrite']) as layer: with np.nditer(input, op_flags=['readwrite']) as layer:
for neuron in layer: for neuron in layer:
neuron = network.__reLu(neuron) neuron = network.__reLu(neuron)
#On peut comparer la performance si on recalcul plus tard
if (storeValues):
self.outputs.append(input)
return input return input
def train(self, inputs, results): def train(self, inputs, desiredOutputs):
self.__outputs = 1 for input, desiredOutput in zip(inputs, desiredOutputs):
#for j in range(1,): self.__output = self.process(input, True)
self.__desiredOutput = desiredOutput
#partialDerivatives #partialDerivatives
def __Error(layer, output, desiredOutput): def __Error(self, layer, neuron):
return __ErrorFinalLayerFromValue() if (layer == 1) return self.__ErrorFinalLayer(neuron) if (layer == 1) else self.__ErrorHiddenLayer(layer, neuron)
def __ErrorFinalLayer(self, neuron): def __ErrorFinalLayer(self, neuron):
return __reLu(value, true) * (output - desiredOutput) return network.__reLu(self.activations[len(self.activations)-1][neuron], True) * (self.__output[neuron] - self.__desiredOutput[neuron])
def __ErrorHiddenLayer(self, layer, neuron):
upperLayerLinksSum = 0
for upperLayerNeuron in range(len(self.__weights[layer+1]-1)):
#A comparer avec un acces direct au erreurs precalcules
upperLayerLinksSum += self.__weights[layer+1][upperLayerNeuron][neuron] * self.__Error(layer+1, neuron)
return network.__reLu(self.activations[layer][neuron], True) * upperLayerLinksSum
def __partialDerivative(self, layer, neuron):
return self.__Error(layer, neuron) * self.outputs[layer][neuron]