java中的Perceptron代码

时间:2016-03-11 05:11:15

标签: java perceptron

以下是在java中学习感知器(学习权重向量)的代码。输入数据集包含大约1333行,其中每行包含标签和48个功能。 使用的标签是0和1.一半的数据用0标记,另一半用1标记。因此它是一个2级的问题。 为了获得增强向量,我已经将所有标签= 0视为标签= 1,并将整行乘以-1。因此,结果标签是-1和1。 我似乎没有得到正确的重量矢量。 (这些类是线性可分的)这段代码有什么问题?

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

class Point {
    float[] featureSet = new float[50];

    Point() {
    }

    Point(float[] x) {
        for (int i = 0; i < x.length; i++)
            featureSet[i] = x[i];
    }

    @Override
    public String toString() {
        if (null != featureSet) {
            return Arrays.toString(featureSet);
        } else {
            return null;
        }
    }
}

public class LearnPerceptron {

    float[] w = new float[50];
    static ArrayList<Point> points = new ArrayList<Point>();

    void read() {
        String fileName = "output4_train.txt";
        String line = null;
        float[] vertices = new float[50];
        try {
            FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            while ((line = bufferedReader.readLine()) != null) {

                String[] parts = line.split(" ");
                for (int i = 0; i < parts.length; i++) {
                    if (parts[i] != null && parts[i].length() > 0) {
                        vertices[i] = Float.parseFloat(parts[i]);
                    }
                }
                Point p = new Point(vertices);
                points.add(p);
            }
            bufferedReader.close();
        } catch (FileNotFoundException ex) {
            System.out.println("Unable to open file '" + fileName + "'");
        } catch (IOException ex) {
            System.out.println("Error reading file '" + fileName + "'");
        }
    }

    void init() {
        for (int i = 0; i < w.length; i++)
            w[i] = 0;
    }

    void negate() {
        for (Point p : points) {
            if (p.featureSet[0] == 0) {
                p.featureSet[0] = 1;
                for (int i = 0; i < p.featureSet.length; i++) {
                    p.featureSet[i] = -p.featureSet[i];
                }
            }
        }
    }

    void learn() {

        int iteration = 0, mistakes;

        while (true) {

            System.out.print("iteration " + iteration + ":");
            for (int i = 0; i < w.length; i++)
                System.out.print(w[i] + " ");
            System.out.println();

            mistakes = 0;
            iteration++;
            float sum = 0;
            for (Point p : points) {
                for (int j = 0; j < p.featureSet.length; j++) {
                    float f = p.featureSet[j] * w[j];
                    sum = sum + f;
                }
                if (sum <= 0) {
                    mistakes++;
                    for (int k = 0; k < p.featureSet.length; k++) {
                        w[k] += p.featureSet[k];
                    }
                }
            }
            if (mistakes == 0)
                break;
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LearnPerceptron p = new LearnPerceptron();
        p.read();
        p.init();
        p.negate();
        p.learn();

    }
}

0 个答案:

没有答案