Package opennlp.tools.depparse
Class FeedforwardNetwork.Serializer
java.lang.Object
opennlp.tools.depparse.FeedforwardNetwork.Serializer
- All Implemented Interfaces:
ArtifactSerializer<FeedforwardNetwork>
- Enclosing class:
FeedforwardNetwork
static final class FeedforwardNetwork.Serializer
extends Object
implements ArtifactSerializer<FeedforwardNetwork>
Reads and writes a network as a
FeedforwardDependencyModel artifact: a
four-byte magic, an int format version, the three vocabularies, the
transition inventory, the embedding size, and the weight matrices and vectors.
Symbols are stored as an int byte length followed by UTF-8 bytes; a symbol
may have up to ResourceLimits.MAX_ENTRIES bytes.
Counts read from the stream are bounded by ResourceLimits.MAX_ENTRIES,
and the float values of all matrices and vectors together by
ResourceLimits.MAX_MATRIX_CELLS; matrix rows are allocated one at a time
while reading.
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final intThe format version this serializer reads and writes.(package private) static final byte[]The bytes every serialized network starts with. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreate(InputStream in) Creates an artifact from the providedInputStream.private static FeedforwardNetworkread(DataInputStream data) Reads the network body after any wrapping has been set up.private static intreadCount(DataInputStream data, String label) Reads a count and checks it againstResourceLimits.MAX_ENTRIES.private static floatreadFiniteFloat(DataInputStream data, String label) Reads one weight.private static float[][]readMatrix(DataInputStream data, long expectedRows, long expectedColumns, long[] remainingCells, String label) Reads a matrix written bywriteMatrix(java.io.DataOutputStream, float[][]), allocating one row at a time.private static StringreadSymbol(DataInputStream data, String label) Reads a symbol written bywriteSymbol(java.io.DataOutputStream, java.lang.String).private static float[]readVector(DataInputStream data, long expectedLength, long[] remainingCells, String label) Reads a vector written bywriteVector(java.io.DataOutputStream, float[]).readVocabulary(DataInputStream data, String label, long[] remainingEntries) Reads one vocabulary written bywriteVocabulary(java.io.DataOutputStream, java.util.Map<java.lang.String, java.lang.Integer>).private static voidreserveCells(long[] remaining, long count, String label) Reserves float cells before allocating a matrix or vector.voidserialize(FeedforwardNetwork artifact, OutputStream out) Serializes an artifact to the providedOutputStream.private static voidwriteMatrix(DataOutputStream data, float[][] matrix) Writes a rectangular matrix as its dimensions followed by its values in row order.private static voidwriteSymbol(DataOutputStream data, String symbol) Writes a symbol as its UTF-8 byte length followed by the bytes.private static voidwriteVector(DataOutputStream data, float[] vector) Writes a vector as its length followed by its values.private static voidwriteVocabulary(DataOutputStream data, Map<String, Integer> ids) Writes one vocabulary as its size followed by (symbol, id) pairs in ascending id order, so that serializing the same network yields the same bytes on every JVM.
-
Field Details
-
MAGIC
static final byte[] MAGICThe bytes every serialized network starts with. -
FORMAT_VERSION
static final int FORMAT_VERSIONThe format version this serializer reads and writes.- See Also:
-
-
Constructor Details
-
Serializer
Serializer()
-
-
Method Details
-
create
Creates an artifact from the providedInputStream.Note: The
InputStreamremains open.- Specified by:
createin interfaceArtifactSerializer<FeedforwardNetwork>- Parameters:
in- A valid, openInputStreamready to read from.- Returns:
- A valid
artifact. - Throws:
InvalidFormatException- Thrown if the content is not a network in a supported version, a count or symbol length is out of range, the embedding size or hidden layer width is zero, the structure is inconsistent, a value is not finite, or the stream ends early. Malformed UTF-8 in a symbol is replaced, not rejected.IOException- Thrown if IO errors occurred during creation.
-
read
Reads the network body after any wrapping has been set up.- Parameters:
data- The input to read from.- Returns:
- The network. Never
null. - Throws:
IOException- Thrown if reading fails or the content is invalid.
-
serialize
Serializes an artifact to the providedOutputStream.Note: The
OutputStreamremains open.- Specified by:
serializein interfaceArtifactSerializer<FeedforwardNetwork>- Parameters:
artifact- A validartifact.out- A valid, openOutputStreamready to write to.- Throws:
IllegalArgumentException- Thrown ifartifactoroutisnull.IOException- Thrown if IO errors occurred during serialization.
-
writeVocabulary
private static void writeVocabulary(DataOutputStream data, Map<String, Integer> ids) throws IOExceptionWrites one vocabulary as its size followed by (symbol, id) pairs in ascending id order, so that serializing the same network yields the same bytes on every JVM.- Parameters:
data- The output to write to.ids- The vocabulary to write.- Throws:
IOException- Thrown if writing fails.
-
readVocabulary
private static Map<String,Integer> readVocabulary(DataInputStream data, String label, long[] remainingEntries) throws IOException Reads one vocabulary written bywriteVocabulary(java.io.DataOutputStream, java.util.Map<java.lang.String, java.lang.Integer>). The map grows while reading instead of being sized from the header.- Parameters:
data- The input to read from.label- The vocabulary name used in error messages.remainingEntries- The one-element budget of vocabulary entries across all vocabularies, decremented in place.- Returns:
- The symbol to id map. Never
null. - Throws:
IOException- Thrown if reading fails, the size exceeds the budget, or a symbol repeats.
-
writeSymbol
Writes a symbol as its UTF-8 byte length followed by the bytes.- Parameters:
data- The output to write to.symbol- The symbol to write.- Throws:
IOException- Thrown if writing fails.
-
readSymbol
Reads a symbol written bywriteSymbol(java.io.DataOutputStream, java.lang.String).- Parameters:
data- The input to read from.label- The symbol name used in error messages.- Returns:
- The symbol. Never
null. - Throws:
IOException- Thrown if reading fails or the length is out of range.
-
writeMatrix
Writes a rectangular matrix as its dimensions followed by its values in row order.- Parameters:
data- The output to write to.matrix- The matrix to write.- Throws:
IOException- Thrown if writing fails.
-
readMatrix
private static float[][] readMatrix(DataInputStream data, long expectedRows, long expectedColumns, long[] remainingCells, String label) throws IOException Reads a matrix written bywriteMatrix(java.io.DataOutputStream, float[][]), allocating one row at a time.- Parameters:
data- The input to read from.expectedRows- The required row count, or a negative value to accept any positive count within the limits.expectedColumns- The required column count.remainingCells- The one-element float budget, decremented in place.label- The matrix name used in error messages.- Returns:
- The matrix. Never
null. - Throws:
IOException- Thrown if reading fails, a dimension is out of range or does not match, the budget is exceeded, or a value is not finite.
-
writeVector
Writes a vector as its length followed by its values.- Parameters:
data- The output to write to.vector- The vector to write.- Throws:
IOException- Thrown if writing fails.
-
readVector
private static float[] readVector(DataInputStream data, long expectedLength, long[] remainingCells, String label) throws IOException Reads a vector written bywriteVector(java.io.DataOutputStream, float[]).- Parameters:
data- The input to read from.expectedLength- The required length.remainingCells- The one-element float budget, decremented in place.label- The vector name used in error messages.- Returns:
- The vector. Never
null. - Throws:
IOException- Thrown if reading fails, the length does not match, the budget is exceeded, or a value is not finite.
-
readCount
Reads a count and checks it againstResourceLimits.MAX_ENTRIES.- Parameters:
data- The input to read from.label- The count name used in error messages.- Returns:
- The count, never negative.
- Throws:
IOException- Thrown if reading fails or the value is out of range.
-
reserveCells
Reserves float cells before allocating a matrix or vector.- Parameters:
remaining- The one-element budget, decremented in place.count- The number of values about to be allocated.label- The structure name used in error messages.- Throws:
IOException- Thrown ifcountexceeds the remaining budget.
-
readFiniteFloat
Reads one weight.- Parameters:
data- The input to read from.label- The structure name used in error messages.- Returns:
- The weight.
- Throws:
IOException- Thrown if reading fails or the value is NaN or infinite.
-