Initial commit
This commit is contained in:
57
src/main/java/com/naaturel/ANN/Main.java
Normal file
57
src/main/java/com/naaturel/ANN/Main.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package com.naaturel.ANN;
|
||||
|
||||
import com.naaturel.ANN.domain.abstraction.Neuron;
|
||||
import com.naaturel.ANN.domain.model.dataset.DataSet;
|
||||
import com.naaturel.ANN.domain.model.dataset.DataSetEntry;
|
||||
import com.naaturel.ANN.domain.model.dataset.Label;
|
||||
import com.naaturel.ANN.domain.model.neuron.Bias;
|
||||
import com.naaturel.ANN.domain.model.neuron.Input;
|
||||
import com.naaturel.ANN.domain.model.neuron.Synapse;
|
||||
import com.naaturel.ANN.domain.model.neuron.Weight;
|
||||
import com.naaturel.ANN.implementation.activationFunction.Heaviside;
|
||||
import com.naaturel.ANN.implementation.activationFunction.Linear;
|
||||
import com.naaturel.ANN.implementation.neuron.SimplePerceptron;
|
||||
import com.naaturel.ANN.implementation.training.GradientDescentTraining;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args){
|
||||
|
||||
DataSet dataSet = new DataSet(Map.ofEntries(
|
||||
Map.entry(new DataSetEntry(List.of(1.0F, 6.0F)), new Label(1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(7.0F, 9.0F)), new Label(-1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(1.0F, 9.0F)), new Label(1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(7.0F, 10.0F)), new Label(-1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(2.0F, 5.0F)), new Label(-1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(2.0F, 7.0F)), new Label(1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(2.0F, 8.0F)), new Label(1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(6.0F, 8.0F)), new Label(-1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(6.0F, 9.0F)), new Label(-1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(3.0F, 5.0F)), new Label(-1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(3.0F, 6.0F)), new Label(-1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(3.0F, 8.0F)), new Label(1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(3.0F, 9.0F)), new Label(1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(5.0F, 7.0F)), new Label(-1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(5.0F, 8.0F)), new Label(-1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(5.0F, 10.0F)), new Label(1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(5.0F, 11.0F)), new Label(1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(4.0F, 6.0F)), new Label(-1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(4.0F, 7.0F)), new Label(-1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(4.0F, 9.0F)), new Label(1.0F)),
|
||||
Map.entry(new DataSetEntry(List.of(4.0F, 10.0F)), new Label(1.0F))
|
||||
));
|
||||
|
||||
List<Synapse> syns = new ArrayList<>();
|
||||
syns.add(new Synapse(new Input(0), new Weight()));
|
||||
syns.add(new Synapse(new Input(0), new Weight()));
|
||||
|
||||
Bias bias = new Bias(new Weight());
|
||||
|
||||
Neuron n = new SimplePerceptron(syns, bias, new Linear());
|
||||
GradientDescentTraining st = new GradientDescentTraining();
|
||||
st.train(n, 0.0003F, dataSet);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.naaturel.ANN.domain.abstraction;
|
||||
|
||||
public interface ActivationFunction {
|
||||
|
||||
float accept(Neuron n);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.naaturel.ANN.domain.abstraction;
|
||||
import com.naaturel.ANN.domain.model.neuron.Bias;
|
||||
import com.naaturel.ANN.domain.model.neuron.Input;
|
||||
import com.naaturel.ANN.domain.model.neuron.Synapse;
|
||||
import com.naaturel.ANN.domain.model.neuron.Weight;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Neuron {
|
||||
|
||||
protected List<Synapse> synapses;
|
||||
protected Bias bias;
|
||||
protected ActivationFunction activationFunction;
|
||||
|
||||
public Neuron(List<Synapse> synapses, Bias bias, ActivationFunction func){
|
||||
this.synapses = synapses;
|
||||
this.bias = bias;
|
||||
this.activationFunction = func;
|
||||
}
|
||||
|
||||
public abstract float predict();
|
||||
public abstract float calculateWeightedSum();
|
||||
|
||||
public int getSynCount(){
|
||||
return this.synapses.size();
|
||||
}
|
||||
|
||||
public void setInput(int index, Input input){
|
||||
Synapse syn = this.synapses.get(index);
|
||||
syn.setInput(input.getValue());
|
||||
}
|
||||
|
||||
public Bias getBias(){
|
||||
return this.bias;
|
||||
}
|
||||
|
||||
public void updateBias(Weight weight) {
|
||||
this.bias.setWeight(weight.getValue());
|
||||
}
|
||||
|
||||
public Synapse getSynapse(int index){
|
||||
return this.synapses.get(index);
|
||||
}
|
||||
|
||||
public List<Synapse> getSynapses() {
|
||||
return new ArrayList<>(this.synapses);
|
||||
}
|
||||
|
||||
public void setWeight(int index, Weight weight){
|
||||
Synapse syn = this.synapses.get(index);
|
||||
syn.setWeight(weight.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.naaturel.ANN.domain.abstraction;
|
||||
|
||||
public abstract class NeuronTrainer {
|
||||
|
||||
private Trainable trainable;
|
||||
|
||||
public NeuronTrainer(Trainable trainable){
|
||||
this.trainable = trainable;
|
||||
}
|
||||
|
||||
public abstract void train();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.naaturel.ANN.domain.abstraction;
|
||||
|
||||
public interface Trainable {
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.naaturel.ANN.domain.model.dataset;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class DataSet implements Iterable<DataSetEntry>{
|
||||
|
||||
private Map<DataSetEntry, Label> data;
|
||||
|
||||
public DataSet(){
|
||||
this(new HashMap<>());
|
||||
}
|
||||
|
||||
public DataSet(Map<DataSetEntry, Label> data){
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public List<DataSetEntry> getData(){
|
||||
return new ArrayList<>(this.data.keySet());
|
||||
}
|
||||
|
||||
public Label getLabel(DataSetEntry entry){
|
||||
return this.data.get(entry);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterator<DataSetEntry> iterator() {
|
||||
return this.data.keySet().iterator();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.naaturel.ANN.domain.model.dataset;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class DataSetEntry implements Iterable<Float> {
|
||||
|
||||
private List<Float> data;
|
||||
|
||||
public DataSetEntry(List<Float> data){
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public List<Float> getData() {
|
||||
return new ArrayList<>(data);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof DataSetEntry dataSetEntry)) return false;
|
||||
return Objects.equals(this.data, dataSetEntry.data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Float> iterator() {
|
||||
return this.data.iterator();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Arrays.toString(this.data.toArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.naaturel.ANN.domain.model.dataset;
|
||||
|
||||
public class Label {
|
||||
|
||||
private float value;
|
||||
|
||||
public Label(float value){
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
public float getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.naaturel.ANN.domain.model.neuron;
|
||||
|
||||
public class Bias extends Synapse {
|
||||
|
||||
public Bias(Weight weight) {
|
||||
super(new Input(1), weight);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.naaturel.ANN.domain.model.neuron;
|
||||
|
||||
public class Input {
|
||||
|
||||
private float value;
|
||||
|
||||
public Input(float value){
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public void setValue(float value){
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public float getValue(){
|
||||
return this.value;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.naaturel.ANN.domain.model.neuron;
|
||||
|
||||
public class Synapse {
|
||||
|
||||
private Input input;
|
||||
private Weight weight;
|
||||
|
||||
public Synapse(Input input, Weight weight){
|
||||
this.input = input;
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public float getInput(){
|
||||
return this.input.getValue();
|
||||
}
|
||||
|
||||
public void setInput(float value){
|
||||
this.input.setValue(value);
|
||||
}
|
||||
|
||||
public float getWeight() {
|
||||
return weight.getValue();
|
||||
}
|
||||
|
||||
public void setWeight(float value){
|
||||
this.weight.setValue(value);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.naaturel.ANN.domain.model.neuron;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Weight {
|
||||
|
||||
private float value;
|
||||
|
||||
public Weight(){
|
||||
this(new Random().nextFloat() * 2 - 1);
|
||||
}
|
||||
|
||||
public Weight(float value){
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public void setValue(float value){
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public float getValue(){
|
||||
return this.value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.naaturel.ANN.implementation.activationFunction;
|
||||
|
||||
import com.naaturel.ANN.domain.abstraction.ActivationFunction;
|
||||
import com.naaturel.ANN.domain.abstraction.Neuron;
|
||||
|
||||
public class Heaviside implements ActivationFunction {
|
||||
|
||||
public Heaviside(){
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public float accept(Neuron n) {
|
||||
float weightedSum = n.calculateWeightedSum();
|
||||
return weightedSum <= 0 ? 0:1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.naaturel.ANN.implementation.activationFunction;
|
||||
|
||||
import com.naaturel.ANN.domain.abstraction.ActivationFunction;
|
||||
import com.naaturel.ANN.domain.abstraction.Neuron;
|
||||
|
||||
public class Linear implements ActivationFunction {
|
||||
|
||||
@Override
|
||||
public float accept(Neuron n) {
|
||||
return n.calculateWeightedSum();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.naaturel.ANN.implementation.neuron;
|
||||
|
||||
import com.naaturel.ANN.domain.abstraction.ActivationFunction;
|
||||
import com.naaturel.ANN.domain.abstraction.Neuron;
|
||||
import com.naaturel.ANN.domain.abstraction.Trainable;
|
||||
import com.naaturel.ANN.domain.model.neuron.Bias;
|
||||
import com.naaturel.ANN.domain.model.neuron.Synapse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SimplePerceptron extends Neuron implements Trainable {
|
||||
|
||||
public SimplePerceptron(List<Synapse> synapses, Bias b, ActivationFunction func) {
|
||||
super(synapses, b, func);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float predict() {
|
||||
return activationFunction.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float calculateWeightedSum() {
|
||||
float res = 0;
|
||||
for(Synapse syn : super.synapses){
|
||||
res += syn.getWeight() * syn.getInput();
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.naaturel.ANN.implementation.training;
|
||||
|
||||
public class AdalineTraining {
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.naaturel.ANN.implementation.training;
|
||||
|
||||
import com.naaturel.ANN.domain.abstraction.Neuron;
|
||||
import com.naaturel.ANN.domain.model.dataset.DataSet;
|
||||
import com.naaturel.ANN.domain.model.dataset.DataSetEntry;
|
||||
import com.naaturel.ANN.domain.model.neuron.Bias;
|
||||
import com.naaturel.ANN.domain.model.neuron.Input;
|
||||
import com.naaturel.ANN.domain.model.neuron.Synapse;
|
||||
import com.naaturel.ANN.domain.model.neuron.Weight;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GradientDescentTraining {
|
||||
|
||||
public GradientDescentTraining(){
|
||||
|
||||
}
|
||||
|
||||
public void train(Neuron n, float learningRate, DataSet dataSet) {
|
||||
int epoch = 1;
|
||||
int maxEpoch = 10000;
|
||||
float errorThreshold = 0.125F;
|
||||
float currentError;
|
||||
|
||||
do {
|
||||
if(epoch > maxEpoch) break;
|
||||
|
||||
float biasCorrector = 0;
|
||||
currentError = 0;
|
||||
List<Float> correctorTerms = this.initCorrectorTerms(n.getSynCount());
|
||||
|
||||
for(DataSetEntry entry : dataSet) {
|
||||
this.updateInputs(n, entry);
|
||||
float prediction = n.predict();
|
||||
float expectation = dataSet.getLabel(entry).getValue();
|
||||
float delta = this.calculateDelta(expectation, prediction);
|
||||
float loss = this.calculateLoss(delta);
|
||||
|
||||
currentError += loss;
|
||||
Bias b = n.getBias();
|
||||
biasCorrector += this.calculateWeightCorrection(learningRate, b.getInput(), delta);
|
||||
|
||||
for(int i = 0; i < correctorTerms.size(); i++){
|
||||
Synapse syn = n.getSynapse(i);
|
||||
float c = correctorTerms.get(i);
|
||||
c += this.calculateWeightCorrection(learningRate, syn.getInput(), delta);
|
||||
correctorTerms.set(i, c);
|
||||
}
|
||||
System.out.printf("Epoch : %d ", epoch);
|
||||
System.out.printf("predicted : %.2f, ", prediction);
|
||||
System.out.printf("expected : %.2f, ", expectation);
|
||||
System.out.printf("delta : %.2f, ", delta);
|
||||
System.out.printf("loss : %.2f\n", loss);
|
||||
|
||||
}
|
||||
System.out.printf("[Total error : %.2f]\n", currentError);
|
||||
n.updateBias(new Weight(biasCorrector));
|
||||
|
||||
for(int i = 0; i < correctorTerms.size(); i++){
|
||||
Synapse syn = n.getSynapse(i);
|
||||
float c = correctorTerms.get(i);
|
||||
syn.setWeight(syn.getWeight() + c);
|
||||
}
|
||||
|
||||
epoch++;
|
||||
} while(currentError > errorThreshold);
|
||||
|
||||
}
|
||||
|
||||
private List<Float> initCorrectorTerms(int number){
|
||||
List<Float> res = new ArrayList<>();
|
||||
for(int i = 0; i < number; i++){
|
||||
res.add(0F);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private void updateInputs(Neuron n, DataSetEntry entry){
|
||||
int index = 0;
|
||||
for(float value : entry){
|
||||
n.setInput(index, new Input(value));
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
private float calculateDelta(float expected, float predicted){
|
||||
return expected - predicted;
|
||||
}
|
||||
|
||||
private float calculateLoss(float delta){
|
||||
return ((float) Math.pow(delta, 2))/2;
|
||||
}
|
||||
|
||||
private float calculateWeightCorrection(float lr, float value, float delta){
|
||||
return lr * value * delta;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.naaturel.ANN.implementation.training;
|
||||
|
||||
import com.naaturel.ANN.domain.abstraction.Neuron;
|
||||
import com.naaturel.ANN.domain.model.dataset.DataSet;
|
||||
import com.naaturel.ANN.domain.model.dataset.DataSetEntry;
|
||||
import com.naaturel.ANN.domain.model.neuron.Input;
|
||||
import com.naaturel.ANN.domain.model.neuron.Synapse;
|
||||
import com.naaturel.ANN.domain.model.neuron.Weight;
|
||||
|
||||
public class SimpleTraining {
|
||||
|
||||
public SimpleTraining() {
|
||||
|
||||
}
|
||||
|
||||
public void train(Neuron n, float learningRate, DataSet dataSet) {
|
||||
int epoch = 1;
|
||||
int errorCount;
|
||||
|
||||
do {
|
||||
errorCount = 0;
|
||||
System.out.printf("Epoch : %d\n", epoch);
|
||||
for(DataSetEntry entry : dataSet) {
|
||||
this.updateInputs(n, entry);
|
||||
float prediction = n.predict();
|
||||
float expectation = dataSet.getLabel(entry).getValue();
|
||||
float delta = this.calculateDelta(expectation, prediction);
|
||||
float loss = this.calculateLoss(delta);
|
||||
if(delta > 1e-6f) {
|
||||
this.updateWeights(n, learningRate, delta);
|
||||
errorCount += 1;
|
||||
}
|
||||
System.out.printf("predicted : %.2f, ", prediction);
|
||||
System.out.printf("expected : %.2f, ", expectation);
|
||||
System.out.printf("delta : %.2f\n", this.calculateDelta(expectation, prediction));
|
||||
}
|
||||
System.out.print("====================================\n");
|
||||
epoch++;
|
||||
} while (errorCount != 0);
|
||||
}
|
||||
|
||||
private void updateInputs(Neuron n, DataSetEntry entry){
|
||||
int index = 0;
|
||||
for(float value : entry){
|
||||
n.setInput(index, new Input(value));
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
private void updateWeights(Neuron n, float rate, float delta){
|
||||
|
||||
Weight biasCorrection = new Weight(n.getBias().getWeight() + (rate * delta * n.getBias().getInput()));
|
||||
n.updateBias(biasCorrection);
|
||||
|
||||
for(Synapse syn : n.getSynapses()){
|
||||
syn.setWeight(syn.getWeight() + (rate * delta * syn.getInput()));
|
||||
}
|
||||
}
|
||||
|
||||
private float calculateDelta(float expected, float predicted){
|
||||
return expected - predicted;
|
||||
}
|
||||
|
||||
private float calculateLoss(float delta){
|
||||
return Math.abs(delta);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user