Commit 6f58e846 authored by Chris Müller's avatar Chris Müller
Browse files

initial commit

parents
<project name="jlearning" default="dist">
<property name="src.dir" location="src"/>
<property name="build.dir" location="build/classes"/>
<target name="build" description="compile sources">
<mkdir dir="${build.dir}" />
<javac srcdir="${src.dir}" destdir="${build.dir}">
<classpath>
<pathelement location="${src.dir}"/>
<pathelement location="${build.dir}"/>
<pathelement location="libs/ejml.jar"/>
</classpath>
</javac>
</target>
<target name="dist" depends="build" description="package into jar">
<jar jarfile="jlearning.jar" basedir="${build.dir}" />
</target>
<target name="clean">
<delete dir="${build.dir}" />
<delete file="jlearning.jar" />
</target>
</project>
File added
package de.ruuns;
import org.ejml.data.DenseMatrix64F;
import org.ejml.ops.CommonOps;
import java.util.ArrayList;
import java.util.Random;
class Layer {
static Random RNG = new Random();
public Layer(int I, int O, double var) {
W = new DenseMatrix64F(I + 1, O);
a = new DenseMatrix64F(O + 1);
for(int i = 0; i < W.getNumElements(); ++i)
W.set(i, RNG.nextGaussian() * var);
};
public DenseMatrix64F W;
public DenseMatrix64F a;
}
public class NeuralNetwork {
public ArrayList<Layer> layers;
public NeuralNetwork(double init_variance, int ... dims) {
assert(dims.length > 0);
int I = dims[0];
for(int i = 1; i < dims.length; ++i)
layers.add(new Layer(I, dims[i], init_variance));
}
public DenseMatrix64F propagateForward(DenseMatrix64F X_input) {
DenseMatrix64F z = X_input.copy();
CommonOps.transpose(z);
for(Layer L: layers){
// Bias value to input vector [a1 a2 a3 ... 1.0]
z.reshape(1, z.getNumElements() + 1, true);
z.set(z.getNumElements() - 1, 1.0);
// perform forward propagation
CommonOps.multTransA(z, L.W, L.a);
// remove bias value again
z.reshape(1, z.getNumElements() - 1, true);
// apply activation function g(a)
z = sigmoid(L.a);
}
return z;
}
private DenseMatrix64F sigmoid(DenseMatrix64F X) {
for(int i = 0; i < X.getNumElements(); ++i) {
X.set(i, 1.0 / (1.0 + Math.exp(- X.get(i))));
}
return X;
}
}
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