0

我正在测试数字识别的编码。这是我的源代码。P/s:我编辑了我的编码。这是我主要的完整编码。我尝试为 sigmoid 函数添加 public/protected/private,但错误不断增加。

  import java.util.Scanner;
  import java.io.*;
  import java.util.*;
  import javax.swing.JOptionPane;

  public class Recog
  {
     public static void main(String[] args) 
     {
     int row, col, data;
     int[][] input = new int[10][35];   //array for input data
     int[][] target = new int[10][10];  //array for target data
     double[][] weight1 = new double[20][35];   //weight between input & hidden layer
     double[][] weight2 = new double[10][20];   //weight bween hidden & output layer
     double[] threshold1 = new  double[20];
     double[] threshold2 = new double[10];  //array for threshold value
     double[] error = new double[20];   //error
     double[] errorgradient1 = new double[20]; //error gradient between input & hidden layer
    double[] errorgradient2 = new double[10];   //error gradient between hidden & output layer
    double alpha=0.9;

     double randomNumber = Math.random();
     double randomMax = Math.random();

     System.out.println("---------------------------------------------------------");
     System.out.println("     Initialize weight & threshold at hidden layer       ");
     System.out.println("---------------------------------------------------------");

     for(row=0; row<20; row++)
     {
        System.out.println("Initialization of weighted values at neuron.");

        for(col=0; col<35; col++)
        {
           weight1[row][col]=((randomNumber/randomMax)*2.4)/35;

           if((randomNumber%2+1)==1)    //if 1 becomes negative
           {
              weight1[row][col]=weight1[row][col]*(-1);
           }

           System.out.println("Value of Hidden layer - neuron W " +row +" " +col +"[" +weight1[row][col] +"]");
        }

        threshold1[row]=(randomNumber/randomMax)*0.5;

        if ((randomNumber%3+1)==1)
        {
           threshold1[row]=threshold1[row]*(-1);
           System.out.println("Initialization of threshold values of neuron :");
           System.out.println("initialization of neuron value : " +row +" [" +threshold1[row] +"]");
        }
        System.out.println("End of Neuron (1)");
        //System.in.read(); 
     }

     System.out.println("---------------------------------------------------------");
     System.out.println("     Initialize weight & threshold at output layer       ");
     System.out.println("---------------------------------------------------------");

     for(row=0;row<10;row++)
     {
        System.out.println("Initialization of weighted values at neuron");

        for(col=0;col<20;col++)
        {
           weight2[row][col]=((randomNumber/randomMax)*2.4)/35;

           if((randomNumber%2+1)==1)    //if 1 then negative
           {
              weight2[row][col]=weight2[row][col]*(-1);
           }

           System.out.println("Value of Output Layer - neuron W " +row +" " +col +" [" +weight2[row][col] +"]");
        }

        threshold2[row]=((randomNumber/randomMax) * 0.5);

        if((randomNumber%3+1)==1)   //if 1 then negative
        {
           threshold2[row]=threshold2[row]*(-1);

           System.out.println("Initialization of threshold values at neuron : " +row);
           System.out.println("threshold value at neuron : " +threshold2[row]);
        }
        System.out.println("End of Neuron (2)");

        String fileName="number.txt";   //Name of the file
        try
        {
           FileReader inFile = new FileReader(fileName);
           BufferedReader bufferReader = new BufferedReader(inFile);

           String line;

           while ((line = bufferReader.readLine()) != null) // Read file line by line and print on the console
           {
              for(row=0; row<10; row++)
              {
                 for (col=0; col<35; col++)
                 {
                    System.out.println(input[row][col] +" ");
                 }
                 System.out.println("Row : " +row);
              }
           }
           bufferReader.close();    //Close the buffer reader
        }

           catch(Exception x) //if cannot read file
           {
              System.out.println("Error while reading file line by line:" + x.getMessage());                      
           }

        String fileName2=("target.txt");    //read target file
        try
        {
           FileReader inFile2 = new FileReader(fileName2);
           BufferedReader bufferReader = new BufferedReader(inFile2);

           String line2;

           while ((line2 = bufferReader.readLine()) != null)    // Read file line by line and print on the console
           {
              for(row=0; row<10; row++)
              {
                 for (col=0; col<10; col++)
                 {
                    System.out.println(target[row][col] +" ");
                 }
              }
           }
           bufferReader.close();    //Close the buffer reader
        }

           catch(Exception x) //if cannot read file
           {
              System.out.println("Error while reading file line by line:" + x.getMessage());                      
           }

        //iteration-------------------------------------------------------------------------------

        int epoch=0;
        int milestone=1000;

        while (epoch<1000000)
        {
            for(data=0; data<10; data++){}  // end data

            //learning process
            System.out.println("----------LEARNING PROCESS STARTS HERE----------");

            double[] activation_hidden = new double[20];
            double[] activation_output = new double[10];
            double temp_dotproduct=0;
            double[][] deltaweight1 = new double[10][20];
            double[][] deltaweight2 = new double[20][35];
            double dot;
            int neuron=0;
            data=0;

            //start
            int epoch=0;
            int milestone=1000;
            while (epoch<1000000)
            {
                for (data=0; data<10; data++)
                {
                    //test activation for all data
                    for (data=0; data<20; data++)   //close at the end of network output
                    {
                        //test for first data
                        for (row=0; row<20; row++)
                        {
                            //do summation weight * input
                            for (col=0; col<35; col++)
                            {
                                dot=weight1[row][col] * input[data][col];
                                temp_dotproduct = temp_dotproduct + dot;
                            }

                            //activate the neuron when dot product of input x weight is finished
                            activation_hidden[row] = sigmoid(temp_dotproduct-threshold1[row]);

                            //reinitialize temp for the next neuron activation
                            temp_product=0;
                        }

                        for(row=0; row<10; row++)
                        {
                            for(col=0; col<20; col++)
                            {
                                dot = activation_hidden[col] * weight2[row][col];
                                temp_dotproduct = temp_dotproduct+dot;
                            }

                            activation_output[row] = sigmoid(temp_dotproduct-threshold2[row]);

                            //reinitialize temp for the next neuron activation
                            temp_dotproduct=0;
                        }

                        //error is calculated by ---> error = desired-actual <---

                        double errortemp=0;

                        //calculate error of each output neuron
                        // REMEMBER ! each neuron has their own error value

                        for(row=0; row<10; row++)
                        {
                            error[row]=target[data][row] - activation_output[row];
                            errortemp = error[row];

                            System.out.println("Error at neuron " +row +" is " +errortemp);
                        }

                        //next process is weight update - need to calculate the error gradient first and the network error(d-a)

                        //calculating error gradient
                        for (row=0; row<10; row++)
                        {
                            errorgradient2[row] = activation_output[row] * (1 - acivation_output[row]) * error[row];
                            errortemp = errorgradient1[row];

                            System.out.println("Error gradient at output neuron " +row +" is " +errortemp[row]);
                        }

                        //calculating error gradient first
                        for (row=0; row<10; row++)
                        {
                            errorgradient2[row] = activation_output[row] * (1 - acivation_output[row]) * error[row];
                            errortemp = errorgradient1[row];                        
                        }

                        //calculating weight corrections
                        //dw[outputneuron][hiddenneuron]
                        for (col=0; col<10; col++)
                        {
                            for (row=0; row<20; row++)
                            {
                                deltaweight1[col][row] = alpha * activation_hidden[row] * errorgradient1[col];
                            }
                        }

                        //calculate error gradient at hidden layer
                        int row1;

                        for (row1=0; row1<20; row++)
                        {
                            //calculate the hidden first
                            double sumOfErrorGradientTimesWeightOutput = 0;
                            for (col=0; col<10; col++)
                            {
                                for(row=0; row<20; row++)
                                {
                                    sumOfErrorGradientTimesWeightOutput = errorgradient1[col] * weight2[col][row];
                                }
                            }

                            errorgradient2[20] = activation_hidden[row] * (1-activation_hidden[row]) * sumOfErrorGradientTimesWeightOutput;
                        }

                        //calculating weight corrections
                        //input[samplesize][inputneuron]
                        //delta[hiddenneuron][inputneuron]

                        for (col=0; col<20; col++)
                        {
                            for (row=0; row<35; row++)
                            {
                                deltaweight2[col][row] = alpha * input[data][row] * errorgradient1[col];
                            }
                        }

                        //update the weights
                        for (row=0; row<20; row++)
                        {
                            for (col=0; col<35; col++)
                            {
                                weight2[row][col] = weight2[row][col] + deltaweight2[row][col];
                            }
                        }   //hidden weight

                        for (row=0; row<20; row++)
                        {
                            for (col=0; col<35; col++)
                            {
                                weight2[row][col] = weight2[row][col] + deltaweight2[row][col];
                            }
                        }   //output weight

                        System.out.println("Epoch : " +epoch);
                        //end of learning process

                        epoch++;

                        if (epoch==milestone)
                        {
                            System.out.println(epoch);
                            milestone = milestone + 1000;
                        }
                    }   //end epoch

                    System.out.println("---------------------------------------------------------");
                    System.out.println("                Testing the Input Samples                    ");
                    System.out.println("---------------------------------------------------------");

                    System.out.println("Enter Value between 0 - 9");
                        answer = Integer.parseInt();
                    if (data<=10 && data>=0)
                    {
                        for (col=0; col<35; col++)
                        {
                            if (input[data][col] == 1)
                                System.out.print("*");
                            else
                                System.out.print(" ");

                            if (col==4 || col==9 || col==14 || col==19 || col==24 || col=29 || col=24)
                                System.out.println();
                        }
                        System.out.println();

                        System.out.println("Target");                   
                        for (col=0; col<10; col++)
                        {
                            System.out.println(target[data][col]);
                        }
                        System.out.println();
                    }

                    else
                    {
                        System.out.println("Wrong input. Pick a number between 0 - 9");
                    }

                    //To stop the program
                    Scanner scanner = new Scanner(System.in);    
                    System.out.println("Continue? (Y/N) : ");
                    char ch = scanner.next().charAt(0);
                    if(ch=='Y' || ch=='y')
                    {   
                        System.out.println("exiting");
                    break;
                    }
                }
                return 0;
     }

  static double sigmoid (double a) 
  {
    return 1 / (1 + Math.exp(-(a)));
  }
}

编译后出现4个错误

error: illegal start of expression
  static double sigmoid (double a) 
  ^
error: ';' expected
  static double sigmoid (double a) 
                       ^
error: ';' expected
  static double sigmoid (double a) 
                                 ^
error: reached end of file while parsing
}
 ^
4 errors

谁能告诉我我哪里做错了?谢谢你。

4

2 回答 2

2

}在方法声明之前的某处缺少关闭。

于 2014-11-22T19:23:39.363 回答
0

您缺少“sigmoid”方法的访问级别修饰符,只需在声明的第一个位置键入 public、private 或 protected。

于 2014-11-22T23:00:46.560 回答