用感知器实现或运算

CODE

import java.util.Scanner;


public class OR {
static int N = 2;
static double[] W = new double[N];
static double sita = Math.random();
static int[] X = new int[N];
static int Y = 0;
static double c = 0.2;
static int evolve = 100;
static void input(){
int sum = 0;
for(int i=0; i X[i] = (int) (Math.random()*2);
sum += X[i];
}
if(sum>0) Y = 1;
else Y = 0;
}

static int sign(double net){
if(net>0) return 1;
else if(net<0) return -1;
else return 0;
}

static void train(){
double net = 0.0;
for(int i=0; i net += W[i]*X[i];
}
double out = sign(net);
for(int i=0; i W[i] += c*(Y-out)*X[i];
}
}

static int perceptron(int[] x){
double net = 0.0;
for(int i=0; i net += W[i]*x[i];
}
return sign(net);
}

@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int ca = 0;
while(ca < evolve){
input();
train();
ca++;
}
System.out.println("训练后权值结果:");
for(int i=0; i System.out.println(W[i]);
}
System.out.println("训练后阈值结果:");
System.out.println(sita);
System.out.println("-----------------开始运算-----------------");
while(true){
System.out.println("请输入进行或运算的操作数(0/1):");
int[] x = new int[N];
for(int i=0; i x[i] = in.nextInt();
}
int ans = perceptron(x);
System.out.println("运算结果为:");
System.out.println(ans);
}
}


}


Input&output

用感知器实现或运算_第1张图片


你可能感兴趣的:(人工智能,java)