gpt4 book ai didi

java - 使用 deeplearning4j 训练简单的神经网络

转载 作者:搜寻专家 更新时间:2023-11-01 02:05:58 26 4
gpt4 key购买 nike

我开始使用 deeplearning4j,我正在尝试创建一个简单的神经网络。

我想逼近函数 sin(x)/x 。这在理论上应该可以通过单个隐藏层实现。

首先,我创建了一个模拟数据集 (x,y),然后我尝试使用具有 20 个隐藏节点和 sigmoid 激活函数的神经网络来逼近该函数。不幸的是,使用 NN y_est 估计的值甚至不接近实际值 y

我想知道错误在哪里。

这是我当前的代码:

package org.deeplearning4j.examples.myexamples

import org.deeplearning4j.nn.api.OptimizationAlgorithm
import org.deeplearning4j.nn.conf.{ MultiLayerConfiguration, NeuralNetConfiguration }
import org.deeplearning4j.nn.conf.layers.OutputLayer
import org.deeplearning4j.nn.conf.layers.DenseLayer
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork
import org.deeplearning4j.nn.weights.WeightInit
import org.deeplearning4j.optimize.listeners.ScoreIterationListener
import org.nd4j.linalg.api.ops.impl.transforms.Sin
import org.nd4j.linalg.dataset.DataSet
import org.nd4j.linalg.factory.Nd4j
import org.nd4j.linalg.lossfunctions.LossFunctions.LossFunction

import org.nd4j.linalg.api.ndarray.INDArray
import scalax.chart.api._

import breeze.linalg.linspace

/**
* Created by donbeo on 16/10/15.
*/
package object MyExample1 {
def main(args: Array[String]) = {

def plotXY(x:INDArray, y:INDArray):Unit = {

val dataPlot = for(i <- 0 to y.length()-1) yield (x.getFloat(i), y.getFloat(i))
val chart = XYLineChart(dataPlot)
chart.show()
}


val nSamples = 500
val xMin = -4
val xMax = 4

val x0 = linspace(xMin, xMax, nSamples)
val y0 = breeze.numerics.sin(x0) / x0

val x = Nd4j.create(x0.toArray).reshape(nSamples, 1)
val y = Nd4j.create(y0.toArray).reshape(nSamples, 1)

plotXY(x, y)


val numInputs = 1
val numOutputs = 1
val numHiddenNodes = 20

val seed = 123
val iterations = 100


val conf = new NeuralNetConfiguration.Builder()
.seed(seed)
.iterations(iterations)
.optimizationAlgo(OptimizationAlgorithm.LBFGS)
.list(2)
.layer(0, new DenseLayer.Builder().nIn(numInputs).nOut(numHiddenNodes)
.activation("sigmoid")
.weightInit(WeightInit.XAVIER)
.build())
.layer(1, new OutputLayer.Builder(LossFunction.MSE)
.weightInit(WeightInit.XAVIER)
.activation("identity")
.nIn(numHiddenNodes).nOut(numOutputs).build())
.build()

val dataSet = new DataSet(x, y)
val network: MultiLayerNetwork = new MultiLayerNetwork(conf)
network.init()
network.setListeners(new ScoreIterationListener(1))
network.fit(dataSet)

val y_est = network.output(x)

plotXY(x, y_est)

}
}

最佳答案

这是一个基本配置。我只玩了几分钟,但这应该能让你有个好的开始。

package org.deeplearning4j.examples.deepbelief;

import org.deeplearning4j.nn.api.OptimizationAlgorithm;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.Updater;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
import org.deeplearning4j.nn.conf.layers.OutputLayer;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.nn.weights.WeightInit;
import org.deeplearning4j.optimize.listeners.ScoreIterationListener;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.api.ops.impl.transforms.Sin;
import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.lossfunctions.LossFunctions;

/**
* Created by agibsonccc on 10/17/15.
*/
public class RandomValues {
public static void main(String[] args) {
Nd4j.ENFORCE_NUMERICAL_STABILITY = true;
int numInputs = 1;
int numOutputs = 1;
int numHiddenNodes = 20;
int nSamples = 500;
INDArray x0 = Nd4j.linspace(-10, 10, 500).reshape(nSamples,1);
INDArray y0 = Nd4j.getExecutioner().execAndReturn(new Sin(x0, x0.dup())).div(x0);
System.out.println(y0);

int seed = 123;
int iterations = 100;
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.seed(seed).constrainGradientToUnitNorm(true).learningRate(1e-1)
.iterations(iterations).constrainGradientToUnitNorm(true).l1(1e-1)
.l2(1e-3).regularization(true).miniBatch(false)
.optimizationAlgo(OptimizationAlgorithm.LINE_GRADIENT_DESCENT)
.list(2)
.layer(0, new DenseLayer.Builder().nIn(numInputs).nOut(numHiddenNodes)
.activation("relu")
.weightInit(WeightInit.XAVIER)
.build())
.layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.RMSE_XENT)
.weightInit(WeightInit.XAVIER).updater(Updater.SGD)
.activation("identity").weightInit(WeightInit.XAVIER)
.nIn(numHiddenNodes).nOut(numOutputs).build()).backprop(true)
.build();

MultiLayerNetwork network = new MultiLayerNetwork(conf);
network.init();
network.setListeners(new ScoreIterationListener(1));
network.fit(new DataSet(x0, y0));
System.out.println(network.output(x0));

}


}

关于java - 使用 deeplearning4j 训练简单的神经网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33189315/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com