※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.
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.
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