Neural Networks from Scratch in JAVA
Neural
Networks from Scratch in JAVA
Completely
using an Object Orientated Approach
Vedio#1
: Introduction and Neural Layer Class
- Not need to include complete libraries like NumPy, TensorFlow or Pytrouch
- Writing a lightweight and server independent application.
- More Experimentation.
Java V/s Python
- Code Reusability in Java Best as OOPs
- Java handles concurrency better than Python, Python is more sequential
- Speed – Using JVM for Just in Time compilation
- Platform independent - Write once run anywhere with JVM (Java Virtual machine)
Neural Layer Class
Neural Layer – Collection of a set of neurons.
Neurons – Multi-Input , Single Output ( i/p * Weights) => Activation Function
Activation Function – Bounding the values.
Code:-
import java.util.function.Function; public class NeuralLayer { public enum ActivatoinFunctions{ Tanh, Sigmod } public enum InitailWeights{ Random,
Memory,
Zeros
} int noOfInputstoNeuron;
int noOfNeurons; double [][]weights; InitailWeights initWeight;
public final Function<Double,Double> ActivationFun , ActivationFunDervitaive; public NeuralLayer(int noOfIpNeuron , int noOfNeurons) { this(ActivatoinFunctions.Sigmod,InitailWeights.Random,noOfIpNeuron,noOfNeurons);
} public NeuralLayer(ActivatoinFunctions activate, InitailWeights iw ,int noOfIp, int noOfNeurons) { this.noOfInputstoNeuron = noOfIp;
this.noOfNeurons=noOfNeurons; //TODO : Handle differnt Activation Functions
this.ActivationFun = NNMath::Sigmod;
this.ActivationFunDervitaive = NNMath::SigmodDerviative; weights = new double[this.noOfInputstoNeuron][this.noOfNeurons]; for (int i =0; i < this.noOfInputstoNeuron ; ++i) { for (int j= 0; j < this.noOfNeurons; ++j) { weights[i][j] = 2*Math.random() -1 ; //to scale -1 to 1 //TODO handle switch based on iw weights[i][j] =0 } } } public void printTester(){ for (int i =0; i < this.noOfInputstoNeuron ; ++i) { for (int j= 0; j < this.noOfNeurons; ++j) { System.out.print(this.weights[i][j] + "\t" + ",");
} System.out.print("\n");
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class NNMath { public static double Sigmod(double nnValue){ return 1/(1+Math.exp(-nnValue));} public static double SigmodDerviative(double nnValue){ return nnValue/(1-nnValue);} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////public class NNTester { public static void main(String[] args) { System.out.println("Testing NN Layer");NeuralLayer n1 = new NeuralLayer(3,4);n1.printTester();} }
//////////////////////////////////////////////////////////////////////
#Vedio2: Math Operations Neural Network Math’s Operations Class 1) Activation functions – Sigmoid Function, Tanh Functions 2) Matrix Multiplication 3) Scalar Multiplication to matrix 4) Matrix Addition 5) Matrix Subtraction 6) Matrix Transpose 7) Matrix Normalization
import java.sql.PreparedStatement;
public class NNMath {
public static double Sigmoid(double nnValue){
return 1/(1+Math.exp(-nnValue));
}
public static double SigmoidDerivative(double nnValue){
return nnValue/(1-nnValue);
}
public static double Tanh(double nnvalue){
return Math.tanh(nnvalue);
}
public static double TanhDerivative(double nnValue){
return 1 - Math.tanh(nnValue)*Math.tanh(nnValue);
}
public static double[][] MatrixMultiply(double[][] a, double [][]b){
//mXn nXp n=n m!=0 n!=0 , mXp
if(a.length ==0 || b.length ==0 || a[0].length != b.length){
throw new IllegalArgumentException("Matrix Multiplication Not posssible");
}
int m=a.length;
int n=a[0].length;
int p=b[0].length;
double[][] result = new double[m][p];
for(int mItr=0; mItr<m;++mItr){
for(int pItr=0; pItr<p;++pItr){
double sum =0.0;
for(int nItr=0; nItr < n; ++nItr){
sum+= a[mItr][nItr]*b[nItr][pItr];
}
result[mItr][pItr]=sum;
}
}
return result;
}
public static double[][] SclarMultiply(double [][]a, double [][]b){
//mXn mXn
if(a.length ==0 || b.length ==0 || a[0].length != b[0].length || a.length != b.length){
throw new IllegalArgumentException("Matrix Multiplication Not posssible");
}
double[][] result = new double[a.length][a[0].length];
for(int mItr=0; mItr < a.length;++mItr){
for(int nItr=0; nItr < a[0].length; ++nItr){
result[mItr][nItr]=a[mItr][nItr]*b[mItr][nItr];
}
}
return result;
}
public static double[][] MatrixAddtion(double [][]a, double [][]b){
//mXn mXn
if(a.length ==0 || b.length ==0 || a[0].length != b[0].length || a.length != b.length){
throw new IllegalArgumentException("Matrix Add Not posssible");
}
double[][] result = new double[a.length][a[0].length];
for(int mItr=0; mItr < a.length;++mItr){
for(int nItr=0; nItr < a[0].length; ++nItr){
result[mItr][nItr]=a[mItr][nItr]+b[mItr][nItr];
}
}
return result;
}
public static double[][] MatrixSubtraction(double [][]a, double [][]b){
//mXn mXn
if(a.length ==0 || b.length ==0 || a[0].length != b[0].length || a.length != b.length){
throw new IllegalArgumentException("Matrix Sub Not posssible");
}
double[][] result = new double[a.length][a[0].length];
for(int mItr=0; mItr < a.length;++mItr){
for(int nItr=0; nItr < a[0].length; ++nItr){
result[mItr][nItr]=a[mItr][nItr]-b[mItr][nItr];
}
}
return result;
}
public static double[][] MatrixTrnaspose(double [][]a){
//mXn mXn
if(a.length ==0 ){
throw new IllegalArgumentException("Matrix Transpose Not posssible");
}
double[][] result = new double[a.length][a[0].length];
for(int mItr=0; mItr < a.length;++mItr){
for(int nItr=0; nItr < a[0].length; ++nItr){
result[nItr][mItr]=a[mItr][nItr];
}
}
return result;
}
public static double[][] MatrixNormalize(double [][]a){
//mXn
if(a.length ==0 ){
throw new IllegalArgumentException("Matrix Normliaze Not posssible");
}
double sum =0.0;
for(int mItr=0; mItr < a.length;++mItr){
for(int nItr=0; nItr < a[0].length; ++nItr){
sum+=a[mItr][nItr];
}
}
double[][] result = new double[a.length][a[0].length];
for(int mItr=0; mItr < a.length;++mItr){
for(int nItr=0; nItr < a[0].length; ++nItr){
result[mItr][nItr]=a[mItr][nItr]/sum;
}
}
return result;
}
}
Labels: AI