You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DL4J 0.9.1:MultiLayerNetwork无NeuralNetConfiguration构造方法的解决问询

Correct way to build a MultiLayerNetwork in latest DL4J versions

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 of NeuralNetConfiguration objects.
  • The optimizationAlgo method is deprecated in recent DL4J versions—we replace it with updater(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 SOFTMAX activation in the output layer paired with NEGATIVELOGLIKELIHOOD loss. For binary classification, use SIGMOID activation with XENT (cross-entropy) loss.

This approach is the recommended, up-to-date way to build multi-layer networks in DL4J now.

内容的提问来源于stack exchange,提问作者Pedro Alipio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:15:27