public class Parameter
extends java.lang.Object
implements java.lang.AutoCloseable
Parameter is a container class that holds a learnable parameter of a model.
Every Parameter is associated with a Block. The output of the block's forward
function depends on the values in the Parameter. During training, the values in the
Parameter are updated to reflect the training data. This process forms the crux of
learning.
| Modifier and Type | Class and Description |
|---|---|
static class |
Parameter.Builder
A Builder to construct a
Parameter. |
static class |
Parameter.Type
Enumerates the types of
Parameter. |
| Modifier and Type | Method and Description |
|---|---|
static Parameter.Builder |
builder()
Creates a builder to build a
Parameter. |
void |
close() |
NDArray |
getArray()
Gets the values of this
Parameter as an NDArray. |
java.lang.String |
getId()
Gets the ID of this
Parameter. |
java.lang.String |
getName()
Gets the name of this
Parameter. |
Parameter.Type |
getType()
Gets the type of this
Parameter. |
void |
initialize(NDManager manager,
DataType dataType)
|
boolean |
isInitialized()
Checks if this
Parameter is initialized. |
void |
load(NDManager manager,
java.io.DataInputStream dis)
Loads parameter NDArrays from InputStream.
|
boolean |
requiresGradient()
Returns whether this parameter needs gradients to be computed.
|
void |
save(java.io.DataOutputStream dos)
Writes the parameter NDArrays to the given output stream.
|
void |
setArray(NDArray array)
Sets the values of this
Parameter. |
void |
setInitializer(Initializer initializer)
Sets the
Initializer for this Parameter, if not already set. |
void |
setShape(Shape shape)
Sets the shape of this
Parameter. |
public java.lang.String getId()
Parameter.Parameterpublic java.lang.String getName()
Parameter.Parameterpublic Parameter.Type getType()
Parameter.Parameterpublic void setArray(NDArray array)
Parameter.array - the NDArray that contains values of this Parameterpublic void setShape(Shape shape)
Parameter.shape - the shape of this Parameterpublic NDArray getArray()
Parameter as an NDArray.NDArray that contains values of this Parameterpublic boolean requiresGradient()
public boolean isInitialized()
Parameter is initialized.true if this Parameter is initializedpublic void setInitializer(Initializer initializer)
Initializer for this Parameter, if not already set. If overwrite
flag is true, sets the initializer regardless.initializer - the initializer to be setpublic void initialize(NDManager manager, DataType dataType)
NDManager, with given DataType for
the given expected input shapes.manager - an NDManager to create the arraysdataType - the datatype of the Parameterpublic void save(java.io.DataOutputStream dos)
throws java.io.IOException
dos - the output stream to write tojava.io.IOException - if the write operation failspublic void load(NDManager manager, java.io.DataInputStream dis) throws java.io.IOException, MalformedModelException
Currently, we cannot deserialize into the exact subclass of NDArray. The SparseNDArray will be loaded as NDArray only.
manager - the NDManagerdis - the InputStreamjava.io.IOException - if failed to readMalformedModelException - Exception thrown when model is not in expected format
(parameters).public void close()
close in interface java.lang.AutoCloseablepublic static Parameter.Builder builder()
Parameter.
The methods start with set are required fields, and opt for optional
fields.