Menu

Problem: XOR Nested ANN only through java

Help
Anonymous
2011-06-29
2013-04-16
  • Anonymous

    Anonymous - 2011-06-29

    Hello everyone,
    I go straight to the point
    I'm trying to study neural networks with java, I have some questions about JOONE I hope you can help me
    1) How do I IMPORT and USE through java code a .snet neural network, I mean: Once imported, how can I use it in my java code? I successfully exported the network created (XOR), but I can not properly load and use the trained network, I do not know where I make the mistake
    the model  considered is the XOR one.

    a)ExtractTrainedNet (performs the extraction of a previously saved neural network, the network works fine, it  has been tested with the editor)

    package xor;
    import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;
    import org.joone.engine.Layer;
    import org.joone.net.NeuralNet;
    import org.joone.net.NeuralNetLoader;
    public class ExtractTrainedNet {
    
        public ExtractTrainedNet(){
    
        }
        /**
         * @param args
         */
        public static void main(String[] args) {
            ExtractTrainedNet ex = new ExtractTrainedNet();
            ex.extract();
    
        }
    
        public void extract (){
            restoreNeuralNet("C:\\Users\\...\\Desktop\\saveNetXOR.snet");
            NeuralNet xorNNet = this.restoreNeuralNet("C:\\Users\\...\\Desktop\\saveNetXOR.snet");
            if (xorNNet != null) {
                Layer inputI = xorNNet.getInputLayer();
                inputI.removeAllInputs();
    
                Layer outputO = xorNNet.getOutputLayer();
                outputO.removeAllOutputs();
    
                String fileName= "C:\\Users\\...\\Desktop\\TraindeXOR.snet";
                try {
    
                    FileOutputStream stream = new FileOutputStream(fileName);
                    ObjectOutputStream out = new ObjectOutputStream(stream);
                    out.writeObject(xorNNet);
                    out.close();
                    } catch (Exception excp) {
                    excp.printStackTrace();
                    }
            }
        }
    
        public NeuralNet restoreNeuralNet(String fileName) {
            NeuralNetLoader loader = new NeuralNetLoader(fileName);
            NeuralNet nnet = loader.getNeuralNet();
            return nnet;
            }
    }
    

    b)NestedANN
    (This code will take care of the trained network previously saved, INPUT data are read from a text file, the result is written to another file) the network created is:

    INPUT Layer (.txt) -> NestedANN (trained neurla network)  -> OUTPUT Layer (.txt)

    the goal is to create all alone through JAVA code

    package xor;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;
    import org.joone.engine.*;
    import org.joone.engine.learning.*;
    import org.joone.io.*;
    import org.joone.net.NestedNeuralLayer;
    import org.joone.net.NeuralNet;
    import org.joone.net.NeuralNetLoader;
    public class NestedANN implements NeuralNetListener {
    
        public NestedANN(){
    
        }
        /**
         * @param args
         */
        public static void main(String[] args) {
            NestedANN nsta = new NestedANN();
            nsta.start_nestedNet();
        }
    
        public void start_nestedNet (){
    
            String trainedNeuralNet = "C:\\Users\\...\\Desktop\\TraindeXOR.snet";
            /*
             * Create the three layers
             */
    
            // Input Layer --> Nested ANN Layer --> OutputLayer
            LinearLayer input = new LinearLayer(); // INPUT Layer
            NestedNeuralLayer nest = new NestedNeuralLayer(); // NestedNerual Layer
            //SigmoidLayer output = new SigmoidLayer(); // Output Layer
            LinearLayer output = new LinearLayer(); // OUTPUT Layer
    
            /*
             * Set layer dimension (INPUT, NestedNeural, OUTPUT)
             */
            input.setRows(200); // INPUT
            nest.setNeuralNet(trainedNeuralNet); // NestedNeural
            output.setRows(1); // OUTPUT
    
            /*
             * Build the neural net connection
             */
            SangerSynapse synapse_IN = new SangerSynapse(); /* Input -> Nested connection */
            SangerSynapse synapse_NO = new SangerSynapse(); /* Nested -> Output connection */
    
            //Connect the INPUT with the Nested layer
            input.addOutputSynapse(synapse_IN);
            nest.addInputSynapse(synapse_IN);
    
            //Connect the Nested layer with the OUTPUT layer
            nest.addOutputSynapse(synapse_NO);
            output.addInputSynapse(synapse_NO);
    
            /*
             * Define an INPUT for the net
             */
            FileInputSynapse inputStream = new FileInputSynapse();
            /* The first two columns contain the input values */
            //inputStream.setAdvancedColumnSelector("1,2");
            inputStream.setFirstCol(1); // start Col 1
            inputStream.setLastCol(2); //  last Col 2
            /* This is the file that contains the input data */
            inputStream.setInputFile(new File("C:\\Users\\....\\Desktop\\testSet.txt"));
    
            //add the input synapse to the first layer, The input synapse extends the
            //Synapse object, so it can be attached to a layer like a synapse
            input.addInputSynapse(inputStream);
    
            //create an output synapse
            FileOutputSynapse fileOutput = new FileOutputSynapse();
            fileOutput.setFileName("C:\\Users\\...\\Desktop\\OutTestSet.txt");
            // attach the output synapse to the last layer of the NN
            output.addOutputSynapse(fileOutput);
    
             NeuralNet nnet = new NeuralNet();
             nnet.addLayer(input, NeuralNet.INPUT_LAYER);
             nnet.addLayer(nest);
             nnet.addLayer(output, NeuralNet.OUTPUT_LAYER);
             
            /*  
             * Set parameters for the monitor
             */
            Monitor monitor = nnet.getMonitor();
            monitor.setLearningRate(0.7); // era 0.8
            monitor.setMomentum(0.9);  //era 0.3
    
            /*
             * The application registers itself as a monitor's listener, so it can receive the
                notifications of termination from the net.
             */
            monitor.addNeuralNetListener(this);
    
            //Set all the training parameters of the net
            monitor.setTrainingPatterns(4); /* # of rows in the input file */
            monitor.setTotCicles(1); /* How many times the net must be trained*/
            nest.setLearning(false);//La rete non deve essere addestrata
            nnet.getMonitor().setLearning(false);
    
            //Start the nested neural network
            //nest.start();
            //monitor.setLearning(false); /* The net must be trained */
            nnet.go(); /* The network starts the training phase */      
            //Start the nested neural network
            nest.start();
        }
    
        //Method for restore neuralNet
        public NeuralNet restoreNeuralNet(String fileName) {
            NeuralNetLoader loader = new NeuralNetLoader(fileName);
            NeuralNet nnet = loader.getNeuralNet();
            return nnet;
            }
        public void netStopped(NeuralNetEvent e) {
            System.out.println("Training finished");
        }
        
     public void cicleTerminated(NeuralNetEvent e) {
         Monitor mon = (Monitor)e.getSource();
         long c = mon.getCurrentCicle();
         /* We want print the results every 100 epochs */
         if (c % 100 == 0){
         System.out.println(c + " epochs remaining - RMSE = " +
         mon.getGlobalError());}
    
         }
    
        
        public void netStarted(NeuralNetEvent e) {
            System.out.println("Training...");
        }
        
        public void errorChanged(NeuralNetEvent e) {
            Monitor mon = (Monitor)e.getSource();
            /* We want print the results every 200 cycles */
            if (mon.getCurrentCicle() % 200 == 0)
                System.out.println(mon.getCurrentCicle() + " epochs remaining - RMSE = " + mon.getGlobalError());
        }
        
        public void netStoppedError(NeuralNetEvent e,String error) {
        }
    }
    

    I hope I have clearly explained the problem I'm facing,
    thanks for your help
    Marco

     
  • Anonymous

    Anonymous - 2011-06-29

    Sorry, there is a copying error in this line:

    /* * Set layer dimension (INPUT, NestedNeural, OUTPUT) */ input.setRows(200); // INPUT nest.setNeuralNet(trainedNeuralNet); // NestedNeural output.setRows(1); // OUTPUT
    

    The correct code is:

    /* * Set layer dimension (INPUT, NestedNeural, OUTPUT) */ input.setRows(2); // INPUT nest.setNeuralNet(trainedNeuralNet); // NestedNeural output.setRows(1); // OUTPUT
    

    I hope that there are no other copying errors, however I think that in general the logic of the code  is not correct

     

Log in to post a comment.