Study Deep Learning from scratch in Java.

※Caution This article is low level and has a lot of self-satisfaction that I am writing for myself, just starting to read this book The content may be incorrect or the code may be strange. Also, I don't explain terms so much, so I think it's meaningless to people who don't know machine learning. I will update this article gradually.

(Single layer) Perceptron implementation

Perceptron: A type of algorithm. The algorithm that was the basis of the neural network. A step function is used as the activation function. With a single layer, only a linear region can be expressed.

** Express AND, OR, NAND by passing the bias and the weight of each input signal to the constructor. ** **

public class PerceptronSample {

    private double b, w1, w2;

    PerceptronSample(double b, double w1, double w2){
        this.b = b;
        this.w1 = w1;
        this.w2 = w2;
    }

    int perceptron(int x1, int x2){
       double tmp = (x1*w1 + x2*w2) + b; //Sum
        return tmp > 0 ? 1: 0;
    }




    public static void main(String[] args) {

        PerceptronSample perceptronSample = new PerceptronSample(-0.5, 0.3, 0.3); 
        //bias,Input 1,Input 2
        //This time AND circuit


        System.out.println("" +
                perceptronSample.perceptron(0, 0) +
                perceptronSample.perceptron(1, 0) +
                perceptronSample.perceptron(0, 1) +
                perceptronSample.perceptron(1, 1)
        );

    }

}

The result is 0001.

Express XOR with multi-layer perceptron.

A deeper layer of perceptron. It can solve problems that cannot be solved by a single-layer perceptron.

XOR is expressed by NAND, OR and AND.

public class MultilayerPerceptronSample {

    private double b, w1, w2;

    MultilayerPerceptronSample(double b, double w1, double w2){
        this.b = b;
        this.w1 = w1;
        this.w2 = w2;
    }

    int perceptron(int x1, int x2, MultilayerPerceptronSample m1, MultilayerPerceptronSample m2){

        if (m1 != null && m2 != null){
            int x[] = {x1, x2};
            x1 = m1.perceptron(x[0], x[1], null, null);
            x2 = m2.perceptron(x[0], x[1], null, null);
            double tmp = (x1*w1 + x2*w2) + b;
            return tmp > 0.0 ? 1 : 0;
        }else {
        double tmp = (x1*w1 + x2*w2) + b; //Sum

        return tmp > 0.0 ? 1 : 0;
        }
    }






    public static void main(String[] args) {

        MultilayerPerceptronSample AND = new MultilayerPerceptronSample(-0.5, 0.3, 0.3);
        MultilayerPerceptronSample NAND = new MultilayerPerceptronSample(0.5, -0.3, -0.3);
        MultilayerPerceptronSample OR = new MultilayerPerceptronSample(-0.3, 0.5, 0.5);




        System.out.println(" " +
                AND.perceptron(0, 0, NAND, OR)+
                AND.perceptron(1, 0, NAND, OR)+
                AND.perceptron(0, 1, NAND, OR)+
                AND.perceptron(1, 1, NAND, OR)


        );


    }

}

Recommended Posts

Study Deep Learning from scratch in Java.
[Deep Learning from scratch] in Java 3. Neural network
Deep Learning Java from scratch 6.4 Regularization
Deep Learning Java from scratch Chapter 1 Introduction
Deep Learning Java from scratch 6.1 Parameter update
Deep Learning Java from scratch Chapter 2 Perceptron
Deep Learning Java from scratch 6.3 Batch Normalization
Deep Learning from scratch Java Chapter 4 Neural network learning
Deep Learning Java from scratch Chapter 3 Neural networks
Deep Learning Java from scratch 6.2 Initial values of weights
[Deep Learning from scratch] in Java 1. For the time being, differentiation and partial differentiation
Deep Learning Java from scratch Chapter 5 Error back propagation method
First steps for deep learning in Java
I tried to implement deep learning in Java
Fastest PC setup for deep learning from scratch
Java life starting from scratch
Deep copy collection in Java
Object-oriented child !? I tried Deep Learning in Java (trial edition)
Java learning (0)
Java scratch scratch
Let's touch on Deep Java Library (DJL), a library that can handle Deep Learning in Java released from AWS.
Call Java method from JavaScript executed in Java
OCR in Java (character recognition from images)
Reverse Key from Value in Java Map
Using JavaScript from Java in Rhino 2021 version
Get history from Zabbix server in Java
GetInstance () from a @Singleton class in Groovy from Java
Let's study Java
9 strongest sites for learning Java by self study
For JAVA learning (2018-03-16-01)
Deep dive into how HashMap works in Java
Java learning day 5
Partization in Java
Java method call from RPG (method call in own class)
The story of learning Java in the first programming
Changes in Java 11
Rock-paper-scissors in Java
How to get Class from Element in Java
Text extraction in Java from PDF with pdfbox-2.0.8
Capture and save from selenium installation in Java
Get unixtime (seconds) from ZonedDateTime in Scala / Java
[Java] Study notes
Java 8 study (repeatable)
Java study memorandum
Pi in Java
Study Java Silver 1
Generate OffsetDateTime from Clock and LocalDateTime in Java
FizzBuzz in Java
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
java learning day 2
java learning day 1
Try calling synchronized methods from multiple threads in Java
Build VS Code + WSL + Java + Gradle environment from scratch
Delete All from Java SDK in Azure Cosmos DB
[Personal memo] Make a simple deep copy in Java
[Note] Create a java environment from scratch with docker
Reverse Enum constants from strings and values in Java
Call a program written in Swift from Processing (Java)
Quick learning Java "Introduction?" Part 3 Talking away from programming
[java] sort in list
Read JSON in Java