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

📄 network.java

📁 神经网络源代码,实现了一个BP神经网络,可以完成基于BP的神经网络算法.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		    db("found an output layer...skipping");		    continue;		}		layer.addBias(); 	    }	}	db("connecting network...");	if(architecture == null)	    throw new NetworkConfigurationException("No architecture has "			 + "been set.  The Network could not be connected.");	architecture.connectNetwork(this);	//  set the connected flag	connected = true;    }    /**     * Gets all layers for this network.     * @return Vector All layers in this network.     */    public final Vector getLayers() {	return layers;    }    /**     * Gets the input layer for this network.     * @return Layer The first, or input layer, of this network.     */    public final Layer getInputLayer() {	if(layers == null || layers.isEmpty())	    return null;	return (Layer) layers.firstElement();    }    /**     * Gets the hidden layers for this network.       * @return Vector All layers other than the input and output layer.     */    public final Vector getHiddenLayers() {	if(layers == null || layers.isEmpty())	    return null;	Vector hiddenLayers = null;	int size = layers.size();	for(int i = 0; i < size; i++) {	    hiddenLayers.add(layers.elementAt(i));	}	hiddenLayers.remove(hiddenLayers.firstElement());	hiddenLayers.remove(hiddenLayers.lastElement());	return hiddenLayers;    }    /**     * Gets the output layer for this network.     * @return Layer The last, or output layer, of this network.     */    public final Layer getOutputLayer() {	if(layers == null || layers.isEmpty())	    return null;	return (Layer) layers.lastElement();    }    /**     * Sets the size of this network. (i.e. the number of layers)     * This should behave much like the Vector.setSize() in that, if      * i > current Size, new Layers are added to the network and if      * i < current Size then any Layer at index i or greater are discarded.     * @param i The number of layers to be held by this network.     */    public final void setSize(int i) {	//  make sure we have an initialized vector of layers	if(layers == null)	    layers = new Vector();	//  if the network is to have less layers than it currently has, 	//  just call setSize on the Vector and let that class handle it.	if(i < layers.size()) {	    layers.setSize(i);	    return;	}	//  if the network is to have more layers than it currently has, 	//  create new layers and place them in the Vector.	int newLayers = i - layers.size();	for(int j = 0; j < newLayers; j++) {	    addLayer();	}    }    /**     * Adds a single layer to the network.     */    public final void addLayer() {	Layer layer = new Layer();	layers.add(layer);    }    /**     * Get a specific layer in the network..     * @param position The position in the network of the layer requested.     * @return Layer the layer at position.     */    public final Layer getLayerAt(int position) {	Layer layer = null;	try {	    layer = (Layer) layers.elementAt(position);	} catch (ArrayIndexOutOfBoundsException aioobe) {	    db("Layer #" + position + " was requested but does not "	       + "exist in the network, returning null.");	}	return layer;    }    /**     * Set a specific position in the network to be a specific layer.     * @param position The position in the network.     * @param layer The layer to be set at the specified position.     */    public final void setLayerAt(Layer layer, int position) {	layers.setElementAt(layer, position);    }    /**     * Get the learning flag.     * @return boolean Whether the network is in learning mode.     */    public final boolean getLearning() {	return learning;    }    /**     * Set the learning flag.     * @param learning A boolean value which if true sets the      * network in learning mode.     */    public final void setLearning(boolean learning) {	this.learning = learning;    }    /**     * Get the value of errorType.     * @return Value of errorType.     */    public final ErrorType getErrorType() {	return errorType;    }        /**     * Set the value of errorType.     * @param v  Value to assign to errorType.     */    public final void setErrorType(ErrorType  v) {	this.errorType = v;    }        /**     * Get the value of architecture.     * @return Value of architecture.     */    public final Architecture getArchitecture() {	return architecture;    }        /**     * Set the value of architecture.     * @param v  Value to assign to architecture.     */    public final void setArchitecture(Architecture  v) {	this.architecture = v;    }    /**     * Get the training set to be worked on by the network.     *     * @return TrainingSet The training set for the network.     */    public final TrainingSet getTrainingSet() {	return trainingSet;    }    /**     * Set the training set to be worked on by the network.     *     * @param trainingSet The training set for the network.     */    public final void setTrainingSet(TrainingSet trainingSet) {	this.trainingSet = trainingSet;    }    /**     * Get the training element currently being worked on by the network.     *     * @return TrainingElement The current training element.     */    public final TrainingElement getTrainingElement() {	return trainingElement;    }    /**     * Set the training element to be worked on by the network.     *     * @param trainingElement The training element for the network.     */    public final void setTrainingElement(TrainingElement trainingElement) {	this.trainingElement = trainingElement;    }    /**     * get the flag for whether this network will use a bias.     *     * @return boolean Flag for using a bias.     */    public final boolean getUseBias() {	return useBias;    }    /**     * Set whether the network will use a bias.     *     * @param useBias Whether the network will use a bias.     */    public final void setUseBias(boolean useBias) {	this.useBias = useBias;    }    /**     * Get the network error.     *     * @return double The error for the network.     */    public final double getError() {	return error;    }    /**     * Set the network error.     *     * @param error The value of the network error.     */    public final void setError(double error) {	this.error = error;    }    /**     * Get the network errorCriterion.     *     * @return double The errorCriterion for the network.     */    public final double getErrorCriterion() {	return errorCriterion;    }    /**     * Set the network errorCriterion.     *     * @param errorCriterion The value of the network errorCriterion.     */    public final void setErrorCriterion(double errorCriterion) {	this.errorCriterion = errorCriterion;    }    /**     * Get the debug flag.     *     * @return boolean Whether the network is in debug mode.     */    public static final boolean getDebug() {	return debug;    }    /**     * Set the debug flag.     *     * @param debug A boolean value which if true sets the      * network in debug mode.     */    public final void setDebug(boolean debug) {	this.debug = debug;    }    private final void db(String s) {	if(Network.getDebug())	    System.err.println("Network: " + s);    }    /**     * The 'main' of the network.     * @param args Contains the network file to load.      *              (for now since there's no gui)     */    public static void main(String[] args) {// 	if(args.length < 1) {// 	    System.err.println("No network file specified...exiting.");// 	    System.exit(-1);// 	}// 	Network network = new Network(args[0]);	//  This is just for testing purposes...	//  create the network	Network network = new Network();	//network.setUseBias(true);	//  add layers	network.setSize(3);	Vector layers = network.getLayers();	//  add neurons to each layer	Layer layer = (Layer) layers.elementAt(0);	layer.setName("Input");	layer.setSize(2);	//  set the learning rule for this layer	layer.setInputFunction(new TestInputFunction());	layer.setLearningRule(new TestLearningRule());	layer.setTransferFunction(new TestTransferFunction());	layer = (Layer) layers.elementAt(1);	layer.setName("Hidden");	layer.setSize(2);	//  set the learning rule for this layer	layer.setInputFunction(new TestInputFunction());	layer.setLearningRule(new TestLearningRule());	layer.setTransferFunction(new TestTransferFunction());	layer = (Layer) layers.elementAt(2);	layer.setName("Output");	layer.setSize(1);	//  set the learning rule for this layer	layer.setInputFunction(new TestInputFunction());	layer.setLearningRule(new TestLearningRule());	layer.setTransferFunction(new TestTransferFunction());	//  set the architecture type for the network	network.setArchitecture(new TestArchitecture());	//  set the error type	network.setErrorType(new TestErrorType());	//  set the learning flag	network.setLearning(true);	//  set the training data	network.loadTrainingData("./inputData", "./outputData");	//  connect the network	try {	    network.connect();		} catch (NetworkConfigurationException nce) {	    System.out.println("Could not connect network.");	    nce.printStackTrace();	}	network.setErrorCriterion(0.01);	network.iterateToCriterion();	//network.iterate(2000);    }}

⌨️ 快捷键说明

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