📄 neuralnetmatrix.java
字号:
/* * NeuralNetMatrix.java * * Created on 20 gennaio 2004, 22.11 */package org.joone.helpers.structure;import org.joone.engine.*;import org.joone.engine.learning.*;import org.joone.net.*;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutput;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.*;/** * Utility class that performs several useful 'decompositions' * of a NeuralNet object, making very simple to handle its internal * structure. * This class can be useful to analyze or transfor a neural network, * because it transforms the network to a 'flat' structure, * where there isn't any object pointed more than once. * It, also, by means of the getConnectionMatrix public method, * returns a 2D representation of the internal Synapses of * a neural network. * * @author P.Marrone */public class NeuralNetMatrix { private ArrayList layers; private ArrayList connectionSet; private Monitor monitor; private Layer inputLayer = null; private Layer outputLayer = null; private int inputLayerInd = -1; private int outputLayerInd = -1; transient Hashtable synTemp; /** Creates a new instance of NeuralNetMatrix */ public NeuralNetMatrix() { } /** Creates a new instance of NeuralNetMatrix */ public NeuralNetMatrix(NeuralNet net) { this.setNeuralNet(net); } /** Method used to set the neural network to dissect */ public void setNeuralNet(NeuralNet net) { int n = net.getLayers().size(); inputLayer = net.findInputLayer(); outputLayer = net.findOutputLayer(); // Extract and save the Monitor monitor = net.getMonitor(); /* Puts the layers into an ArrayList and extracts * the synapses by inserting them into a hashtable */ layers = new ArrayList(net.getLayers()); synTemp = new Hashtable(); for (int i=0; i < n; ++i) { Layer ly = (Layer)layers.get(i); checkInputs(i, ly); checkOutputs(i, ly); } Enumeration enumerat = synTemp.keys(); connectionSet = new ArrayList(); while (enumerat.hasMoreElements()) { Object key = enumerat.nextElement(); Connection tsyn = (Connection)synTemp.get(key); int x = tsyn.getInput(); int y = tsyn.getOutput(); if (x * y > 0) { connectionSet.add(tsyn); } } } /** Converts the neural structure to a matrix * containing all the synapses of the neural network. * At each not-null [x][y] element of the 2D array, * there is a pointer to the Synapse connecting * the Layer[x] to the Layer[y]. * The returned 2D array of Synapses could be used, for instance, * to draw a graphical representation of the neural network. */ public Synapse[][] getConnectionMatrix() { Synapse[][] connectionMatrix = new Synapse[layers.size()][layers.size()]; for (int n=0; n < connectionSet.size(); ++n) { Connection tsyn = (Connection)connectionSet.get(n); int x = tsyn.getInput(); int y = tsyn.getOutput(); connectionMatrix[x-1][y-1] = tsyn.getSynapse(); } return connectionMatrix; } /** Searches a path between two layers. * Useful in order to discover recurrent networks * * @return true if there is a path from fromLayer to toLayer */ public boolean isThereAnyPath(Layer fromLayer, Layer toLayer) { boolean retValue = false; int iFrom = getLayerInd(fromLayer); int iTo = getLayerInd(toLayer); retValue = isThereAnyPath(iFrom, iTo, getConnectionMatrix()); return retValue; } /* Same as the above method, but with layers' indexes instead of pointers * Used recursively to discover paths */ private boolean isThereAnyPath(int iFrom, int iTo, Synapse[][] matrix) { boolean retValue = false; for (int t=0; (t < layers.size()) && !retValue; ++t) { Synapse conn = matrix[iFrom][t]; if ((conn != null) && (!conn.isLoopBack())) { if (t == iTo) retValue = true; else retValue = isThereAnyPath(t, iTo, matrix); } } return retValue; } /** Converts the neural structure to a matrix * containing all the synapses of the neural network. * The indexes indicates the layer in Layer[] getOrderedLayers(). * At each not-null [x][y] element of the 2D array, * there is a pointer to the Synapse connecting * the (ordered)Layer[x] to the (ordered)Layer[y]. * The returned 2D array of Synapses could be used, for instance, * to draw a graphical representation of the neural network. * * @return a matrix where the indexes x,y point to the Layers * returned by getOrderedLayers(). */ public Synapse[][] getOrderedConnectionMatrix() { // Just to fill the translation array getOrderedLayers(); Synapse[][] connectionMatrix = new Synapse[layers.size()][layers.size()]; for (int n=0; n < connectionSet.size(); ++n) { Connection tsyn = (Connection)connectionSet.get(n); int x = tsyn.getInput(); int y = tsyn.getOutput(); connectionMatrix[translation[x-1]][translation[y-1]] = tsyn.getSynapse(); } return connectionMatrix; } /** Converts the neural structure to a matrix * containing all the synapses of the neural network. * The index indicates the layer in Layer[] getOrderedLayers(). * True means connection between the corresponding layers. * * @return a matrix where the indexes x,y point to the Layers * returned by getOrderedLayers(). */ public boolean[][] getBinaryOrderedConnectionMatrix() { // Just to fill the translation array getOrderedLayers(); boolean[][] booleanConnectionMatrix = new boolean[layers.size()][layers.size()]; for (int n=0; n < connectionSet.size(); ++n) { Connection tsyn = (Connection)connectionSet.get(n); int x = tsyn.getInput(); int y = tsyn.getOutput(); booleanConnectionMatrix[translation[x-1]][translation[y-1]] = true; } return booleanConnectionMatrix; } // This array, after the call to getOrderedLayers, contains // the correspondence between the old and the new layers' order int[] translation = null; /** * This method calculates the order of the layers * of a neural network, from the input to the output. * This method fills also the translations array. * * @return An array containing the ordered Layers, from the input to the output (i.e. layers[0]=input layer, layers[n-1]=output layer. */ public Layer[] getOrderedLayers() { // TODO: To adapt to recurrent networks Synapse[][] connMatrix = getConnectionMatrix(); if (connMatrix == null) return null; // An array containing the index of each layer int[] ord = new int[layers.size()]; // First of all, finds the input layers, and assign them the order #1 ArrayList inputLayers = getInputLayers(connMatrix); for (int i=0; i < inputLayers.size(); ++i) { int ind = ((Integer)inputLayers.get(i)).intValue(); ord[ind] = 1; } boolean changed = assignOrderToLayers(ord, connMatrix); // Calculate the order until the array is OK (it didn't change) while (changed) { changed = assignOrderToLayers(ord, connMatrix); } /* Now puts the layers into ordLayers according to * the order contained in the ord[] array, and * fills the translation array. */ translation = new int[layers.size()]; Layer[] ordLayers = new Layer[layers.size()]; int n=1; // the current order number to find within ord[] for (int d=0; d < layers.size(); ++n) { // Searches in ord[] the elements containing n for (int x=0; x < ord.length; ++x) { if (ord[x] == n) { ordLayers[d] = (Layer)layers.get(x); translation[x] = d; // Layers[x] ==> orderedLayers[d] ++d; } } } return ordLayers; } /* * This routine assignes the correct order to each layer, * depending on the order of the layer that feeds it.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -