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

📄 modelserializer.java

📁 利用Java实现的神经网络工具箱
💻 JAVA
字号:
/* * $RCSfile: ModelSerializer.java,v $ * $Revision: 1.4 $ * $Date: 2005/05/01 19:54:17 $ * * NeuralNetworkToolkit * Copyright (C) 2004 Universidade de Brasília * * This file is part of NeuralNetworkToolkit. * * NeuralNetworkToolkit is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * NeuralNetworkToolkit is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with NeuralNetworkToolkit; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 - USA. */package neuralnetworktoolkit.modelstorage;import java.io.*;import java.util.ResourceBundle;import neuralnetworktoolkit.*;/** * Implements methods to serialize models to files or byte arrays, * providing a model persistence mechanism. File serialization * provides immediate model storage, obviously. Byte array serialization * provides a way to store models in relational databases or network * transmission, for example. *  * @version $Revision: 1.4 $ - $Date: 2005/05/01 19:54:17 $ *  * @author <a href="mailto:hugoiver@yahoo.com.br">Hugo Iver V. Gonçalves</a> * @author <a href="mailto:rodbra@pop.com.br">Rodrigo C. M. Coimbra</a> */public class ModelSerializer {	public static final String[] modelExtensions = {"nn"};		private static final ResourceBundle resource = ResourceBundle			.getBundle("neuralnetworktoolkit.modelstorage.resources.ModelSerializerResource");	/**	 * Serializes a model to a file.	 * 	 * @param nn	 *            Model to be serialized.	 * @param fileName	 *            File name to store model.	 * 	 * @throws ModelSerializerException	 */	public static void serializeModelToFile(NeuralModel nn, String fileName)		throws ModelSerializerException {		try {			ObjectOutput out =				new ObjectOutputStream(					new FileOutputStream(fileName));			out.writeObject(nn);			out.close();		} catch (IOException e) {			throw new ModelSerializerException(				resource.getString("serializeFile") + e.getMessage(), e);		}	} //serializeModelToFile()	/**	 * Serializes a model to a byte array.	 * 	 * @param nn	 *            Model to be serialized.	 * 	 * @return Byte array model representation.	 */	public static byte[] serializeModelToByteArray(NeuralModel nn)		throws ModelSerializerException {		byte[] array = null;		try {			ByteArrayOutputStream byteArray = new ByteArrayOutputStream();			ObjectOutput out = new ObjectOutputStream(byteArray);			out.writeObject(nn);			out.close();			array = byteArray.toByteArray();		} catch (IOException e) {			throw new ModelSerializerException(				resource.getString("serializeArray") + e.getMessage(), e);		}		return array;	} //serializeModelToByteArray()	/**	 * Retrieves a model from a file.	 * 	 * @param fileName	 *            File name that stores model.	 * 	 * @return Model from file (<code>INeuralNetwork</code> object).	 */	public static NeuralModel retrieveSerializedModel(String fileName)		throws ModelSerializerException {		NeuralModel nn = null;		try {			File file = new File(fileName);			ObjectInputStream in =				new ObjectInputStream(					new FileInputStream(file));			nn = (NeuralModel) in.readObject();			in.close();		} catch (ClassNotFoundException e) {			throw new ModelSerializerException(				resource.getString("retrieveFile") + e.getMessage(), e);		} catch (IOException e) {			throw new ModelSerializerException(				resource.getString("retrieveFile") + e.getMessage(), e);		}		return nn;	} //retrieveSerializedModel()		/**	 * Retrieves a model from a file.	 * 	 * @param fileName File name that stores model.	 * 	 * @return Model from file (<code>INeuralNetwork</code> object).	 */	public static NeuralModel retrieveSerializedModel(File file)		throws ModelSerializerException {		NeuralModel nn = null;		try {			ObjectInputStream in =				new ObjectInputStream(					new FileInputStream(file));			nn = (NeuralModel) in.readObject();			in.close();		} catch (ClassNotFoundException e) {			throw new ModelSerializerException(				resource.getString("retrieveFile") + e.getMessage(), e);		} catch (IOException e) {			throw new ModelSerializerException(				resource.getString("retrieveFile") + e.getMessage(), e);		}		return nn;	} //retrieveSerializedModel()	/**	 * Retrieves a model from a byte array.	 * 	 * @param modelBytes	 *            Byte array that stores model.	 * 	 * @return Model from byte array (<code>NeuralModel</code> object).	 */	public static NeuralModel retrieveSerializedModel(byte[] modelBytes)		throws ModelSerializerException {		NeuralModel nn = null;		try {			ObjectInputStream in =				new ObjectInputStream(new ByteArrayInputStream(modelBytes));			nn = (NeuralModel) in.readObject();			in.close();		} catch (ClassNotFoundException e) {			throw new ModelSerializerException(				resource.getString("retrieveArray") + e.getMessage(), e);		} catch (IOException e) {			throw new ModelSerializerException(				resource.getString("retrieveArray") + e.getMessage(), e);		}		return nn;	} //retrieveSerializedModel()} //ModelSerializer

⌨️ 快捷键说明

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