Package ai.djl.nn
Class Parameter
java.lang.Object
ai.djl.nn.Parameter
- All Implemented Interfaces:
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.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
A Builder to construct aParameter
.static enum
Enumerates the types ofParameter
. -
Method Summary
Modifier and TypeMethodDescriptionstatic Parameter.Builder
builder()
Creates a builder to build aParameter
.void
close()
void
freeze
(boolean freeze) Freezes or unfreezes the parameter for training.getArray()
Gets the values of thisParameter
as anNDArray
.getId()
Gets the ID of thisParameter
.Returns theInitializer
for thisParameter
, if not already set.getName()
Gets the name of thisParameter
.getShape()
Gets the shape of thisParameter
.getType()
Gets the type of thisParameter
.void
initialize
(NDManager manager, DataType dataType) boolean
Checks if thisParameter
is initialized.void
load
(NDManager manager, DataInputStream dis) Loads parameter NDArrays from InputStream.boolean
Returns whether this parameter needs gradients to be computed.void
save
(DataOutputStream dos) Writes the parameter NDArrays to the given output stream.void
Sets the values of thisParameter
.void
setInitializer
(Initializer initializer) Sets theInitializer
for thisParameter
, if not already set.void
Sets the shape of thisParameter
.
-
Method Details
-
getId
Gets the ID of thisParameter
.- Returns:
- the ID of this
Parameter
-
getName
Gets the name of thisParameter
.- Returns:
- the name of this
Parameter
-
getType
Gets the type of thisParameter
.- Returns:
- the type of this
Parameter
-
setArray
Sets the values of thisParameter
.- Parameters:
array
- theNDArray
that contains values of thisParameter
-
setShape
Sets the shape of thisParameter
.- Parameters:
shape
- the shape of thisParameter
-
getShape
Gets the shape of thisParameter
.- Returns:
- the shape of this
Parameter
-
getArray
Gets the values of thisParameter
as anNDArray
.- Returns:
- an
NDArray
that contains values of thisParameter
-
requiresGradient
public boolean requiresGradient()Returns whether this parameter needs gradients to be computed.- Returns:
- whether this parameter needs gradients to be computed
-
freeze
public void freeze(boolean freeze) Freezes or unfreezes the parameter for training.Sometimes during training, especially during transfer learning, it is typical to train only part of the model. For this, the freeze can be used to prevent certain parts from being trained.
This modifies the
requiresGradient()
of the parameter.- Parameters:
freeze
- true if the parameter should be frozen (freeze == !requiresGradient()
)
-
isInitialized
public boolean isInitialized()Checks if thisParameter
is initialized.- Returns:
true
if thisParameter
is initialized
-
setInitializer
Sets theInitializer
for thisParameter
, if not already set. If overwrite flag is true, sets the initializer regardless.- Parameters:
initializer
- the initializer to be set
-
getInitializer
Returns theInitializer
for thisParameter
, if not already set. If overwrite flag is true, sets the initializer regardless.- Returns:
- the initializer of this
Parameter
-
initialize
Initializes the parameter with the givenNDManager
, with givenDataType
for the given expected input shapes.- Parameters:
manager
- an NDManager to create the arraysdataType
- the datatype of theParameter
-
save
Writes the parameter NDArrays to the given output stream.- Parameters:
dos
- the output stream to write to- Throws:
IOException
- if the write operation fails
-
load
public void load(NDManager manager, DataInputStream dis) throws IOException, MalformedModelException Loads parameter NDArrays from InputStream.Currently, we cannot deserialize into the exact subclass of NDArray. The SparseNDArray will be loaded as NDArray only.
- Parameters:
manager
- the NDManagerdis
- the InputStream- Throws:
IOException
- if failed to readMalformedModelException
- Exception thrown when model is not in expected format (parameters).
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-
builder
Creates a builder to build aParameter
.The methods start with
set
are required fields, andopt
for optional fields.- Returns:
- a new builder
-