Saturday, 25 April 2020

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.
Error Correction – Back Propagation => Covered In next video









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;
}
}
Read more »

Labels:

Saturday, 18 April 2020

Unit testing in JAVA using JUnit5 from Basics


Understanding Unit Testing in JAVA


* JUnit 5
* What and Why and Where  Unit Testing ???

Testing Code in a faster way.
Testing Code with wide range of combinations of input parameters
Structural and behavioral flow testing of your code.
Art of writing Test cases.

#Video 1

1) Write a sample Java Class.
2) Generate its Test class in a faster way using IntelliJ editor.
3) Understanding imports and Annotation in JUnit 5.
4) Run the test cases.
5) Understand its usefulness of test cases.



#Video 2

1) Adding a few more test cases to Cordinate2D class Example from the previous video.
2) Auto Generate getters, setters, overload comparator function in a faster way using IntelliJ editor.
3) Few more imports and Annotation in JUnit 5.
4) What is Test coverage and How to Run coverage?
5) Understand test coverage meaning and interpret it.
6) Meaning Class Coverage, Method Coverage, and Line Coverage with Example.



#Video 3

1) Quick Recap of Cordinate2D Class.
2) Coverage report -Class Coverage, Method Coverage, and Line Coverage.
3) Understanding What is Test Driven Development(TDD).
4) How to make use of the unit testing if working in groups.
5) Effective Unit Testing, How a developer can think as tester ??
6) Improve your testing over and over again covering negative test cases.





Wednesday, 15 April 2020

Auto Generate Source Code From Class Diagram using Enterprise Architect 15




*Class Diagram -> Source Code
(Any language)

*Faster Development

*Easy Modification

*Good traceability


Tuesday, 14 April 2020

Auto Generate Class Diagram From Source Code Using Enterprise architect 15



Generate Class Diagram From Source Code Simple Example Explained

How to import source code in Enterprise Architect 15?

Steps –

1) Select the model – as a class diagram.
2) Delete the sample example in the default model created
3) Import Source files in the model.
4) And Select the necessary options 


EA manual link -https://sparxsystems.com/enterprise_architect_user_guide/15.1/index/index.html

Monday, 13 April 2020

PlantUML Sequence Diagrams in IntelliJ Editor



Sequence Diagram Drawing Tips and Tricks Using Plant UML in IntelliJ Activity Diagrams Tips and tricks Draw sequence diagram quickly Learn Sequence in 5 mins Learn Activity Diagram in 5 mins Intellij Installation for windows - https://www.jetbrains.com/idea/download/#section=windows Intellij Installation for Linux - https://www.jetbrains.com/idea/download/#section=linux Install this Plugin Plant UML Plugin - https://plugins.jetbrains.com/plugin/7017-plantuml-integration


Sample Example Shown-



@startuml
title MyFirstsequnceDiagramPlantUML\n

participant "ClassA" as A
participant "ClassB" as B
participant "ClassC" as C

A->B : HelloB()
B-->A : HelloA()
A-->C : ExtraCalls()
B-> C : HelloC()

alt CSpeaksWithBonly
C-->B : HelloB_C()
end
note over A
ErrorCondtionReached
end note
@enduml