Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Warith HARCHAOUI committed Apr 19, 2016
0 parents commit 442d62a
Show file tree
Hide file tree
Showing 20 changed files with 1,097 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .classpath
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
target
target/*
23 changes: 23 additions & 0 deletions .project
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>mlp</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
5 changes: 5 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
20 changes: 20 additions & 0 deletions pom.xml
@@ -0,0 +1,20 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.oscaro</groupId>
<artifactId>multiLayerPerceptron</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Multi Layer Perceptron</name>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
26 changes: 26 additions & 0 deletions src/main/java/com/oscaro/neuralnetwork/FunctionAbstract.java
@@ -0,0 +1,26 @@
package com.oscaro.neuralnetwork;

public abstract class FunctionAbstract {

// Names are useful for debugging to know exactly where it gives...
protected String name;

public String getName() {
return name;
}

public FunctionAbstract(String name) throws IllegalArgumentException {
if (name == null || name.trim().length() == 0) {
throw new IllegalArgumentException(
"Name of function should not be null not empty not spaces only");
}
this.name = name;
}

// Computation of function
public abstract float[] run(float[] x) throws IllegalArgumentException;

// Computation of its derivative
public abstract float[] runGradient(float[] intermediateLinearCombination,
float[] error) throws IllegalArgumentException;
}
34 changes: 34 additions & 0 deletions src/main/java/com/oscaro/neuralnetwork/FunctionLinear.java
@@ -0,0 +1,34 @@
package com.oscaro.neuralnetwork;

public class FunctionLinear extends FunctionAbstract {

public FunctionLinear(String s) throws IllegalArgumentException {
super(s);
}

@Override
public float[] run(float[] x) throws IllegalArgumentException {
if (x == null || x.length == 0) {
throw new IllegalArgumentException("Input in layer " + getName()
+ " should not be null nor empty");
}
float[] res = new float[x.length];
for (int i = 0; i < res.length; i++) {
res[i] = x[i]; // Identity
}
return res;
}

@Override
public float[] runGradient(float[] intermediateLinearCombination,
float[] error) {

float[] res = new float[intermediateLinearCombination.length];

for (int i = 0; i < res.length; i++) {
res[i] = error[i]; // * 1.0f the derivative of the identity
}
return res;
}

}
38 changes: 38 additions & 0 deletions src/main/java/com/oscaro/neuralnetwork/FunctionRelu.java
@@ -0,0 +1,38 @@
package com.oscaro.neuralnetwork;

public class FunctionRelu extends FunctionAbstract {

public FunctionRelu(String s) throws IllegalArgumentException {
super(s);
}

@Override
public float[] run(float[] x) {
float[] res = new float[x.length];
for (int i = 0; i < res.length; i++) {
if (x[i] <= 0) {
res[i] = 0.0f;
} else {
res[i] = x[i];
}
}
return res;
}

@Override
public float[] runGradient(float[] intermediateLinearCombination,
float[] error) {
float[] res = new float[intermediateLinearCombination.length];
float tmp;
for (int i = 0; i < intermediateLinearCombination.length; i++) {
if (intermediateLinearCombination[i] <= 0) {
tmp = 0.0f;
} else {
tmp = 1.0f;
}
res[i] = tmp * error[i];
}
return res;
}

}
31 changes: 31 additions & 0 deletions src/main/java/com/oscaro/neuralnetwork/FunctionSigmoid.java
@@ -0,0 +1,31 @@
package com.oscaro.neuralnetwork;

public class FunctionSigmoid extends FunctionAbstract {

public FunctionSigmoid(String string) throws IllegalArgumentException {
super(string);
}

@Override
public float[] run(float[] x) {
float[] res = new float[x.length];
for (int i = 0; i < res.length; i++) {
res[i] = (float) (1.0f / (1.0f + Math.exp(-x[i])));
}
return res;
}

@Override
public float[] runGradient(float[] intermediateLinearCombination,
float[] error) {

float[] tmp = run(intermediateLinearCombination);
float[] res = new float[tmp.length];

for (int i = 0; i < tmp.length; i++) {
res[i] = tmp[i] * (1 - tmp[i]) * error[i];
}
return res;
}

}
59 changes: 59 additions & 0 deletions src/main/java/com/oscaro/neuralnetwork/FunctionSoftmax.java
@@ -0,0 +1,59 @@
package com.oscaro.neuralnetwork;

import java.util.Arrays;

public class FunctionSoftmax extends FunctionAbstract {

public FunctionSoftmax(String string) throws IllegalArgumentException {
super(string);
}

@Override
public float[] run(float[] x) {
float[] res = new float[x.length];

// ValMax is here for numerical stability of the Math.exp function
float valMax = Float.NEGATIVE_INFINITY;
for (int i = 0; i < x.length; i++) {
if (x[i] > valMax) {
valMax = x[i];
}
}

float z = 0.0f;
// Exponentiation
for (int i = 0; i < x.length; i++) {
// Dividing both numerator and denominator by Math.exp(-valMax) does
// not change the result but avoid numerical instability
res[i] = (float) (Math.exp(x[i] - valMax));
z += res[i];
}
// Normalization where w is the denominator
for (int i = 0; i < x.length; i++) {
res[i] = res[i] / z;
}
return res;
}

@Override
public float[] runGradient(float[] intermediateLinearCombination,
float[] error) {

float[] tmp = run(intermediateLinearCombination);

float[] res = new float[tmp.length];
Arrays.fill(res, 0.0f);

float delta_ij;
for (int i = 0; i < tmp.length; i++) {
for (int j = 0; j < tmp.length; j++) {
delta_ij = 0.0f;
if (i == j) {
delta_ij = 1.0f;
}
res[i] += (tmp[i]) * (delta_ij - (tmp[j])) * error[j];
}
}
return res;
}
}
@@ -0,0 +1,11 @@
package com.oscaro.neuralnetwork;

public class IllegalArgumentException extends Exception {

public IllegalArgumentException(String string) {
super("com.oscaro.neuralnetwork Exception: " + string);
}

private static final long serialVersionUID = -8029877992021795500L;

}

0 comments on commit 442d62a

Please sign in to comment.