Chinaunix首页 | 论坛 | 博客
  • 博客访问: 112524
  • 博文数量: 23
  • 博客积分: 975
  • 博客等级: 准尉
  • 技术积分: 262
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-19 00:54
文章分类
文章存档

2011年(2)

2010年(3)

2008年(18)

我的朋友

分类:

2008-07-19 03:16:08

1 实验题目
构造一个基于感知器学习算法的神经学习系统。
2实验目的
理解和掌握感知器模型及其学习过程,能够利用一编程语言建立一个基于感知器/BP学习算法的神经学习系统。
3 规格说明
运行环境:

操作系统:Windows XP/windows 2000

      运行环境:JRE1.5
4 设计思想和步骤
Perceptron类说明:定义了一个人工神经元的类。

方法说明:
a) Perceptron:构造函数,length指示了输入端的数量。
b) setThreshold:设置阀值,其中参数threshold为要新设置的阀值。
c) getOutput:取得神经元的输出。
d) train:训练神经元,其中input为训练实例的二维表,d为期望结果。
算法流程:

5 实验仿真和结果分析
1.训练数据文件格式约定
第一行为感知器输入个数
第二行为训练数据的条数
余下每一行为一组数据,每一行的最后一个数据为该行输入的期望输出
(文件命名为train.txt,放在本目录下)
2.实验结果
i.与运算的测试
训练数据文件内容:
2
4
0 0 0
0 1 1
1 0 1
1 1 1

ii. 比较数字大小(左大输出0,右大输出1

训练数据文件内容:
2
10
10 2 0
32 15 0
68 23 0
92 47 0
12 56 1
58 96 1
54 12 0
78 45 0
12 98 1
36 48 1


感知器模型

package ann;

import java.lang.*;
import java.util.*;

public class Perceptron {
    private double[] input;
    private double[] weight;
    private double threshold;
    private int output;
    
    public Perceptron(){
        input=new double[1];
        weight=new double[1];
    }
    
    public Perceptron(int length){
        input=new double[length];
        weight=new double[length];
    }

    public double[] getInput() {
        return input;
    }

    public void setInput(double[] input) {
        this.input = input;
        refresh();
    }

    public double[] getWeight() {
        return weight;
    }

    public void setWeight(double[] weight) {
        this.weight = weight;
    }

    public double getThreshold() {
        return threshold;
    }

    public void setThreshold(double threshold) {
        this.threshold = threshold;
    }

    public int getOutput() {
        return output;
    }
    
    private void refresh(){
        double temp=0;
        for(int i=0;i             temp+=input[i]*weight[i];
        temp-=threshold;
        output=temp<0?0:1;
    }
    
    public void train(double[][] input,int[] d){
        //增益6因子、阀值、权值初始化

        double eta=.4;
        for(int i=0;i             weight[i]=.1;
        //weight[0]=.5;weight[1]=.7;

        threshold=.6;
        
        int count=0;
        do {
            //对每组输入进行训练

            for (int i = 0; i < d.length; i++) {
                this.input = input[i];
                refresh();
                if (output != d[i]) {
                    count=0;
                    //修正阀值和权值

                    threshold += eta * (d[i] - output) * (-1);
                    for (int j = 0; j < input[j].length; j++)
                        weight[j] += eta * (d[i] - output) * input[i][j];
                }else
                    count++;
                
            }
        } while (count
    }
}

package ann;

import java.io.*;
import java.util.*;

public class App {

    public static void main(String[] args) {
        /*Perceptron aPerceptron=new Perceptron(2);
        double[][] trainIn={{6,2},{7,1},{4,3},{3,4},{12.4,15},{4,8},{3,10},{10,3}};
        int[] d={0,0,0,1,1,1,1,0};
        aPerceptron.train(trainIn,d);
        
        double[] in={4.5,5};
            aPerceptron.setInput(in);
            System.out.println(aPerceptron.getOutput());
        */
        double[][] trainInput;
        int[] expectation;
        int inputCount=0,trainCount=0;
        Perceptron aPerceptron;
        Scanner scanner;
        try {
            scanner=
                new Scanner(new BufferedReader(new FileReader(new File("train.txt"))));
            inputCount=scanner.nextInt();
            trainCount=scanner.nextInt();
                
            trainInput=new double[trainCount][inputCount];
            expectation=new int[trainCount];
            for(int i=0;i                 for(int j=0;j                     trainInput[i][j]=scanner.nextDouble();
                }
                expectation[i]=scanner.nextInt();
            }
            
            aPerceptron=new Perceptron(inputCount);
            aPerceptron.train(trainInput,expectation);
            Scanner enter=new Scanner(System.in);
            
            double[] test=new double[inputCount];
            while (true) {
                for (int i = 0; i < test.length; i++)
                    test[i] = enter.nextDouble();
                aPerceptron.setInput(test);
                System.out.println(aPerceptron.getOutput());
            }
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.exit(0);
        }
        
        
        
    }
    

}

阅读(1432) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~