Commit e2a2c585 authored by Petr Kubeš's avatar Petr Kubeš

just a quick addition of regularization

parent 56adde23
This source diff could not be displayed because it is too large. You can view the blob instead.
export interface Activation {
der(x: number): number;
output(x: number): number;
}
export class Activations {
public static SIGMOID = {
public static SIGMOID: Activation = {
output: (x: number): number => 1 / (1 + Math.exp(-x)),
der: (x: number): number => {
let output = Activations.SIGMOID.output(x);
......
......@@ -10,6 +10,7 @@ export class NeuralCore {
private layerCnt: number;
private rate = 1;
private lambda = 0.003;
private biasNeuron = new Neuron('bias', true);
private neurons: Neuron[][] = [];
......@@ -94,7 +95,11 @@ export class NeuralCore {
}, 0);
}, 0);
return 1 / 2 * costSum * (1 / this.trainSamples.length);
return 1 / 2 * costSum * (1 / this.trainSamples.length) +
1 / 2 * this.lambda * this.connections.reduce( // Regularization
(prev, connLayer: Connection[]) => {
return prev + connLayer.reduce((acc, conn) => acc + conn.getWeight(), 0) ** 2
}, 0) * (1 / this.getNumberOfConnections());
}
public train() {
......@@ -126,7 +131,8 @@ export class NeuralCore {
const weightChange =
connection.getOutputNeuron().getSigma() *
connection.getInputNeuron().getActivation() *
this.rate;
this.rate -
this.lambda * connection.getWeight() * (1 / this.getNumberOfConnections()); // Regularization
connection.addSampleWeightChange(weightChange);
});
......@@ -304,6 +310,10 @@ export class NeuralCore {
}
}
private getNumberOfConnections():number {
return this.connections.reduce((acc, conn) => acc + conn.length, 0);
}
public getNeurons() {
return this.neurons;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment