Java implementation of Perceptual machine learning algorithm

Source: Internet
Author: User

The Java implementation of the Perceptual machine learning algorithm.

The Perceptron class is used to implement a perceptual machine,

The Perceptronoriginal () method is used to realize the primitive form of perceptual machine learning algorithm.

The Perceptronanother () method is used to implement the dual form of the Perceptual machine learning algorithm (there are still bugs here).

ImportJava.util.Scanner; Public classPerceptron {Private Static Final intMAXN = 1010; Private Static Final intMAXM = 101; Private Static Double[] x =New Double[MAXN][MAXM]; Private Static Double[] y =New Double[MAXN]; Private Static intN = 0; Private Static intM = 0; Private Static DoublePhi = 0.1; Private Static Double[] W =New Double[MAXM]; Private Static Doubleb = 0; Private Static Double[] Alpha =New Double[MAXN]; Private Static Double[] G =New Double[MAXN][MAXN]; Private Static voidperceptronoriginal () { for(inti=0;i<m;i++) W[i] = 0; b= 0; BooleanOK =true;  while(OK) {OK=false;  for(inti=0;i<n;i++) {                DoubleTMP = 0;  for(intj=0;j<m;j++) tmp + = w[j] *X[i][j]; TMP+=b; if(TMP * Y[i] <= 0) {OK=true;  for(intj=0;j<m;j++) W[j] + phi * Y[i] *X[i][j]; b+ = Phi *Y[i]; }            }        }    }        Private Static voidBeforeperceptronanother () { for(inti=0;i<n;i++)             for(intj=i;j<n;j++)                 for(intk=0;k<m;k++) G[i][j]= X[i][k] *X[j][k];  for(inti=0;i<n;i++)             for(intj=0;j<i;j++) G[i][j]=G[j][i]; }         Public Static voidPerceptronanother () {//Has bugBeforeperceptronanother ();  for(inti=0;i<n;i++) Alpha[i] = 0; b= 0; BooleanOK =true;  while(OK) {OK=false;  for(inti=0;i<n;i++) {                DoubleTMP = 0;  for(intj=0;j<n;j++) TMP+ = alpha[j] * Y[J] *G[j][i]; TMP+=b; if(y[i] * tmp <= 0) {OK=true; Alpha[i]+=Phi; b+ = Phi *Y[i]; System.out.println ("alpha[" + i + "]:" + alpha[i] + ", B:" +b); }}} PHI*= 0.9; }         Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); System.out.print ("Input N:"); N=In.nextint (); System.out.print ("Input M:"); M=In.nextint (); System.out.println ("Input x:");  for(inti=0;i<n;i++)              for(intj=0;j<m;j++) X[i][j]=in.nextdouble (); System.out.println ("Input y (y={-1,+1}):");  for(inti=0;i<n;i++) Y[i]=in.nextdouble (); System.out.println ("Perceptron Original ...");        Perceptronoriginal ();  for(inti=m-1;i>=0;i--) System.out.println ("W" + i + ":" +W[i]); System.out.println ("B:" +b); System.out.println ("Perceptron another ...");        Perceptronanother ();  for(inti=0;i<m;i++) W[i] = 0;  for(inti=0;i<n;i++) {             for(intj=0;j<m;j++) {W[j]+ = alpha[i] * Y[i] *X[i][j]; }        }         for(inti=m-1;i>=0;i--) System.out.println ("W" + i + ":" +W[i]); System.out.println ("B:" +b); }}

Java implementation of Perceptual machine learning algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.