神经网络开发包joone的功能演示

import java.io.File;

import org.joone.engine.FullSynapse;
import org.joone.engine.LinearLayer;
import org.joone.engine.Monitor;
import org.joone.engine.SigmoidLayer;
import org.joone.engine.learning.TeachingSynapse;
import org.joone.io.FileInputSynapse;
import org.joone.io.FileOutputSynapse;
import org.joone.io.InputSwitchSynapse;
import org.joone.net.NeuralNet;


public class Main {
    private NeuralNet   nnet = null;
    private FileInputSynapse inputSynapse1, inputSynapse2, inputSynapse3;
    private FileInputSynapse desiredSynapse1, desiredSynapse2, desiredSynapse3;
    //    private MemoryOutputSynapse outputSynapse;
    private InputSwitchSynapse inputSw, desiredSw;
    private static String inputFile = "src/test.txt";

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  Main m = new Main();
  m.test();
  m.train();
 }

    public void train() {
       
        // get the monitor object to train or feed forward
        Monitor monitor = nnet.getMonitor();
       
        // set the monitor parameters
        monitor.setLearningRate(0.8);
        monitor.setMomentum(0.3);
        monitor.setTrainingPatterns(4);
        monitor.setTotCicles(1000);
        monitor.setLearning(true);
        nnet.addNeuralNetListener(new Test());
        interrogate();
        interrogate();
        interrogate();
       
    }
   
    private void interrogate() {
        // neuralNet is an instance of NeuralNet
        Monitor monitor=nnet.getMonitor();
        monitor.setTotCicles(1);
        monitor.setLearning(false);
        FileOutputSynapse output=new FileOutputSynapse();
        // set the output synapse to write the output of the net
        output.setFileName("src/xorOut.txt");
        // inject the input and get the output
        if(nnet!=null) {
            nnet.addOutputSynapse(output);
            System.out.println(nnet.check());
            nnet.start();
            monitor.Go();
            nnet.join();
        }
       
  
    }
 
 public void test() {
        LinearLayer input = new LinearLayer();
        SigmoidLayer hidden = new SigmoidLayer();
        SigmoidLayer output = new SigmoidLayer();
       
        input.setRows(2);
        hidden.setRows(3);
        output.setRows(1);
       
        input.setLayerName("inputLayer");
        hidden.setLayerName("hiddenLayer");
        output.setLayerName("outputLayer");
       
        FullSynapse synapse_IH = new FullSynapse(); /* input -> hidden conn. */
        FullSynapse synapse_HO = new FullSynapse(); /* hidden -> output conn. */

        input.addOutputSynapse(synapse_IH);
        hidden.addInputSynapse(synapse_IH);
       
        hidden.addOutputSynapse(synapse_HO);
        output.addInputSynapse(synapse_HO);
       
        inputSynapse1 = new FileInputSynapse();
        inputSynapse2 = new FileInputSynapse();
        inputSynapse3 = new FileInputSynapse();
       
       
       
        inputSynapse1.setInputFile(new File(inputFile));
        inputSynapse1.setName("input1");
        inputSynapse1.setAdvancedColumnSelector("1-2");
        inputSynapse1.setFirstRow(1);
        inputSynapse1.setLastRow(4);
       
        inputSynapse2.setInputFile(new File(inputFile));
        inputSynapse2.setName("input2");
        inputSynapse2.setAdvancedColumnSelector("1-2");
        inputSynapse2.setFirstRow(2);
        inputSynapse2.setLastRow(3);
       
        inputSynapse3.setInputFile(new File(inputFile));
        inputSynapse3.setName("input3");
        inputSynapse3.setAdvancedColumnSelector("1-2");
        inputSynapse3.setFirstRow(4);
        inputSynapse3.setLastRow(4);
       
        inputSw = new InputSwitchSynapse();
        inputSw.addInputSynapse(inputSynapse1);
        inputSw.addInputSynapse(inputSynapse2);
        inputSw.addInputSynapse(inputSynapse3);
       
        input.addInputSynapse(inputSw);
       
        // The Trainer and its desired output
        desiredSynapse1 = new FileInputSynapse();
        desiredSynapse2 = new FileInputSynapse();
        desiredSynapse3 = new FileInputSynapse();
       
        desiredSynapse1.setInputFile(new File(inputFile));
        desiredSynapse1.setName("desired1");
        desiredSynapse1.setAdvancedColumnSelector("3");
        desiredSynapse1.setFirstRow(1);
        desiredSynapse1.setLastRow(4);
       
        desiredSynapse2.setInputFile(new File(inputFile));
        desiredSynapse2.setName("desired2");
        desiredSynapse2.setAdvancedColumnSelector("3");
        desiredSynapse2.setFirstRow(2);
        desiredSynapse2.setLastRow(3);
       
        desiredSynapse3.setInputFile(new File(inputFile));
        desiredSynapse3.setName("desired3");
        desiredSynapse3.setAdvancedColumnSelector("3");
        desiredSynapse3.setFirstRow(4);
        desiredSynapse3.setLastRow(4);
       
        desiredSw = new InputSwitchSynapse();
        desiredSw.addInputSynapse(desiredSynapse1);
        desiredSw.addInputSynapse(desiredSynapse2);
        desiredSw.addInputSynapse(desiredSynapse3);
       
        TeachingSynapse trainer = new TeachingSynapse();
        trainer.setDesired(desiredSw);
       
        // Now we add this structure to a NeuralNet object
        nnet = new NeuralNet();
       
        nnet.addLayer(input, NeuralNet.INPUT_LAYER);
        nnet.addLayer(hidden, NeuralNet.HIDDEN_LAYER);
        nnet.addLayer(output, NeuralNet.OUTPUT_LAYER);
        nnet.setTeacher(trainer);
        output.addOutputSynapse(trainer);
 }
 
}

 

 

 

 

import org.joone.engine.Monitor;
import org.joone.engine.NeuralNetEvent;
import org.joone.engine.NeuralNetListener;


public class Test implements NeuralNetListener {

 @Override
 public void cicleTerminated(NeuralNetEvent arg0) {
  // TODO Auto-generated method stub

 }

 @Override
 public void errorChanged(NeuralNetEvent e) {
  // TODO Auto-generated method stub
        Monitor mon = (Monitor)e.getSource();
        System.out.println("Cycle: "+(mon.getTotCicles()-mon.getCurrentCicle())+" RMSE:"+mon.getGlobalError());

 }

 @Override
 public void netStarted(NeuralNetEvent arg0) {
  // TODO Auto-generated method stub
  System.out.println("Training...");
 }

 @Override
 public void netStopped(NeuralNetEvent arg0) {
  // TODO Auto-generated method stub
   System.out.println("Stopped");
 }

 @Override
 public void netStoppedError(NeuralNetEvent arg0, String arg1) {
  // TODO Auto-generated method stub

 }

}

 

 

 

 

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值