1

对于我的 APCS 最终项目,我正在制作一个应用程序:

  • 允许用户在绘图面板上绘制数字;
  • 将每个笔划(由 xy 坐标列表表示)缩放/平移到 100x100;
  • 从缩放的笔划生成图像;
  • 从该图像生成一个二进制二维数组(0 表示白色,否则为 1);
  • 并将该二进制数组传递给神经元对象以进行字符识别。

下面的类代表神经元:

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

public class Neuron
{
    private double[][] weights;
    public static double LEARNING_RATE = 0.01;

    /**
     *Initialize weights
     *Assign random double values to weights
     */
    public Neuron(int r, int c)
    {
        weights = new double[r][c];

        PrintWriter printer = null;
        try
        {
            printer = new PrintWriter("training.txt");
        }
        catch (FileNotFoundException e) {};
        for (int i = 0; i < weights.length; i++)
        {
            for (int j = 0; j < weights[i].length; j++)
            {
                weights[i][j] = 2 * Math.random() - 1; //Generates random number between -1 and 1
                if (j < weights[i].length - 1)
                    printer.print(weights[i][j] + " ");
                else
                    printer.print(weights[i][j]);
            }
            printer.println();
        }
        printer.close();
    }

    public Neuron(String fileName)
    {
        File data = new File(fileName);
        Scanner input = null;
        try
        {
            input = new Scanner(data);
        }
        catch (FileNotFoundException e)
        {
            System.out.println("Error: could not open " + fileName);
            System.exit(1);
        }

        int r = Drawing.DEF_HEIGHT, c = Drawing.DEF_WIDTH;
        weights = new double[r][c];

        int i = 0, j = 0;
        while (input.hasNext())
        {
            weights[i][j] = input.nextDouble();
            j++;
            if (j > weights[i].length - 1)
            {
                i++;
                j = 0;
            }
        }

        for (double[] a : weights)
            System.out.println(Arrays.toString(a));

    }

    /**
     *1. Initialize a sum variable
     *2. Multiply each index of weights by each index of bin
     *3. Sum these values
     *4. Return the activated sum
     */
    public int feedforward(int[][] bin) //bin represents 2D array of binary values for a binary image
    {
        double sum = 0;
        for (int i = 0; i < weights.length; i++)
        {
            for (int j = 0; j < weights[i].length; j++)
                sum += weights[i][j] * bin[i][j];
        }
        return activate(sum);
    }

    /**
     *1. Generate a sigmoid (logistic) value from a sum
     *2. "Digitize" the sigmoid value
     *3. Return the digitized value, which corresponds to a number
     */
    public int activate(double n)
    {
        double sig = 1.0/(1+Math.exp(-1*n));
        int digitized = 0;

        if (sig < 0.1)
            digitized = 0;
        else if (sig >= 0.1 && sig < 0.2)
            digitized = 1;
        else if (sig >= 0.2 && sig < 0.3)
            digitized = 2;
        else if (sig >= 0.3 && sig < 0.4)
            digitized = 3;
        else if (sig >= 0.4 && sig < 0.5)
            digitized = 4;
        else if (sig >= 0.5 && sig < 0.6)
            digitized = 5;
        else if (sig >= 0.6 && sig < 0.7)
            digitized = 6;
        else if (sig >= 0.7 && sig < 0.8)
            digitized = 7;
        else if (sig >= 0.8 && sig < 0.9)
            digitized = 8;
        else if (sig >= 0.9)
            digitized = 9;

        System.out.println("Sigmoid value: " + sig + "\nDigitized value: " + digitized);
        return digitized;
    }

    /**
     * 1. Provide inputs and "known" answer
     * 2. Guess according to the inputs using feedforward(inputs)
     * 3. Compute the error
     * 4. Adjust all weights according to the error and learning rate
     */
    public void train(int[][] bin, int desired)
    {
        int guess = feedforward(bin);
        int error = desired-guess;

        for (int i = 0; i < weights.length; i++)
        {
            for (int j = 0; j < weights[i].length; j++)
                weights[i][j] += LEARNING_RATE * error * bin[i][j];
        }
    }

}

我使用不同的类来“训练”神经元。另一个类——TrainingConsole.java——基本上采用带有随机生成组件的“training.txt”,为其提供训练示例(图像 --> 二进制 2D 数组),并根据误差、学习率和相应的值调整权重对于垃圾箱:

   import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

import javax.imageio.ImageIO;

public class TrainingConsole
{

    private File folder;
    private File data;

    public TrainingConsole(String dataFileName, String folderName)
    {
        data = new File(dataFileName);
        folder = new File(folderName);
    }

    public void changeFolder(String folderName)
    {
        folder = new File(folderName);
    }

    public void feedAll(int desired)
    {
        System.out.println(Arrays.toString(folder.listFiles()));
        for (int i = 1; i < folder.listFiles().length; i++) //To exclude folder
        {
            BufferedImage img = new BufferedImage(Drawing.DEF_WIDTH,Drawing.DEF_HEIGHT,BufferedImage.TYPE_INT_RGB);
            try
            {

                String name = folder.listFiles()[i].getName();
                if (name.substring(name.length()-4).equals(".png"))
                    img = ImageIO.read(folder.listFiles()[i]);
            }
            catch(IOException e)
            {System.out.println("Error?");}

            int[][] bin = new int[Drawing.DEF_WIDTH][Drawing.DEF_HEIGHT];

            if (img != null)
            {
                for (int y = 0; y < img.getHeight(); y++)
                {
                    for (int x = 0; x < img.getWidth(); x++)
                    {
                        int rgb = img.getRGB(x,y);
                        //System.out.println(rgb);
                        if (rgb == -1) //White
                            bin[y][x] = 0;
                        else
                            bin[y][x] = 1;
                    }
                }
                for (int[] a : bin)
                    System.out.println(Arrays.toString(a));
                train(bin,desired);
            }
        }
    }

     public void train(int[][] bin, int desired) {
         int guess = feedforward(bin);
         int error = desired - guess;

         Scanner input = null;
         try {
             input = new Scanner(data);
         } catch (FileNotFoundException e) {
             System.exit(1);
         }
         double[][] weights = new double[Drawing.DEF_HEIGHT][Drawing.DEF_WIDTH];
         int i = 0, j = 0;
         while (input.hasNext() && i < Drawing.DEF_HEIGHT) {
             weights[i][j] = input.nextDouble();
             j++;
             if (j > weights[i].length - 1) {
                 i++;
                 j = 0;
             }
         }

         for (int k = 0; k < weights.length; k++) {
             for (int l = 0; l < weights[k].length; l++)
                 weights[k][l] += IMGNeuron.LEARNING_RATE * error * bin[k][l];
         }

         data = new File(data.getName());
         PrintWriter output = null;
         try {
             output = new PrintWriter(data);
         } catch (FileNotFoundException e) {
             System.out.println("Cannot find data");
         }
         for (int m = 0; m < weights.length; m++) {
             for (int n = 0; n < weights[m].length - 1; n++)
                 output.print(weights[m][n] + " ");
             output.print(weights[m][weights[m].length - 1]);
             output.println();
         }
         output.close();
     }

    public int feedforward(int[][] bin)
    {
        double sum = 0;

        Scanner input = null;
        try
        {
            input = new Scanner(data);
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Could not locate data");
        }
        double[][] weights = new double[Drawing.DEF_HEIGHT][Drawing.DEF_WIDTH];
        int i = 0, j = 0;
        while (i < Drawing.DEF_HEIGHT && j < Drawing.DEF_WIDTH)
        {
            //System.out.println("( " + i + " , " + j + " )");
            weights[i][j] = input.nextDouble();
            j++;
            if (j > weights[i].length - 1)
            {
                i++;
                j = 0;
            }
        }

        for (int m = 0; m < weights.length; m++)
        {
            for (int n = 0; n < weights[m].length; n++)
                sum += weights[m][n] * bin[m][n];
        }
        return activate(sum);
    }

    public int activate(double n)
    {
        double sig = 1.0/(1+Math.exp(-1*n));
        int digitized = 0;

        if (sig < 0.1)
            digitized = 0;
        else if (sig >= 0.1 && sig < 0.2)
            digitized = 1;
        else if (sig >= 0.2 && sig < 0.3)
            digitized = 2;
        else if (sig >= 0.3 && sig < 0.4)
            digitized = 3;
        else if (sig >= 0.4 && sig < 0.5)
            digitized = 4;
        else if (sig >= 0.5 && sig < 0.6)
            digitized = 5;
        else if (sig >= 0.6 && sig < 0.7)
            digitized = 6;
        else if (sig >= 0.7 && sig < 0.8)
            digitized = 7;
        else if (sig >= 0.8 && sig < 0.9)
            digitized = 8;
        else if (sig >= 0.9)
            digitized = 9;

        return digitized;
    }

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        TrainingConsole trainer = new TrainingConsole("training.txt","Training_000");

        System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        System.out.println("Training Console");
        System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");

        for (int i = 0; i <= 9; i++) {
            //System.out.print("Folder with training data for desired = " + i + ", or enter \"skip\" to skip: ");
            //String folderName = input.nextLine().trim();
            String folderName = "Training_00" + i;
            //System.out.println(folderName);
            if (!folderName.toLowerCase().equals("skip"))
            {
                trainer.changeFolder(folderName);
//              System.out.print("Press enter to run: ");
//              String noReason = input.nextLine();
                trainer.feedAll(i);
            }
            System.out.println("----------------------------------------------------------------------------------------------------ava----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        }
    }

}

对于后续的神经元构造,我将“training.txt”作为权重矩阵传递。但是,这显然不起作用:在此处输入图像描述

请帮忙!我对神经网络和机器学习真的很陌生。在这一点上,我不知道自己做错了什么:我需要更多的训练样本吗?我应该实现一个糟糕的激活功能吗?任何意见,将不胜感激。此外,如果需要,请随时请求其他代码。

4

1 回答 1

0

正如评论中所指出的,有两个主要问题,我将更详细地描述它们。

  1. 您的整个模型是单个感知器,这意味着您创建了一个从输入空间(像素)到类(数字)的线性模型。这根本行不通,它不是现代意义上的神经网络。为图像处理设计的“现代”神经网络将由数千个神经元组成,这些神经元以间非线性激活的方式连接,可能以卷积核的形式排列(因为这是用于图像识别的最先进的架构)。

  2. 你应该解决多类问题,但你实际上是在做排名。为了使 NN 分类为 K 类,你应该有 K 个输出神经元,每个神经元都会产生一个信号,解释为属于特定类的“概率”(不是严格的数学意义上的),因此为了分类 - 你会采取arg max(具有最高值的神经元数)。

一旦你解决了整个架构的这两个重要问题,你应该开始获得合理的结果,那么唯一缺少的部分就是调整超参数和获得更多的训练数据。

于 2016-05-30T19:18:02.783 回答