⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multiplevalidationsample.java

📁 一个纯java写的神经网络源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    private double[][] constructLearningData(int learningPatternNumber){        // Define the number of columns        int columns = 5;        // Define the Learning Data array        double[][] learningData = new double[learningPatternNumber][columns];        // Define the randomized Learning Data        // Select the not Randomized patterns                // The Simple Parity Input Data        double[][] simpleParityData = {            {0.0,0.0,0.0,0.0,1.0},            {0.0,0.0,0.0,1.0,0.0},            {0.0,0.0,1.0,0.0,0.0},            {0.0,0.0,1.0,1.0,1.0},            {0.0,1.0,0.0,0.0,0.0},            {0.0,1.0,0.0,1.0,1.0},            {0.0,1.0,1.0,0.0,1.0},            {0.0,1.0,1.0,1.0,0.0},            {1.0,0.0,0.0,0.0,0.0},            {1.0,0.0,0.0,1.0,1.0},            {1.0,0.0,1.0,0.0,1.0},            {1.0,0.0,1.0,1.0,0.0},            {1.0,1.0,0.0,0.0,1.0},            {1.0,1.0,0.0,1.0,0.0},            {1.0,1.0,1.0,0.0,0.0},            {1.0,1.0,1.0,1.0,1.0}};                    for (int i = 0;i<learningPatternNumber;i++){            // Construct the each pattern of the Learning Data            learningData[i][0] = simpleParityData[i][0];            learningData[i][1] = simpleParityData[i][1];            learningData[i][2] = simpleParityData[i][2];            learningData[i][3] = simpleParityData[i][3];            learningData[i][4] = simpleParityData[i][4];        }        return learningData;    }        /** Constructs the network Test Data based on the GUI options - Class Method     * @param testPatternNumber The int Number of Test Patterns     * @return The Test Patterns Vector     */    private double[][] constructTestData(int testPatternNumber){        // Define the number of columns        int columns = 5;                // Define the Learning Data array        double[][] testData = new double[testPatternNumber][columns];        // Define the randomized Learning Data        // Select the not Randomized patterns        // The Simple Parity Input Data        double[][] simpleParityData = {{0.0,0.0,0.0,0.0,1.0},{0.0,0.0,0.0,1.0,0.0},{0.0,0.0,1.0,0.0,0.0},{0.0,0.0,1.0,1.0,1.0},{0.0,1.0,0.0,0.0,0.0},{0.0,1.0,0.0,1.0,1.0},{0.0,1.0,1.0,0.0,1.0},{0.0,1.0,1.0,1.0,0.0},{1.0,0.0,0.0,0.0,0.0},{1.0,0.0,0.0,1.0,1.0},{1.0,0.0,1.0,0.0,1.0},{1.0,0.0,1.0,1.0,0.0},{1.0,1.0,0.0,0.0,1.0},{1.0,1.0,0.0,1.0,0.0},{1.0,1.0,1.0,0.0,0.0},{1.0,1.0,1.0,1.0,1.0}};        for (int i = 0;i<testPatternNumber;i++){            // Construct the each pattern of the Test Data            testData[i][0] = simpleParityData[i][0];            testData[i][1] = simpleParityData[i][1];            testData[i][2] = simpleParityData[i][2];            testData[i][3] = simpleParityData[i][3];            testData[i][4] = simpleParityData[i][4];        }        return testData;    }        private NeuralNet initializeModularParity(int learningPatternNumber,int testPatternNumber){        // Initialize the neural networks        NeuralNet network = new NeuralNet();        NestedNeuralLayer firstNetwork = new NestedNeuralLayer();        NestedNeuralLayer secondNetwork = new NestedNeuralLayer();        // Set the First & Second network properties        firstNetwork.setNeuralNet(xorNet);        secondNetwork.setNeuralNet(xorNet);        firstNetwork.setLayerName("First Network");        secondNetwork.setLayerName("Second Network");        //firstNetwork.setLearning(true);        //secondNetwork.setLearning(true);        // Define & Initialize the Learning & Test inputs        double[][] learningData=constructLearningData(learningPatternNumber);        double[][] testData=constructTestData(testPatternNumber);        // Define & Initialize the network layers and Define the layer names        LinearLayer inputFirst=new LinearLayer();        LinearLayer inputSecond=new LinearLayer();        SigmoidLayer hidden=new SigmoidLayer();        SigmoidLayer output=new SigmoidLayer();        inputFirst.setLayerName("First Input Third Network Layer");        inputSecond.setLayerName("Second Input Third Network Layer");        hidden.setLayerName("Hidden Third Network Layer");        output.setLayerName("Output Third Network Layer");        // Define the number of neurons for each layer        inputFirst.setRows(1);        inputSecond.setRows(1);        hidden.setRows(2);        output.setRows(1);        // Define the first network output -> input connection for the third network        DirectSynapse firstSynapseOI=new DirectSynapse();        firstSynapseOI.setName("First OI Synapse");        // Define the second network output -> input connection for the third network        DirectSynapse secondSynapseOI=new DirectSynapse();        secondSynapseOI.setName("First OI Synapse");        // Define the first input -> hidden connection for the third network        FullSynapse firstSynapseIH=new FullSynapse();        firstSynapseIH.setName("First IH Synapse");        // Define the Second input -> hidden connection for the third network        FullSynapse secondSynapseIH=new FullSynapse();        secondSynapseIH.setName("Second IH Synapse");        // Define the hidden -> output connection for the third network        FullSynapse synapseHO=new FullSynapse();        synapseHO.setName("HO Synapse");        // Connect the First Network with the First Input Third Network Layer        NeuralNetFactory.connect(firstNetwork,firstSynapseOI,inputFirst);        // Connect the Second Network with the Second Input Third Network Layer        NeuralNetFactory.connect(secondNetwork,secondSynapseOI,inputSecond);        // Connect the First Input Third Network Layer with the Hidden Third Network Layer        NeuralNetFactory.connect(inputFirst,firstSynapseIH,hidden);        // Connect the Second Input Third Network Layer with the Hidden Third Network Layer        NeuralNetFactory.connect(inputSecond,secondSynapseIH,hidden);        // Connect the Hidden Third Network Layer with the Output Third Network Layer        NeuralNetFactory.connect(hidden,synapseHO,output);        // Define & Initialize the First Learning Input Synapse        MemoryInputSynapse firstLearningInputSynapse=NeuralNetFactory.createInput("First Learning Input Synapse",learningData,1,1,2);        // Define the First Test Input Synapse        MemoryInputSynapse firstTestInputSynapse=NeuralNetFactory.createInput("First Test Input Synapse",testData,1,1,2);        // Initialize the First Input Switch Synapse        LearningSwitch firstInputSwitch=NeuralNetFactory.createSwitch("First Input Switch Synapse",firstLearningInputSynapse,firstTestInputSynapse);        // Connect the First Input Switch Synapse to the First Network        firstNetwork.addInputSynapse(firstInputSwitch);        // Define & Initialize the Second Learning Input Synapse        MemoryInputSynapse secondLearningInputSynapse=NeuralNetFactory.createInput("Second Learning Input Synapse",learningData,1,3,4);        // Define the Second Test Input Synapse        MemoryInputSynapse secondTestInputSynapse=NeuralNetFactory.createInput("Second Test Input Synapse",testData,1,3,4);        // Initialize the Second Input Switch Synapse        LearningSwitch secondInputSwitch=NeuralNetFactory.createSwitch("Second Input Switch Synapse",secondLearningInputSynapse,secondTestInputSynapse);        secondInputSwitch.setStepCounter(false);        // Connect the Second Input Switch Synapse to the Second Network        secondNetwork.addInputSynapse(secondInputSwitch);        // Define the Trainer Input Switch        MemoryInputSynapse learningDesiredSynapse=NeuralNetFactory.createInput("Learning Desired Synapse",learningData,1,5,5);        // Define the Test Input Synapse        MemoryInputSynapse testDesiredSynapse=NeuralNetFactory.createInput("Test Desired Synapse",testData,1,5,5);        // Initialize the Input Switch Synapse        LearningSwitch learningSwitch=NeuralNetFactory.createSwitch("Learning Switch Synapse",learningDesiredSynapse,testDesiredSynapse);        // Define the Trainer and link it to the Monitor        TeachingSynapse trainer=new TeachingSynapse();        trainer.setName("Modular Parity Trainer Synapse");        // Connect the Teacher to the Output Layer        output.addOutputSynapse(trainer);        // Connect the Learning Switch Synapse to the Trainer        trainer.setDesired(learningSwitch);        // Define the Output Synapse Memory (Data)        MemoryOutputSynapse outputMemoryData=new MemoryOutputSynapse();        outputMemoryData.setName("Output Data");        // Connect the Output Memory Synapse (Data) to the Output Third Network Layer        output.addOutputSynapse(outputMemoryData);        // Incorpore the network components to the NeuralNet object        network.addLayer(firstNetwork);        network.addLayer(secondNetwork);        network.addLayer(inputFirst);        network.addLayer(inputSecond);        network.addLayer(hidden);        network.addLayer(output);        network.setTeacher(trainer);        network.getMonitor().setLearningRate(0.5);        network.getMonitor().setMomentum(0.5);        network.getMonitor().setTotCicles(totCycles);        return network;    }    /** Initializes the EETNN Neural Network I - Class Method     * @param learningPatternNumber Number of Learning Patterns     * @param testPatternNumber Number of Test Patterns     */    private NeuralNet initializeNetworkI(int learningPatternNumber,int testPatternNumber){        // Initialize the neural network        NeuralNet network=new NeuralNet();        // Define & Initialize the network layers and Define the layer names        LinearLayer input=new LinearLayer();        SigmoidLayer hidden=new SigmoidLayer();        SigmoidLayer output=new SigmoidLayer();        input.setLayerName("Input Layer");        hidden.setLayerName("Hidden Layer");        output.setLayerName("Output Layer");        // Define the number of neurons for each layer        input.setRows(2);        hidden.setRows(2);        output.setRows(1);        // Define the input -> hidden connection        FullSynapse synapseIH=new FullSynapse();        synapseIH.setName("IH Synapse");        // Define the hidden -> output connection        FullSynapse synapseHO=new FullSynapse();        synapseHO.setName("HO Synapse");        // Connect the Input Layer with the Hidden Layer        NeuralNetFactory.connect(input,synapseIH,hidden);        // Connect the Hidden Layer with the Output Layer        NeuralNetFactory.connect(hidden,synapseHO,output);        // Define & Initialize the Learning Input Synapse        XLSInputSynapse learningInputSynapse = new XLSInputSynapse();        learningInputSynapse.setName("Learning Input Synapse");        learningInputSynapse.setInputFile(new File("/tmp/wine.xls"));        learningInputSynapse.setAdvancedColumnSelector("6,7");        learningInputSynapse.setSheetName("wine.data");        learningInputSynapse.setFirstRow(2);        learningInputSynapse.setLastRow(100);        // Define the Test Input Synapse        XLSInputSynapse testInputSynapse = new XLSInputSynapse();        testInputSynapse.setName("Test Input Synapse");        testInputSynapse.setInputFile(new File("/tmp/wine.xls"));        testInputSynapse.setAdvancedColumnSelector("6,7");        testInputSynapse.setSheetName("wine.data");        testInputSynapse.setFirstRow(2);        testInputSynapse.setLastRow(100);        // Initialize the Input Switch Synapse        LearningSwitch inputSwitch=NeuralNetFactory.createSwitch("Input Switch Synapse",learningInputSynapse,testInputSynapse);        // Connect the Input Switch Synapse to the Input Layer        input.addInputSynapse(inputSwitch);        // Define the Trainer's Learning Input Synapse        XLSInputSynapse learningDesiredSynapse = new XLSInputSynapse();        learningDesiredSynapse.setName("Learning Desired Synapse");        learningDesiredSynapse.setInputFile(new File("/tmp/wine.xls"));        learningDesiredSynapse.setAdvancedColumnSelector("8");        learningDesiredSynapse.setSheetName("wine.data");        learningDesiredSynapse.setFirstRow(2);        learningDesiredSynapse.setLastRow(100);        // Define the Trainer's Test Input Synapse        XLSInputSynapse testDesiredSynapse = new XLSInputSynapse();        testDesiredSynapse.setName("Test Desired Synapse");        testDesiredSynapse.setInputFile(new File("/tmp/wine.xls"));        testDesiredSynapse.setAdvancedColumnSelector("8");        testDesiredSynapse.setSheetName("wine.data");        testDesiredSynapse.setFirstRow(2);        testDesiredSynapse.setLastRow(100);        // Initialize the Input Switch Synapse        LearningSwitch learningSwitch=NeuralNetFactory.createSwitch("Learning Switch Synapse",learningDesiredSynapse,testDesiredSynapse);        // Define the Trainer and link it to the Monitor        TeachingSynapse trainer=new TeachingSynapse();        trainer.setName("EETNN Trainer Synapse");        // Connect the Teacher to the Output Layer        output.addOutputSynapse(trainer);        // Connect the Learning Switch Synapse to the Trainer        trainer.setDesired(learningSwitch);        // Define the Output Synapse Memory (Data)    /*XLSOutputSynapse outputMemoryData=new XLSOutputSynapse();    outputMemoryData.setName("Output Data");    outputMemoryData.setFileName("/tmp/OutputData.xls");    outputMemoryData.setSheetName("wine.data");    // Connect the Output Memory Synapse (Data) to the Output    output.addOutputSynapse(outputMemoryData); */        // Incorpore the network components to the NeuralNet object        network.addLayer(input);        network.addLayer(hidden);        network.addLayer(output);        network.setTeacher(trainer);        return network;    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -