Commit dccfbaaf authored by Michal Kravcenko's avatar Michal Kravcenko

commit before branching to version2

parents b33698b6 a0fd976e
......@@ -8,13 +8,6 @@ Steps to reproduce
(How one can reproduce the issue - this is very important)
Example Project
(If possible, please create an example project here on GitLab.com that exhibits the problematic behaviour, and link to it here in the bug report)
(If you are using an older version of GitLab, this will also determine whether the bug has been fixed in a more recent version)
What is the current bug behavior?
(What actually happens)
......@@ -35,7 +28,5 @@ Possible fixes
(If you can, link to the line of code that might be responsible for the problem)
/label ~bug ~reproduced ~needs-investigation
/cc @project-manager
/assign @qa-tester
/label ~bug ~needs-investigation
Summary
(Summarize the bug encountered concisely)
Associated classes
(Classes, which need to be modified, if any)
/label ~bug ~reproduced ~needs-investigation
......@@ -32,7 +32,7 @@ DOXYFILE_ENCODING = UTF-8
# title of most generated pages and in a few other places.
# The default value is: My Project.
PROJECT_NAME = "4Neuro"
PROJECT_NAME = "lib4neuro"
# The PROJECT_NUMBER tag can be used to enter a project or revision number. This
# could be handy for archiving the generated documentation or if some version
......@@ -51,7 +51,7 @@ PROJECT_BRIEF = "Massivelly-parallel neural networks library"
# pixels and the maximum width should not exceed 200 pixels. Doxygen will copy
# the logo to the output directory.
PROJECT_LOGO = "img/4neuro_logo_small.png"
PROJECT_LOGO = "img/lib4neuro_logo.png"
# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) path
# into which the generated documentation will be written. If a relative path is
......
This diff is collapsed.
package cz.vsb.mro0010.neuralnetworks;
public class BinaryNeuron extends Neuron {
@Override
public void transfer() {
if (this.getPotential() > this.getThreshold()) {
this.setState(1);
} else {
this.setState(0);
}
}
}
This diff is collapsed.
package cz.vsb.mro0010.neuralnetworks;
public class Connection {
private Neuron inputNeuron;
private Neuron outputNeuron;
private float weight;
public Connection(Neuron inputNeuron, Neuron outputNeuron, float weight) {
this.setInputNeuron(inputNeuron);
this.setOutputNeuron(outputNeuron);
this.setWeight(weight);
}
protected Neuron getInputNeuron() {
return inputNeuron;
}
protected void setInputNeuron(Neuron inputNeuron) {
this.inputNeuron = inputNeuron;
}
protected Neuron getOutputNeuron() {
return outputNeuron;
}
protected void setOutputNeuron(Neuron outputNeuron) {
this.outputNeuron = outputNeuron;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public void adjustWeight(float value) {
this.weight += value;
}
public void passSignal() {
outputNeuron.adjustPotential(inputNeuron.getState()*this.getWeight());
}
@Override
public String toString() {
return "Weight: " + this.getWeight();
}
}
package cz.vsb.mro0010.neuralnetworks;
public class InputLayerPseudoNeuron extends Neuron {
public InputLayerPseudoNeuron() {
super();
}
@Override
public void transfer() {
this.setState(this.getPotential());
}
}
package cz.vsb.mro0010.neuralnetworks;
import java.util.ArrayList;
public abstract class Interconnections {
protected ArrayList<Connection> connections;
public ArrayList<Connection> getConnections() {
return connections;
}
public Interconnections() {
this.connections = new ArrayList<Connection>();
}
public void addConnection(Connection connection) {
this.connections.add(connection);
}
// public void passSignal() {
// for (Connection c : this.connections) {
//
// Neuron n = c.getOutputNeuron();
// n.initialize();
// for (Connection cn : this.connections) {
// if (cn.getOutputNeuron().equals(n)) {
// cn.passSignal();
// }
// }
// n.transfer();
// }
// }
public void passSignal() { // Faster version
ArrayList<Neuron> processedNeurons = new ArrayList<Neuron>();
for (Connection c : this.connections) {
Neuron n = c.getOutputNeuron();
if (!processedNeurons.contains(n)) {
processedNeurons.add(n);
n.initialize();
for (Connection cn : this.connections) {
if (cn.getOutputNeuron().equals(n)) {
cn.passSignal();
}
}
n.transfer();
}
}
}
public abstract void adjustWeights();
}
package cz.vsb.mro0010.neuralnetworks;
public class InterconnectionsBP extends InterconnectionsMultiLayer {
public InterconnectionsBP(float learningRate) {
super(learningRate);
}
public void setLearningRate(float learningRate) {
this.learningRate = learningRate;
}
@Override
public void adjustWeights() { // backPropagation - set new weights !after! all deltas are calculated
for (Connection connection : this.connections) {
float delta = ((SigmoidalNeuron)connection.getOutputNeuron()).getError();
float lambda = ((SigmoidalNeuron)connection.getOutputNeuron()).getSlope();
float y = connection.getOutputNeuron().getState();
float x = connection.getInputNeuron().getState();
float errorDerivative = delta*lambda*y*(1-y)*x;
connection.adjustWeight(-learningRate*errorDerivative);
}
}
}
package cz.vsb.mro0010.neuralnetworks;
public abstract class InterconnectionsMultiLayer extends Interconnections {
protected float learningRate; //eta
public InterconnectionsMultiLayer(float learningRate) {
this.learningRate = learningRate;
}
}
package cz.vsb.mro0010.neuralnetworks;
public class InvalidInputNumberException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = -6282750644609100469L;
public InvalidInputNumberException() {
super("Number of input values does not correspond with network input size");
}
}
package cz.vsb.mro0010.neuralnetworks;
public class InvalidLayerNumberException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1366940285989358521L;
public InvalidLayerNumberException() {
super("Number of layer does not correspond with network");
}
}
package cz.vsb.mro0010.neuralnetworks;
public class InvalidNeuronTypeException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 5354372081840990196L;
public InvalidNeuronTypeException() {
super("Wrong Neuron type");
}
}
package cz.vsb.mro0010.neuralnetworks;
import java.util.ArrayList;
public abstract class MultiLayeredNet extends NeuralNet {
protected ArrayList<ArrayList<Neuron>> neuronLayers;
protected ArrayList<InputLayerPseudoNeuron> inputNeuronLayer;
protected int nrOfInputs;
protected int nrOfLayers;
protected ArrayList<Integer> nrOfNeuronsPerLayer;
public MultiLayeredNet(int nrOfInputs, int nrOfLayers, ArrayList<Integer> nrOfNeuronsPerLayer) {
super();
this.nrOfInputs = nrOfInputs;
this.nrOfLayers = nrOfLayers;
this.nrOfNeuronsPerLayer = nrOfNeuronsPerLayer;
neuronLayers = new ArrayList<ArrayList<Neuron>>(nrOfLayers);
inputNeuronLayer = new ArrayList<InputLayerPseudoNeuron>(nrOfInputs);
for (int i = 0; i < nrOfLayers; i++) {
neuronLayers.add(new ArrayList<Neuron>(nrOfNeuronsPerLayer.get(i)));
}
for (int i = 0; i < nrOfInputs; i++) {
inputNeuronLayer.add(new InputLayerPseudoNeuron());
}
}
public MultiLayeredNet() {
this(0,0,null);
}
public int getNrOfInputs() {
return nrOfInputs;
}
public int getNrOfLayers() {
return nrOfLayers;
}
@Override
public void run(String input) {
String[] inputValues = input.split(" ");
if (inputValues.length != nrOfInputs)
throw new InvalidInputNumberException();
for (int i = 0; i < nrOfInputs; i++) {
InputLayerPseudoNeuron in = this.inputNeuronLayer.get(i);
in.initialize();
in.adjustPotential(Float.parseFloat(inputValues[i]));
in.transfer();
}
for (int i = 0; i < nrOfLayers; i++) {
Interconnections interconnectionsLayer = interconnectionsLayers.get(i);
interconnectionsLayer.passSignal();
}
}
}
package cz.vsb.mro0010.neuralnetworks;
import java.util.ArrayList;
public abstract class NeuralNet {
protected ArrayList<Interconnections> interconnectionsLayers;
public NeuralNet(ArrayList<Interconnections> interconnectionsLayers) {
this.interconnectionsLayers = interconnectionsLayers;
}
public NeuralNet() {
this(new ArrayList<Interconnections>());
}
public abstract String getNeuronType();
public abstract int learn(String trainingSet);
public abstract void run(String input);
}
package cz.vsb.mro0010.neuralnetworks;
public abstract class Neuron {
private float potential; // inner potential
private float state; // excitation state
private float threshold; // threshold of excitation
public Neuron() {
this(0, 0, 0);
}
public Neuron(float potential, float state, float threshold) {
this.setPotential(potential);
this.setState(state);
this.setThreshold(threshold);
}
public void initialize() {
this.setPotential(0);
this.setState(0);
}
public float getThreshold() {
return threshold;
}
public void setThreshold(float threshold) {
this.threshold = threshold;
}
public float getState() {
return state;
}
protected void setState(float state) {
this.state = state;
}
protected float getPotential() {
return this.potential;
}
private void setPotential(float potential) {
this.potential = potential;
}
public void adjustPotential(float value) {
this.potential += value;
}
@Override
public String toString() {
return "Pot.: " + this.potential + ", State: " + this.state + ", Thr.: " + this.threshold;
}
public abstract void transfer();
}
This diff is collapsed.
This diff is collapsed.
package cz.vsb.mro0010.neuralnetworks;
public class SigmoidalNeuron extends Neuron {
private float error; //delta
private float slope; //lambda
public void setSlope(float slope) {
this.slope = slope;
}
public SigmoidalNeuron(float slope) {
this.slope = slope;
this.error = 0;
}
@Override
public void transfer() {
float z = this.getPotential();
float y = (float) (1.0/(1.0 + Math.exp(-slope*z)));
this.setState(y);
}
public float getSlope() {
return slope;
}
public float getError() {
return error;
}
public void setError(float error) {
this.error = error;
}
public static void main(String args[]) {
SigmoidalNeuron neuron = new SigmoidalNeuron((float)0.5);
for (int i = -10; i <= 10; i++) {
neuron.initialize();
neuron.adjustPotential(i);
neuron.transfer();
System.out.println(neuron.getState());
}
}
}
package cz.vsb.mro0010.neuralnetworks;
import java.util.ArrayList;
import java.util.Arrays;
public class SinglePerceptronNeuralNet extends NeuralNet {
private Neuron neuron;
private int nrOfInputs;
private ArrayList<Connection> connections;
private ArrayList<InputLayerPseudoNeuron> input;
private String trainingOutput;
private float learnCoef;
public SinglePerceptronNeuralNet(Neuron neuron, int nrOfInputs, float learnCoef) {
super();
this.neuron = neuron;
this.nrOfInputs = nrOfInputs;
this.input = new ArrayList<InputLayerPseudoNeuron>();
this.connections = new ArrayList<Connection>();
for (int i = 0; i < this.nrOfInputs; i++) {
InputLayerPseudoNeuron inputNeuron = new InputLayerPseudoNeuron();
this.input.add(inputNeuron);
this.connections.add(new Connection(inputNeuron, neuron, (float)Math.random()));
}
this.setTrainingOutput(" ");
this.learnCoef = learnCoef;
}
@Override
public String getNeuronType() {
return neuron.getClass().getSimpleName();
}
@Override
public int learn(String trainingSet) {
ArrayList<String> trainingElements = new ArrayList<String>(Arrays.asList(trainingSet.split("\n")));
boolean learned = false;
int iterations = 0;
StringBuffer trainingProgress = new StringBuffer();
for (Connection c : connections) {
trainingProgress.append(String.valueOf(c.getWeight()));
trainingProgress.append(" ");
}
trainingProgress.append(String.valueOf(-neuron.getThreshold()));
trainingProgress.append("\n");
while (!learned) {
iterations++;
learned = true;
for (String element : trainingElements) {
String[] sa = element.split(" ");
String expectedOutput = sa[sa.length - 1];
StringBuffer sb = new StringBuffer();
for (int i = 0; i < sa.length - 1; i++) {
sb.append(sa[i]);
sb.append(" ");
}
this.run(sb.toString());
if (Float.parseFloat(expectedOutput) != Float.parseFloat(this.getOutput())) {
learned = false;
float eo = Float.parseFloat(expectedOutput);
float ro = Float.parseFloat(this.getOutput());
neuron.setThreshold(neuron.getThreshold() + learnCoef*-(eo-ro)*1); // w_0 = -threshold
for (Connection c : connections) {
c.adjustWeight(learnCoef*(eo-ro)*c.getInputNeuron().getState());
}
for (Connection c : connections) {
trainingProgress.append(String.valueOf(c.getWeight()));
trainingProgress.append(" ");
}
trainingProgress.append(String.valueOf(neuron.getThreshold()));
trainingProgress.append("\n");
}
}
}
//System.out.println("Learned! in " + (iterations-1) + " iterations");
this.setTrainingOutput(trainingProgress.toString());
return iterations;
}
@Override
public void run(String inputString) {
String[] input = inputString.split(" ");
for (int i = 0; i < input.length; i++) {
InputLayerPseudoNeuron in = this.input.get(i);
in.initialize();
in.adjustPotential(Float.parseFloat(input[i]));
in.transfer();
}
neuron.initialize();
for (Connection c : connections) {
c.passSignal();
}
neuron.transfer();
}
public String getOutput() {
String output = String.valueOf(neuron.getState());
return output;
}
public String getTrainingOutput() {
return trainingOutput;
}
private void setTrainingOutput(String trainingOutput) {
this.trainingOutput = trainingOutput;
}
/*public static void main(String[] args) {
SinglePerceptronNeuralNet net = new SinglePerceptronNeuralNet(new BinaryNeuron(), 2, (float)0.7);
net.neuron.setThreshold((float) Math.random());
// String learnSet = "1 0.5 0\n0.4 0.8 1\n0.1 0.1 0\n0.6 0.9 1\n0.8 0.7 0\n0.4 1.0 1";
// net.learn(learnSet);
// net.run("0.7 0.9");
// System.out.println(net.getOutput());
// net.run("0.9 0.7");
// System.out.println(net.getOutput());
// net.run("0.2 0.2");
// System.out.println(net.getOutput());
// net.run("0.1 1.0");
// System.out.println(net.getOutput());
// net.run("1.0 0.1");
// System.out.println(net.getOutput());
String learnSet = "0.7 0.3 0\n0.2 0.6 1\n0.3 0.4 1\n0.9 0.8 0\n0.1 0.2 1\n0.5 0.6 1";
net.learn(learnSet);
net.run("0.7 0.9");
System.out.println(net.getOutput());
net.run("0.9 0.7");
System.out.println(net.getOutput());
net.run("0.2 0.2");
System.out.println(net.getOutput());
net.run("0.1 1.0");
System.out.println(net.getOutput());
net.run("1.0 0.1");
System.out.println(net.getOutput());
net.run("0.6 0.5");
System.out.println(net.getOutput());
net.run("0.5 0.6");
System.out.println(net.getOutput());
}*/
}
......@@ -84,5 +84,8 @@ target_link_libraries(particle_swarm_test boost_unit_test 4neuro)
add_executable(particle_test tests/Particle_test.cpp)
target_link_libraries(particle_test boost_unit_test 4neuro)
add_executable(NeuralNetworkSum_test tests/NeuralNetworkSum_test.cpp)
target_link_libraries(NeuralNetworkSum_test boost_unit_test 4neuro)