Class XavierInitializer
- java.lang.Object
-
- ai.djl.training.initializer.XavierInitializer
-
- All Implemented Interfaces:
Initializer
public class XavierInitializer extends java.lang.Object implements Initializer
XavierInitializer
is anInitializer
that performs "Xavier" initialization for parameters. This initializer is designed to keep the scale of gradients roughly the same in all layers. It was originally defined in the paper Understanding the difficulty of training deep feedforward neural networks.XavierInitializer
is specified by the type of random distribution(XavierInitializer.RandomType
), the factor type(XavierInitializer.FactorType
), and the magnitude of the scale. By default,XavierInitializer.RandomType
isUNIFORM
andXavierInitializer.FactorType
isAVG
. The initializer fills the weights with random numbers in the range of \([-c, c]\), where \(c = \sqrt{\frac{3.}{0.5 * (n_{in} + n_{out})}}\) where \(n_{in}\) is the number of neurons feeding into weights, and \(n_{out}\) is the number of neurons the result is fed to.If
XavierInitializer.RandomType
isUNIFORM
andXavierInitializer.FactorType
isIN
, then \(c = \sqrt{\frac{3.}{n_{in}}}\). Similarly whenXavierInitializer.FactorType
isOUT
, then \(c = \sqrt{\frac{3.}{n_{out}}}\).If
XavierInitializer.RandomType
isGAUSSIAN
andXavierInitializer.FactorType
isAVG
, the initializer fills the weights with numbers from normal distribution with a standard deviation of \(\sqrt{\frac{3.}{0.5 * (n_{in} + n_{out})}}\).Another common setting of the
XavierInitializer
is defined in the paper Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification. These settings better handle non-linearity when preserving the variance across layers in a neural network. It can be initialized withnew XavierInitializer(RandomType.GAUSSIAN, FactorType.IN, 2))
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
XavierInitializer.FactorType
Enum for different types of factor type.static class
XavierInitializer.RandomType
Enum for different types of random distributions.
-
Field Summary
-
Fields inherited from interface ai.djl.training.initializer.Initializer
ONES, ZEROS
-
-
Constructor Summary
Constructors Constructor Description XavierInitializer()
Creates a new instance ofXavierInitializer
.XavierInitializer(XavierInitializer.RandomType randomType, XavierInitializer.FactorType factorType, float magnitude)
Initializes a Xavier initializer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description NDArray
initialize(NDManager manager, Shape shape, DataType dataType)
Initializes a singleNDArray
.
-
-
-
Constructor Detail
-
XavierInitializer
public XavierInitializer(XavierInitializer.RandomType randomType, XavierInitializer.FactorType factorType, float magnitude)
Initializes a Xavier initializer.- Parameters:
randomType
- the random generator type, can be GAUSSIAN or UNIFORMfactorType
- the factor type, can be one of AVG, IN, or OUTmagnitude
- the scale of the random number
-
XavierInitializer
public XavierInitializer()
Creates a new instance ofXavierInitializer
.
-
-
Method Detail
-
initialize
public NDArray initialize(NDManager manager, Shape shape, DataType dataType)
Initializes a singleNDArray
.- Specified by:
initialize
in interfaceInitializer
- Parameters:
manager
- theNDManager
to create the newNDArray
inshape
- theShape
for the new NDArraydataType
- theDataType
for the new NDArray- Returns:
- the
NDArray
initialized with the manager and shape
-
-