DL4J 0.9.1:MultiLayerNetwork无NeuralNetConfiguration构造方法的解决问询
You’re absolutely right—those older official examples are indeed outdated. DL4J’s API evolved to use MultiLayerConfiguration.Builder as the standard, streamlined way to construct multi-layer networks, replacing the old approach of passing a NeuralNetConfiguration directly to the MultiLayerNetwork constructor.
Your temporary solution works, but it can be simplified significantly. Instead of manually creating a list of NeuralNetConfiguration instances, you can use the MultiLayerConfiguration.Builder (via NeuralNetConfiguration.Builder().list()) to set global config parameters and add layers directly. Here’s the cleaned-up, idiomatic version aligned with the latest DL4J API:
import org.deeplearning4j.nn.conf.MultiLayerConfiguration import org.deeplearning4j.nn.conf.NeuralNetConfiguration import org.deeplearning4j.nn.conf.layers.{DenseLayer, OutputLayer} import org.deeplearning4j.nn.multilayer.MultiLayerNetwork import org.nd4j.linalg.activations.Activation import org.nd4j.linalg.learning.config.ConjugateGradient import org.nd4j.linalg.lossfunctions.LossFunctions import java.util.Random // Set your hyperparameters val seed = 12345L val inputSize = 10 // Adjust to your input feature count val hiddenLayerSize = 20 val numClasses = 3 // Adjust to your classification task // Define your network layers val hiddenLayer = new DenseLayer.Builder() .nIn(inputSize) .nOut(hiddenLayerSize) .activation(Activation.RELU) .build() val outputLayer = new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) .nIn(hiddenLayerSize) .nOut(numClasses) .activation(Activation.SOFTMAX) .build() // Build the multi-layer configuration val conf: MultiLayerConfiguration = new NeuralNetConfiguration.Builder() .seed(seed) .updater(new ConjugateGradient(1e-6f)) // Replaces deprecated optimizationAlgo .l1(1e-1) .l2(2e-4) .regularization(true) .useDropConnect(true) .list() // This initializes the multi-layer configuration builder .layer(0, hiddenLayer) .layer(1, outputLayer) .build() // Initialize the model val model: MultiLayerNetwork = new MultiLayerNetwork(conf) model.init() // Critical step to initialize weights/biases
Key improvements over your temporary solution:
- We use
NeuralNetConfiguration.Builder().list()to directly create a multi-layer configuration, eliminating the need to manually assemble a list ofNeuralNetConfigurationobjects. - The
optimizationAlgomethod is deprecated in recent DL4J versions—we replace it withupdater(new ConjugateGradient(...))to set the optimization algorithm properly. - Explicitly calling
model.init()is essential to initialize the network’s parameters before training or inference (easy to forget but mandatory).
Quick tips for your classifier:
- Ensure layer input/output sizes align (nIn of one layer must match nOut of the previous layer).
- For multi-class classification, stick with
SOFTMAXactivation in the output layer paired withNEGATIVELOGLIKELIHOODloss. For binary classification, useSIGMOIDactivation withXENT(cross-entropy) loss.
This approach is the recommended, up-to-date way to build multi-layer networks in DL4J now.
内容的提问来源于stack exchange,提问作者Pedro Alipio

