Package ai.djl.nn
Class Parameter
- java.lang.Object
-
- ai.djl.nn.Parameter
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class Parameter extends java.lang.Object implements java.lang.AutoCloseableParameteris a container class that holds a learnable parameter of a model.Every
Parameteris associated with aBlock. The output of the block's forward function depends on the values in theParameter. During training, the values in theParameterare updated to reflect the training data. This process forms the crux of learning.- See Also:
- The D2L chapter on parameter management
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classParameter.BuilderA Builder to construct aParameter.static classParameter.TypeEnumerates the types ofParameter.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Parameter.Builderbuilder()Creates a builder to build aParameter.voidclose()voidfreeze(boolean freeze)Freezes or unfreezes the parameter for training.NDArraygetArray()Gets the values of thisParameteras anNDArray.java.lang.StringgetId()Gets the ID of thisParameter.InitializergetInitializer()Returns theInitializerfor thisParameter, if not already set.java.lang.StringgetName()Gets the name of thisParameter.ShapegetShape()Gets the shape of thisParameter.Parameter.TypegetType()Gets the type of thisParameter.voidinitialize(NDManager manager, DataType dataType)booleanisInitialized()Checks if thisParameteris initialized.voidload(NDManager manager, java.io.DataInputStream dis)Loads parameter NDArrays from InputStream.booleanrequiresGradient()Returns whether this parameter needs gradients to be computed.voidsave(java.io.DataOutputStream dos)Writes the parameter NDArrays to the given output stream.voidsetArray(NDArray array)Sets the values of thisParameter.voidsetInitializer(Initializer initializer)Sets theInitializerfor thisParameter, if not already set.voidsetShape(Shape shape)Sets the shape of thisParameter.
-
-
-
Method Detail
-
getId
public java.lang.String getId()
Gets the ID of thisParameter.- Returns:
- the ID of this
Parameter
-
getName
public java.lang.String getName()
Gets the name of thisParameter.- Returns:
- the name of this
Parameter
-
getType
public Parameter.Type getType()
Gets the type of thisParameter.- Returns:
- the type of this
Parameter
-
setArray
public void setArray(NDArray array)
Sets the values of thisParameter.- Parameters:
array- theNDArraythat contains values of thisParameter
-
setShape
public void setShape(Shape shape)
Sets the shape of thisParameter.- Parameters:
shape- the shape of thisParameter
-
getShape
public Shape getShape()
Gets the shape of thisParameter.- Returns:
- the shape of this
Parameter
-
getArray
public NDArray getArray()
Gets the values of thisParameteras anNDArray.- Returns:
- an
NDArraythat 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 thisParameteris initialized.- Returns:
trueif thisParameteris initialized
-
setInitializer
public void setInitializer(Initializer initializer)
Sets theInitializerfor thisParameter, if not already set. If overwrite flag is true, sets the initializer regardless.- Parameters:
initializer- the initializer to be set
-
getInitializer
public Initializer getInitializer()
Returns theInitializerfor thisParameter, if not already set. If overwrite flag is true, sets the initializer regardless.- Returns:
- the initializer of this
Parameter
-
initialize
public void initialize(NDManager manager, DataType dataType)
Initializes the parameter with the givenNDManager, with givenDataTypefor the given expected input shapes.- Parameters:
manager- an NDManager to create the arraysdataType- the datatype of theParameter
-
save
public void save(java.io.DataOutputStream dos) throws java.io.IOExceptionWrites the parameter NDArrays to the given output stream.- Parameters:
dos- the output stream to write to- Throws:
java.io.IOException- if the write operation fails
-
load
public void load(NDManager manager, java.io.DataInputStream dis) throws java.io.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:
java.io.IOException- if failed to readMalformedModelException- Exception thrown when model is not in expected format (parameters).
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable
-
builder
public static Parameter.Builder builder()
Creates a builder to build aParameter.The methods start with
setare required fields, andoptfor optional fields.- Returns:
- a new builder
-
-