Brief description
With the rapid development of the Internet, A (AI) B (BigData) C (Cloud) has become the core development direction at present. If the three are deeply combined, AI is the most core part of it. So if everyone must learn programming in the future society, then for programmers, artificial intelligence is the technology they must master (technology development is really fast).
This article introduces and uses JAVA to implement a simplest perceptron network, without tangling with formula derivation, aiming to provide you with ideas for learning neural networks and have a rough understanding of neural networks.
Perceptron network model analysis
First look at a picture
If you are a little interested in neural networks, you must be familiar with this picture. This picture is a structural diagram of neurons
X1~Xm represents input, W1~Wm represents synaptic weight, Σ represents summing node, Activation function represents activation function, and then output a result. The specific process is
When the neuron receives the input, each input will be multiplied by the weight on its relative path. When the summing node is reached, the result of the summing node is set to z:
z = X1 * W1 + X2 * W2 + X3 * W3 + ...... + Xm * Wm
Then pass z into the activation function (here we call the activation function f) for binary pattern recognition:
if f(x) > e,y = 1else y = -1e is the threshold y is the classification result
It can be seen here that if the value of f(x) is greater than the threshold, the classification y = 1 is obtained, and vice versa y = -1
Note: Compared to the response indicated by the stimulation of biological neurons, if the stimulation is within an acceptable range, the neuron will inhibit the stimulation (y = -1), and if it exceeds the range, it will be excited (y = 1), and the watershed of this range is the threshold (e)
study
We found that if the weight and threshold are fixed, then this neural network has no meaning. Therefore, we introduced the concept of learning and let the neural network modify the weight and threshold through learning, so that the accuracy of pattern recognition can be dynamically corrected. This is the essence of machine learning.
So how to learn? Before we use it, we need to provide a set of sample data to this network (which is taken here to learn with teacher mode), the sample data includes input data x and correct identification result y'.
When we input training data x and get pattern recognition y, if y != y' , the weight and threshold of this network will be adjusted. Please refer to the formula for adjustment. μ represents the learning rate (correction rate), and update represents the need to be corrected:
update = μ * (yi - y')update = (f(x) - y')mΣ Wi += update * Xii=1e += update
When the perceptron classification result is equal to the correct classification, update = 0, and the network is not adjusted; if it is not equal to the correct classification, all weights (w) and thresholds (e) will be adjusted
The above is the simplest learning process for perceptrons I introduced:
Enter data ->Summarize to get z->Wait through activation function to wait for classification results ->Classification results do not match the correct results, adjust the network
Let's implement this simple neural network
Java code implementation
What I have implemented here is to learn to recognize the positive and negative of integers through neural networks. First, define a class that firstly defines a perceptron.
/** * Created by CimZzz on 12/2/17. * */public class Perceptron { /** * Learning rate*/ private final float learnRate; /** * Number of learning times*/ private final int studyCount; /** * Threshold*/ private float e; /** * Weight* Because only one input is required to determine the positive and negative integer, there is only one weight here, and multiple inputs can be set to an array*/ private float w; /** * Correct rate for each learning*/ private float[] correctRate; // /** * Constructor initializes the learning rate, the number of learning times, the weight, and threshold are initialized to 0 * @param learnRate Learning rate (value range 0 < learnRate < 1) * @param studyCount Number of learning times*/ public Perceptron(float learnRate, int studyCount) { this.learnRate = learnRate; this.studyCount = studyCount; this.e = 0; this.w = 0; this.correctRate = new float[studyCount]; } /** * Learning function, samples is a two-dimensional array containing input data and classification results, * samples[][0] represents input data* samples[][1] represents correct classification results* @param samples Training data*/ public void fit(int[][] samples) { int sampleLength = samples.length; for(int i = 0 ; i < studyCount ; i ++) { int errorCount = 0; for (int[] sample : samples) { float update = learnRate * (sample[1]-predict(sample[0])); //Update weight and threshold w += update * sample[0]; e += update; //Calculate the error number if (update != 0) errorCount++; } //Calculate the correctness of this learning correctlyRate[i] = 1 - errorCount * 1.0f / sampleLength; } } /** * Sum function, simulate summing node operation input data* weight* @param num Input data* @return Sum result z */ private float sum(int num) { return num * w + e; } /** * Activate the function, judge by summing result z and threshold e* @param num Input data* @return Classification result*/ public int predict(int num) { return sum(num) >= 0 ? 1 : -1; } /** * Print accuracy*/ public void printCorrectRate() { for (int i = 0 ; i < studyCount ; i ++) System.out.printf("Result of learning at %d-> %.2f%%/n",i + 1,correctRate[i] * 100); }}Then write the function that generates the training data
/** * Generate training data* @return training data*/ private static int[][] genStudyData() { //Here we take an integer between -100 and 100, and if greater than 0, set it to mode y = 1, otherwise y = -1 int[][] data = new int[201][2]; for(int i = -100 , j = 0; i <= 100 ; i ++ , j ++) { data[j][0] = i; data[j][1] = i >= 0 ? 1 : -1; } return data; } /** * Generate training data* @return training data*/ private static int[][] genStudyData2() { //Here we take an integer between 1~250, and if greater than 125, set it to mode y = 1, otherwise y = -1 int[][] data = new int[250][2]; for(int i = 1 , j = 0; i <= 250 ; i ++ , j ++) { data[j][0] = i; data[j][1] = i >= 125 ? 1 : -1; } return data; }Finally, the main function
public static void main(String[] args) { //The learning rate and training times here can be adjusted manually according to the situation Perceptron perceptron = new Perceptron(0.4f,500); perceptron.fit(genStudyData()); perceptron.printCorrectRate(); System.out.println(perceptron.predict(-1)); System.out.println(perceptron.predict(126)); }You can test it
limitation
This perceptron neural network is relatively simple and is suitable for linearly divided data, such as positive and negative numbers in one-dimensional, and two-dimensional coordinate quadrant classification; data that cannot be classified correctly cannot be classified, such as finding prime numbers, etc.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.