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

📄 neuralnetmatrix.java

📁 一个纯java写的神经网络源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * If the Layer[n] feeds the Layer[m], then the Layer[m] is assigned     * the Layer[n]'s order + 1.     * Returns false only if no order is changed (to understand when to stop).     */    private boolean assignOrderToLayers(int[] ord, Synapse[][] connMatrix) {        boolean changed = false;        for (int x=0; x < ord.length; ++x) {            int currLayer = ord[x];            if (currLayer > 0) {                for (int y=0; y < connMatrix[x].length; ++y) {                    if ((connMatrix[x][y] != null) && !connMatrix[x][y].isLoopBack()) {                        if (currLayer >= ord[y]) {                            ord[y] = currLayer + 1;                            changed = true;                        }                    }                }            }        }        return changed;    }        /** Searches for all the input layers of the network.     * An input layer is represented by each column in     * connectionMatrix that doesn't contain any Synapse     *     * @return an ArrayList containing Integers that point to the indexes of the input layers     */    public ArrayList getInputLayers(Synapse[][] connMatrix) {        ArrayList inputs = new ArrayList();        for (int y=0; y < connMatrix.length; ++y) {            boolean found = false;            for (int x=0; x < connMatrix[y].length; ++x) {                if (connMatrix[x][y] != null) {                    // Recurrent connections are ignored                    if (!connMatrix[x][y].isLoopBack()) {                        found = true;                        break;                    }                }            }            if (!found) {                inputs.add(new Integer(y));            }        }        return inputs;    }        /**     * Clones a neural element.     * @return the clone of the element passed as parameter     * @param element The element to clone     */    public Serializable cloneElement(Serializable element){        try {            //Serialize to a byte array            ByteArrayOutputStream bos = new ByteArrayOutputStream() ;            ObjectOutput out = new ObjectOutputStream(bos) ;            out.writeObject(element);            out.close();            byte[] buf = bos.toByteArray();            // Deserialize from a byte array            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buf));            Object theCLone =  in.readObject();            in.close();            return (Serializable)theCLone;        } catch (IOException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        }        return null;    }    //    private void insertChild(int x, Synapse[][] connMatrix, Integer[] ord) {//        for (int y=0; y < connMatrix[x].length; ++y) {//            if (connMatrix[x][y] != null) {//                insertOrderedLayer(ord, y+1);//            }//        }//    }////    private void insertOrderedLayer(Integer[] aInd, int ind) {//        for (int i=0; i < aInd.length; ++i) {//            if ((aInd[i] != null) && (aInd[i].intValue() == ind))//                break;//            if ((aInd[i] == null) || (aInd[i].intValue() == 0)) {//                aInd[i] = new Integer(ind);//                break;//            }//        }//    }        private void checkInputs(int n, Layer ly) {        Vector inps = ly.getAllInputs();        if (inps == null)            return;        for (int i=0; i < inps.size(); ++i) {            InputPatternListener ipl = (InputPatternListener)inps.elementAt(i);            if ((ipl != null) && (ipl instanceof Synapse)) {                Connection temp = getSynapse((Synapse)ipl);                temp.setOutput(n+1);                temp.setOutIndex(i);            }        }    }        private void checkOutputs(int n, Layer ly) {        Vector outs = ly.getAllOutputs();        if (outs == null)            return;        for (int i=0; i < outs.size(); ++i) {            OutputPatternListener opl = (OutputPatternListener)outs.elementAt(i);            if ((opl != null) && (opl instanceof Synapse)) {                Connection temp = getSynapse((Synapse)opl);                temp.setInput(n+1);                temp.setInpIndex(i);            }        }    }        /** Gets a Connection from the hashtable, and if it doesn't     * exist, it is created and put into the hashtable     */    private Connection getSynapse(Synapse s) {        Connection temp = (Connection)synTemp.get(s);        if (temp == null) {            temp = new Connection();            temp.setSynapse(s);            synTemp.put(s, temp);        }        return temp;    }        /** Getter for property layers.     * @return Value of property layers.     *     */    public ArrayList getLayers() {        return this.layers;    }        /** Setter for property layers.     * @param layers New value of property layers.     *     */    public void setLayers(ArrayList layers) {        this.layers = layers;    }        /** Getter for property connectionSet.     * @return Value of property connectionSet.     *     */    public ArrayList getConnectionSet() {        return this.connectionSet;    }        /** Setter for property connectionSet.     * @param connectionSet New value of property connectionSet.     *     */    public void setConnectionSet(ArrayList connectionSet) {        this.connectionSet = connectionSet;    }        /** Getter for property monitor.     * @return Value of property monitor.     *     */    public Monitor getMonitor() {        return monitor;    }        /** Setter for property monitor.     * @param monitor New value of property monitor.     *     */    public void setMonitor(Monitor monitor) {        this.monitor = monitor;    }        public Layer getInputLayer() {        return inputLayer;    }        public void setInputLayer(Layer inputLayer) {        this.inputLayer = inputLayer;    }        public Layer getOutputLayer() {        return outputLayer;    }        public void setOutputLayer(Layer outputLayer) {        this.outputLayer = outputLayer;    }        public int getNumLayers() {        return layers.size();    }        /**     * Calculates the index of the input layer     * @return The index, within NeuralNet.layers[], of the input layer     */    public int getInputLayerInd() {        if (inputLayerInd == -1) {            inputLayerInd = getLayerInd(inputLayer);        }        return inputLayerInd;    }        public int getOutputLayerInd() {        if (outputLayerInd == -1) {            outputLayerInd = getLayerInd(outputLayer);        }        return outputLayerInd;    }        /** Calculates the index of a layer within the layers array.     *  This method uses the NeuralNet's      * @return the Layer's index starting from 0. Returns -1 if not found     */    public int getLayerInd(Layer layer) {        int layerInd = -1;        for (int i=0; i < layers.size(); ++i) {            Layer ly = (Layer)layers.get(i);            if (ly == layer) {                layerInd = i;                break;            }        }        return layerInd;    }    }

⌨️ 快捷键说明

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