/** * Save a file to the directory that this object refers to. * @param name The name to load. * @param obj The object to save. */ public final void saveToDirectory(final String name, final Object obj) { final File path = new File(this.parent, name); EncogDirectoryPersistence.saveObject(path, obj); }
/** * Load a file from the directory that this object refers to. * @param name The name to load. * @return The object. */ public final Object loadFromDirectory(final String name) { final File path = new File(this.parent, name); return EncogDirectoryPersistence.loadObject(path); }
/** * Load an object from an input stream. * @param is The input stream to read from. * @return The loaded object. */ public static Object loadObject(final InputStream is) { final String header = EncogDirectoryPersistence.readLine(is); final String[] params = header.split(","); if (!"encog".equals(params[0])) { throw new PersistError("Not a valid EG file."); } final String name = params[1]; final EncogPersistor p = PersistorRegistry.getInstance().getPersistor( name); if (p == null) { throw new PersistError("Do not know how to read the object: " + name); } if (p.getFileVersion() < Integer.parseInt(params[4])) { throw new PersistError( "The file you are trying to read is from a later version of Encog. Please upgrade Encog to read this file."); } return p.read(is); }
private void saveLR() throws IOException { File folder = new File("./models"); if(!folder.exists()) { FileUtils.forceMkdir(folder); } EncogDirectoryPersistence.saveObject(new File("./models/model" + this.trainerID + ".lr"), classifier); }
/** * Load an EG object as a reousrce. * @param res The resource name. * @return The loaded object. */ public static Object loadResourceObject(final String res) { InputStream is = null; try { is = ResourceInputStream.openResourceInputStream(res); return loadObject(is); } finally { try { if( is!=null ) { is.close(); } } catch(IOException ex) { } } }
private void saveModel() { File folder = new File("./models"); if(!folder.exists()) { try { FileUtils.forceMkdir(folder); } catch (IOException e) { log.error("Failed to create directory: {}", folder.getAbsolutePath()); e.printStackTrace(); } } EncogDirectoryPersistence.saveObject(new File("./models/model" + this.trainerID + ".svm"), svm); }
/** * Load the specified object. * @param file The file to load. * @return The loaded object. */ public static Object loadObject(final File file) { FileInputStream fis = null; try { fis = new FileInputStream(file); final Object result = EncogDirectoryPersistence.loadObject(fis); return result; } catch (final IOException ex) { throw new PersistError(ex); } finally { if (fis != null) { try { fis.close(); } catch (final IOException e) { EncogLogging.log(e); } } } }
private void saveTmpNN(int epoch) throws IOException { if(!toPersistentModel) { return; } File tmpFolder = new File(pathFinder.getTmpModelsPath(SourceType.LOCAL)); if(!tmpFolder.exists()) { FileUtils.forceMkdir(tmpFolder); } EncogDirectoryPersistence.saveObject(new File(tmpFolder, "model" + trainerID + "-" + epoch + ".nn"), network); }
public static void load(File f, CARunner runner) { try { Universe universe = (Universe)EncogDirectoryPersistence.loadObject( new File(FileUtil.forceExtension(f.toString(), "eg"))); CAProgram physics = (CAProgram)SerializeObject.load( new File(FileUtil.forceExtension(f.toString(), "bin"))); physics.setSourceUniverse(universe); runner.init(universe,physics); } catch (IOException ex) { throw new CellularAutomataError(ex); } catch (ClassNotFoundException ex) { throw new CellularAutomataError(ex); } } }
private void saveNN() throws IOException { if(!toPersistentModel) { return; } File folder = new File(pathFinder.getModelsPath(SourceType.LOCAL)); if(!folder.exists()) { FileUtils.forceMkdir(folder); } EncogDirectoryPersistence.saveObject(new File(folder, "model" + this.trainerID + ".nn"), network); }
/** * Obtain the ML method. * @return The method. */ public MLMethod obtainMethod() { final String resourceID = getProp().getPropertyString( ScriptProperties.ML_CONFIG_MACHINE_LEARNING_FILE); final File resourceFile = getScript().resolveFilename(resourceID); final MLMethod method = (MLMethod) EncogDirectoryPersistence .loadObject(resourceFile); if (!(method instanceof MLMethod)) { throw new AnalystError( "The object to be trained must be an instance of MLMethod. " + method.getClass().getSimpleName()); } return method; }
private void writeEncogModelToFileSystem(double[] weights, Path out) { double[] finalWeights = refineWeights(weights); FSDataOutputStream fos = null; try { fos = FileSystem.get(new Configuration()).create(out); LOG.info("Writing results to {}", out); this.network.getFlat().setWeights(finalWeights); if(out != null) { EncogDirectoryPersistence.saveObject(fos, this.network); } } catch (IOException e) { LOG.error("Error in writing output.", e); } finally { IOUtils.closeStream(fos); } }
return BasicML.class.cast(NNModel.loadFromStream(pair.getInput())); } else { return BasicML.class.cast(EncogDirectoryPersistence.loadObject(pair.getInput()));
/** * Save the specified object. * @param filename The filename to save to. * @param obj The Object to save. */ public static void saveObject(final File filename, final Object obj) { FileOutputStream fos = null; try { fos = new FileOutputStream(filename); EncogDirectoryPersistence.saveObject(fos, obj); } catch (final IOException ex) { throw new PersistError(ex); } finally { try { if (fos != null) { fos.close(); } } catch (final IOException e) { EncogLogging.log(e); } } }
.loadObject(methodFile);
public static void save(CARunner runner, File f) { try { EncogDirectoryPersistence.saveObject( new File(FileUtil.forceExtension(f.toString(), "eg")), runner.getUniverse()); SerializeObject.save( new File(FileUtil.forceExtension(f.toString(), "bin")), (Serializable)runner.getPhysics()); BasicCAVisualizer visualizer = new BasicCAVisualizer( runner.getUniverse()); Image img = visualizer.visualize(); ImageIO.write((RenderedImage) img, "png", new File(FileUtil.forceExtension(f.toString(), "png"))); } catch (IOException ex) { throw new CellularAutomataError(ex); } }
.loadObject(methodFile);
@Override public void postApplication(MasterContext<NNParams, NNParams> context) { LOG.info("NNOutput starts to write model to files."); int inputs = NumberFormatUtils.getInt(context.getProps().getProperty(NNConstants.GUAGUA_NN_INPUT_NODES), NNConstants.GUAGUA_NN_DEFAULT_INPUT_NODES); int hiddens = NumberFormatUtils.getInt(context.getProps().getProperty(NNConstants.GUAGUA_NN_HIDDEN_NODES), NNConstants.GUAGUA_NN_DEFAULT_HIDDEN_NODES); int outputs = NumberFormatUtils.getInt(context.getProps().getProperty(NNConstants.GUAGUA_NN_OUTPUT_NODES), NNConstants.GUAGUA_NN_DEFAULT_OUTPUT_NODES); BasicNetwork network = NNUtils.generateNetwork(inputs, hiddens, outputs); Path out = new Path(context.getProps().getProperty(NNConstants.GUAGUA_NN_OUTPUT)); FSDataOutputStream fos = null; try { fos = FileSystem.get(new Configuration()).create(out); LOG.info("Writing results to {}", out.toString()); network.getFlat().setWeights(context.getMasterResult().getWeights()); EncogDirectoryPersistence.saveObject(fos, network); } catch (IOException e) { LOG.error("Error in writing output.", e); } finally { IOUtils.closeStream(fos); } }
/** * GEnerate from a machine learning method. * * @param mainClass * The main class. * @param method * The filename of the method. * @return The newly created node. */ private EncogProgramNode generateForMethod( final EncogProgramNode mainClass, final File method) { if (this.embedData) { final MLEncodable encodable = (MLEncodable) EncogDirectoryPersistence .loadObject(method); final double[] weights = new double[encodable.encodedArrayLength()]; encodable.encodeToArray(weights); mainClass.createArray("WEIGHTS", weights); } return mainClass.createNetworkFunction("createNetwork", method); }
@Override public void postApplication(MasterContext<NNParams, NNParams> context) { LOG.info("NNOutput starts to write model to files."); int inputs = NumberFormatUtils.getInt(context.getProps().getProperty(NNConstants.GUAGUA_NN_INPUT_NODES), NNConstants.GUAGUA_NN_DEFAULT_INPUT_NODES); int hiddens = NumberFormatUtils.getInt(context.getProps().getProperty(NNConstants.GUAGUA_NN_HIDDEN_NODES), NNConstants.GUAGUA_NN_DEFAULT_HIDDEN_NODES); int outputs = NumberFormatUtils.getInt(context.getProps().getProperty(NNConstants.GUAGUA_NN_OUTPUT_NODES), NNConstants.GUAGUA_NN_DEFAULT_OUTPUT_NODES); BasicNetwork network = NNUtils.generateNetwork(inputs, hiddens, outputs); Path out = new Path(context.getProps().getProperty(NNConstants.GUAGUA_NN_OUTPUT)); FSDataOutputStream fos = null; try { fos = FileSystem.get(new Configuration()).create(out); LOG.info("Writing results to {}", out.toString()); network.getFlat().setWeights(context.getMasterResult().getWeights()); EncogDirectoryPersistence.saveObject(fos, network); } catch (IOException e) { LOG.error("Error in writing output.", e); } finally { IOUtils.closeStream(fos); } }