Deep Learning Java from scratch Chapter 2 Perceptron

table of contents

2.3 Implementation of Perceptron

2.3.1 Easy implementation

Perceptrons can be implemented as functions (static methods).

public static int AND(int x1, int x2) {
    double w1 = 0.5, w2 = 0.5, theta = 0.7;
    double tmp = x1 * w1 + x2 * w2;
    if (tmp <= theta)
        return 0;
    else
        return 1;
}
assertEquals(0, AND(0, 0));
assertEquals(0, AND(1, 0));
assertEquals(0, AND(0, 1));
assertEquals(1, AND(1, 1));

2.3.2 Introducing weights and biases

Using ND4J, it becomes as follows.

INDArray x = Nd4j.create(new double[] {0, 1});
INDArray w = Nd4j.create(new double[] {0.5, 0.5});
double b = -0.7;
assertEquals("[0.00,0.50]", Util.string(w.mul(x)));
assertEquals(0.5, w.mul(x).sumNumber().doubleValue(), 0.000005);
assertEquals(-0.19999999999999996, w.mul(x).sumNumber().doubleValue() + b, 0.000005);

2.3.3 Implementation by weight and bias

Using the "weight and bias method", AND, NDND, and OR gates can be implemented as follows.

public static int AND2(int x1, int x2) {
    INDArray x = Nd4j.create(new double[] {x1, x2});
    INDArray w = Nd4j.create(new double[] {0.5, 0.5});
    double b = -0.7;
    double tmp = w.mul(x).sumNumber().doubleValue() + b;
    return tmp <= 0 ? 0 : 1;
}

public static int NAND(int x1, int x2) {
    INDArray x = Nd4j.create(new double[] {x1, x2});
    INDArray w = Nd4j.create(new double[] {-0.5, -0.5});
    double b = 0.7;
    double tmp = w.mul(x).sumNumber().doubleValue() + b;
    return tmp <= 0 ? 0 : 1;
}

public static int OR(int x1, int x2) {
    INDArray x = Nd4j.create(new double [] {x1, x2});
    INDArray w = Nd4j.create(new double[] {0.5, 0.5});
    double b = -0.2;
    double tmp = w.mul(x).sumNumber().doubleValue() + b;
    return tmp <= 0 ? 0 : 1;
}
assertEquals(0, AND2(0, 0));
assertEquals(0, AND2(1, 0));
assertEquals(0, AND2(0, 1));
assertEquals(1, AND2(1, 1));
assertEquals(1, NAND(0, 0));
assertEquals(1, NAND(1, 0));
assertEquals(1, NAND(0, 1));
assertEquals(0, NAND(1, 1));
assertEquals(0, OR(0, 0));
assertEquals(1, OR(1, 0));
assertEquals(1, OR(0, 1));
assertEquals(1, OR(1, 1));

2.5 Multilayer Perceptron

2.5.2 Implementation of XOR gate

XOR gates can be mounted by using multiple layers of perceptrons.

public static int XOR(int x1, int x2) {
    int s1 = NAND(x1, x2);
    int s2 = OR(x1, x2);
    int y = AND2(s1, s2);
    return y;
}
assertEquals(0, XOR(0, 0));
assertEquals(1, XOR(1, 0));
assertEquals(1, XOR(0, 1));
assertEquals(0, XOR(1, 1));

Recommended Posts

Deep Learning Java from scratch Chapter 2 Perceptron
Deep Learning Java from scratch Chapter 1 Introduction
Deep Learning from scratch Java Chapter 4 Neural network learning
Deep Learning Java from scratch Chapter 3 Neural networks
Study Deep Learning from scratch in Java.
Deep Learning Java from scratch 6.1 Parameter update
Deep Learning Java from scratch 6.3 Batch Normalization
Deep Learning Java from scratch Chapter 5 Error back propagation method
[Deep Learning from scratch] in Java 3. Neural network
Deep Learning Java from scratch 6.2 Initial values of weights
Fastest PC setup for deep learning from scratch
[Deep Learning from scratch] 2. There is no such thing as NumPy in Java.
Java life starting from scratch
[Deep Learning from scratch] in Java 1. For the time being, differentiation and partial differentiation
Java scratch scratch
First steps for deep learning in Java
Effective Java Chapter 2
For JAVA learning (2018-03-16-01)
Java learning day 5
I tried to implement deep learning in Java
Effective Java Chapter 6 34-35
Effective Java Chapter 4 15-22
Effective Java Chapter 3
java learning day 2
java learning day 1
Build VS Code + WSL + Java + Gradle environment from scratch
[Note] Create a java environment from scratch with docker
Quick learning Java "Introduction?" Part 3 Talking away from programming
Call Java from JRuby
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Java learning 2 (learning calculation method)
java learning (conditional expression)
Java learning memo (method)
Eval Java source from Java
Java Learning (1)-Hello World
Rails Tutorial Chapter 3 Learning
Access API.AI from Java
Java learning memo (basic)
From Java to Ruby !!
Rails Tutorial Chapter 4 Learning
Java learning memo (interface)
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Java learning memo (inheritance)
Java Performance Chapter 3 Java Performance Toolbox
Object-oriented child !? I tried Deep Learning in Java (trial edition)
Learning from the basics Artificial intelligence textbook Chapter 3 End-of-chapter problems
Learning from the basics Artificial intelligence textbook Chapter 4 Chapter end problems
Learning from the basics Artificial intelligence textbook Chapter 2 Chapter end problems
Let's touch on Deep Java Library (DJL), a library that can handle Deep Learning in Java released from AWS.