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.
| Constructor and Description |
|---|
Parameter(java.lang.String name,
Block block,
ParameterType type)
Creates a
Parameter with the given name, and parameter type, and associated with the
given Block. |
Parameter(java.lang.String name,
Block block,
ParameterType type,
boolean requiresGrad)
Creates a
Parameter with the given name, and parameter type, and associated with the
given Block. |
Parameter(java.lang.String name,
Block block,
ParameterType type,
boolean requireGrad,
SparseFormat gradientFormat)
Creates a
Parameter with the given name, and parameter type, and associated with the
given Block. |
| Modifier and Type | Method and Description |
|---|---|
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. |
ParameterType |
getType()
Gets the type of this
Parameter. |
void |
initialize(NDManager manager,
DataType dataType,
Shape[] inputShapes)
|
boolean |
isInitialized()
Checks if this
Parameter is initialized. |
void |
load(NDManager manager,
java.io.DataInputStream dis)
Loads parameter NDArrays from InputStream.
|
boolean |
requireGradient()
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,
boolean overwrite)
Sets the
Initializer for this Parameter, if not already set. |
void |
setMandatoryDataType(DataType mandatoryDataType)
Sets the mandatory data type for this
Parameter. |
public Parameter(java.lang.String name,
Block block,
ParameterType type)
Parameter with the given name, and parameter type, and associated with the
given Block.name - the name of the Parameterblock - the block with which this Parameter is associatedtype - the type of this Parameterpublic Parameter(java.lang.String name,
Block block,
ParameterType type,
boolean requiresGrad)
Parameter with the given name, and parameter type, and associated with the
given Block.name - the name of the Parameterblock - the block with which this Parameter is associatedtype - the type of this ParameterrequiresGrad - whether this Parameter needs to compute gradientspublic Parameter(java.lang.String name,
Block block,
ParameterType type,
boolean requireGrad,
SparseFormat gradientFormat)
Parameter with the given name, and parameter type, and associated with the
given Block.name - the name of the Parameterblock - the block with which this Parameter is associatedtype - the type of this ParameterrequireGrad - whether this Parameter needs to compute gradientsgradientFormat - the SparseFormat of the gradient arraypublic java.lang.String getId()
Parameter.Parameterpublic java.lang.String getName()
Parameter.Parameterpublic ParameterType getType()
Parameter.Parameterpublic void setArray(NDArray array)
Parameter.array - the NDArray that contains values of this Parameterpublic NDArray getArray()
Parameter as an NDArray.NDArray that contains values of this Parameterpublic boolean requireGradient()
public void setMandatoryDataType(DataType mandatoryDataType)
Parameter.mandatoryDataType - the mandatory data type for this Parameterpublic boolean isInitialized()
Parameter is initialized.true if this Parameter is initializedpublic void setInitializer(Initializer initializer, boolean overwrite)
Initializer for this Parameter, if not already set. If overwrite
flag is true, sets the initializer regardless.initializer - the initializer to be setoverwrite - if true, set the initializer regardless of whether its already set or notpublic void initialize(NDManager manager, DataType dataType, Shape[] inputShapes)
NDManager, with given DataType for
the given expected input shapes.manager - an NDManager to create the arraysdataType - the datatype of the ParameterinputShapes - the expected input shapespublic 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.AutoCloseable