Class XavierInitializer
- java.lang.Object
-
- ai.djl.training.initializer.XavierInitializer
-
- All Implemented Interfaces:
Initializer
public class XavierInitializer extends java.lang.Object implements Initializer
XavierInitializeris anInitializerthat 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.XavierInitializeris specified by the type of random distribution(XavierInitializer.RandomType), the factor type(XavierInitializer.FactorType), and the magnitude of the scale. By default,XavierInitializer.RandomTypeisUNIFORMandXavierInitializer.FactorTypeisAVG. 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.RandomTypeisUNIFORMandXavierInitializer.FactorTypeisIN, then \(c = \sqrt{\frac{3.}{n_{in}}}\). Similarly whenXavierInitializer.FactorTypeisOUT, then \(c = \sqrt{\frac{3.}{n_{out}}}\).If
XavierInitializer.RandomTypeisGAUSSIANandXavierInitializer.FactorTypeisAVG, 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
XavierInitializeris 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 classXavierInitializer.FactorTypeEnum for different types of factor type.static classXavierInitializer.RandomTypeEnum 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 NDArrayinitialize(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:
initializein interfaceInitializer- Parameters:
manager- theNDManagerto create the newNDArrayinshape- theShapefor the new NDArraydataType- theDataTypefor the new NDArray- Returns:
- the
NDArrayinitialized with the manager and shape
-
-