RaspberryPi Model 3B-neuroph

在Raspberry Pi上面跑个Neuroph Sample.

下载neuroph库,在代码目录下创建lib目录,咱这边用的是2.7版本

拷贝主要的几个库neuroph-core-2.7.jar neuroph-imgrec-2.7.jar neuroph-ocr-2.7.jar neuroph-samples-2.7.jar到lib目录下

sample code-Multi layer perceptron for XOR

perceptron.java

  import java.util.Arrays;
  import org.neuroph.core.NeuralNetwork;
  import org.neuroph.core.events.LearningEvent;
  import org.neuroph.core.events.LearningEventListener;
  import org.neuroph.core.learning.DataSet;
  import org.neuroph.core.learning.DataSetRow;
  import org.neuroph.core.learning.LearningRule;
  import org.neuroph.nnet.MultiLayerPerceptron;
  import org.neuroph.nnet.learning.BackPropagation;
  import org.neuroph.nnet.learning.MomentumBackpropagation;
  import org.neuroph.util.TransferFunctionType;
 
  public class perceptron {
 
      public static void main(String[] args){
         new  perceptron().run();
       }
 
      public void run(){
 
         DataSet trainingSet = new DataSet(2,1);
         trainingSet.addRow(new DataSetRow(new double[]{0,0},new double[]{0}));
         trainingSet.addRow(new DataSetRow(new double[]{0,1},new double[]{1}));
         trainingSet.addRow(new DataSetRow(new double[]{1,0},new double[]{1}));
         trainingSet.addRow(new DataSetRow(new double[]{1,1},new double[]{0}));
 
 
         NeuralNetwork Multiperceptron = new MultiLayerPerceptron(TransferFunctionType.TANH,2,3,1);
 
         if(Multiperceptron.getLearningRule() instanceof MomentumBackpropagation)
                             ((MomentumBackpropagation)Multiperceptron.getLearningRule()).setBatchMode(true);
 
         LearningRule lr = Multiperceptron.getLearningRule();
         lr.addListener(listener);
 
         Multiperceptron.learn(trainingSet);
         System.out.println("Testing trained perceptron");
         testNeuralNetwork(Multiperceptron,trainingSet);
 
          }
 
      public static void testNeuralNetwork(NeuralNetwork neuralNet, DataSet testSet) {
 
          for(DataSetRow trainingElement : testSet.getRows()) {
              neuralNet.setInput(trainingElement.getInput());
              neuralNet.calculate();
              double[] networkOutput = neuralNet.getOutput();
 
              System.out.print("Input: " + Arrays.toString(trainingElement.getInput()) );
              System.out.println(" Output: " + Arrays.toString(networkOutput) );
          }
      }
 
      private LearningEventListener listener = new LearningEventListener(){
      @Override
      public void handleLearningEvent(LearningEvent event){
                BackPropagation bp = (BackPropagation)event.getSource();
                System.out.println(bp.getCurrentIteration() + ". iteration : "+ bp.getTotalNetworkError());
              }
      };
  }

然后重点来了,如何正确引入库(这里只用到核心库neuroph-core-2.7.jar),并编译。

javac -cp lib/neuroph-core-2.7.jar: perceptron.java

正确的运行姿势

java -cp perceptron.jar:lib/neuroph-core-2.7.jar: perceptron

Leave a Reply

Your email address will not be published. Required fields are marked *