/** * @return The singleton instance. */ public static PersistorRegistry getInstance() { if (PersistorRegistry.instance == null) { PersistorRegistry.instance = new PersistorRegistry(); } return PersistorRegistry.instance; }
/** * Write a property as an activation function. * * @param name * The name of the property. * @param act * The activation function. */ public final void writeProperty(final String name, final ActivationFunction act) { writeProperty(name, makeActivationFunctionString(act)); }
/** * Add a list of string columns. * * @param cols * The columns to add. */ public final void addColumns(final List<String> cols) { for (final String str : cols) { addColumn(str); } }
/** * {@inheritDoc} */ @Override public void save(final OutputStream os, final Object obj) { final EncogWriteHelper out = new EncogWriteHelper(os); final SOM som = (SOM) obj; out.addSection("SOM"); out.addSubSection("PARAMS"); out.addProperties(som.getProperties()); out.addSubSection("NETWORK"); out.writeProperty(PersistConst.WEIGHTS, som.getWeights()); out.writeProperty(PersistConst.INPUT_COUNT, som.getInputCount()); out.writeProperty(PersistConst.OUTPUT_COUNT, som.getOutputCount()); out.flush(); }
/** * 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); }
/** * 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); } } }
/** * 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 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); } } } }
public void addColumn(ActivationFunction act) { addColumn(makeActivationFunctionString(act)); } }
/** * Parse an activation function from a string. * @param params The params. * @param name The name of the param to parse. * @return The parsed activation function. */ public static ActivationFunction parseActivationFunction( final Map<String, String> params, final String name) { String value = null; try { value = params.get(name); if (value == null) { throw new PersistError("Missing property: " + name); } return parseActivationFunction(value); } catch (final Exception ex) { throw new PersistError(ex); } }
/** * Add the specified properties. * * @param properties * The properties. */ public final void addProperties(final Map<String, String> properties) { for (final String key : properties.keySet()) { final String value = properties.get(key); this.writeProperty(key, value); } }
/** * 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); }
/** * Close the file. */ public final void close() { try { this.reader.close(); } catch (final IOException e) { throw new PersistError(e); } }
/** * Add a line. * * @param l * The line to add. */ public final void addLine(final String l) { if (this.line.length() > 0) { writeLine(); } this.out.println(l); }
/** * Add a persistor. * @param persistor The persistor to add. */ public void add(final EncogPersistor persistor) { this.map.put(persistor.getPersistClassString(), persistor); }
/** * Get a persistor. * @param clazz The class to get the persistor for. * @return Return the persistor. */ public EncogPersistor getPersistor(final Class<?> clazz) { return getPersistor(clazz.getSimpleName()); }
/** * {@inheritDoc} */ @Override public void save(final OutputStream os, final Object obj) { final EncogWriteHelper out = new EncogWriteHelper(os); final BAM bam = (BAM) obj; out.addSection("BAM"); out.addSubSection("PARAMS"); out.addProperties(bam.getProperties()); out.addSubSection("NETWORK"); out.writeProperty(PersistConst.PROPERTY_F1_COUNT, bam.getF1Count()); out.writeProperty(PersistConst.PROPERTY_F2_COUNT, bam.getF2Count()); out.writeProperty(PersistConst.PROPERTY_WEIGHTS_F1_F2, bam.getWeightsF1toF2()); out.writeProperty(PersistConst.PROPERTY_WEIGHTS_F2_F1, bam.getWeightsF2toF1()); out.flush(); }
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); }
/** * Write a property as an int array. * * @param name * The name of the property. * @param array * The array. */ public final void writeProperty(final String name, final int[] array) { final StringBuilder result = new StringBuilder(); NumberList.toListInt(CSVFormat.EG_FORMAT, result, array); writeProperty(name, result.toString()); }
/** * {@inheritDoc} */ @Override public void save(final OutputStream os, final Object obj) { final EncogWriteHelper out = new EncogWriteHelper(os); final CPN cpn = (CPN) obj; out.addSection("CPN"); out.addSubSection("PARAMS"); out.addProperties(cpn.getProperties()); out.addSubSection("NETWORK"); out.writeProperty(PersistConst.INPUT_COUNT, cpn.getInputCount()); out.writeProperty(PersistConst.INSTAR, cpn.getInstarCount()); out.writeProperty(PersistConst.OUTPUT_COUNT, cpn.getOutputCount()); out.writeProperty(PersistCPN.PROPERTY_inputToInstar, cpn.getWeightsInputToInstar()); out.writeProperty(PersistCPN.PROPERTY_instarToInput, cpn.getWeightsInstarToOutstar()); out.writeProperty(PersistCPN.PROPERTY_winnerCount, cpn.getWinnerCount()); out.flush(); }