DL4J 线性回归 [英] DL4J linear regression
问题描述
我是神经网络的新手.我正在尝试使用 DL4j 实现和训练简单的神经网络.我的功能:
I am new in neural networks. I am trying to implement and train simple neural network with DL4j. My function:
y = x * 2 + 300
我的愿景
我的结果
参数:
public final int seed = 12345;
public final int iterations = 1;
public final int nEpochs = 1;
public final int batchSize = 1000;
public final double learningRate = 0.01;
public final Random rng = new Random(seed);
public final int numInputs = 2;
public final int numOutputs = 1;
public final double maxX = 100;//xmax = 100; ymax=500.
public final double scale = 500;//for scale out x and y.
网络配置:
public MultiLayerConfiguration createConf() {
return new NeuralNetConfiguration.Builder()
.seed(seed)
.iterations(iterations)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.learningRate(learningRate)
.weightInit(WeightInit.XAVIER)
.updater(new Nesterovs(0.9))
.list()
.layer(0, new OutputLayer.Builder(LossFunctions.LossFunction.MSE)
.activation(Activation.IDENTITY)
.nIn(numInputs).nOut(numOutputs).build())
.pretrain(false).backprop(true).build();
}
训练数据:
public DataSetIterator generateTrainingData() {
List<DataSet> list = new ArrayList<>();
for (int i = 0; i < batchSize; i++) {
double x = rng.nextDouble() * maxX * (rng.nextBoolean() ? 1 : -1);
double y = y(x);
list.add(
new DataSet(
Nd4j.create(new double[]{x / scale, 1}),
Nd4j.create(new double[]{y / scale})
)
);
}
return new ListDataSetIterator(list, batchSize);
}
测试:
public void test() {
final MultiLayerNetwork net = new MultiLayerNetwork(createConf());
net.init();
net.setListeners(new ScoreIterationListener(1));
for (int i = 0; i < nEpochs; i++) {
net.fit(generateTrainingData());
}
int idx = 0;
double x[] = new double[19];
double y[] = new double[19];
double p[] = new double[19];
for (double i = -90; i < 100; i += 10) {
x[idx] = i;
y[idx] = y(i);
p[idx] = scale * net.output(Nd4j.create(new double[]{i / scale, 1})).getDouble(0, 0);
idx++;
}
plot(x, y, p);
}
请告诉我我做错了什么,或者我的视力是否不正确...
Please tell me what i am doing wrong or if i have incorrect vision...
先谢谢你,问候,米纳斯
Thank you in advance, Regards, Minas
推荐答案
一些提示:
使用我们内置的标准化工具.不要自己做这个.我们的标准化工具也允许您标准化标签.
Use our built in normalization tools. Don't do this yourself. Our normalization tools allow you to normalize labels as well.
关闭 minibatch(在靠近顶部的神经网络配置上设置 minibatch(false))最终你仍然没有真正在做小批量学习"
Turn minibatch off (set minibatch(false) on the neural net config near the top) Ultimately you still aren't actually doing "minibatch learning"
此外,您每次都在重新生成数据集.没有必要这样做.只需创建一次并传入以适应.
Also, you're regenerating the dataset each time. There's no need to do that. Just create it once and pass it in to fit.
出于可视化目的,请使用我之前提到的恢复机制(在示例中,您可以选择任何规范化器中的 1 个,例如 MinMaxScalar、NormalizeStandardize 等)
For visualization purposes, use the restore mechanism I mentioned earlier (This is in the example, you can pick 1 of any of the normalizers like MinMaxScalar, NormalizeStandardize,.. etc)
你的迭代也是错误的.只需将该值保持为 1 并保持您的 for 循环.否则,您只是过度拟合并花费了比您需要的更多的训练时间.迭代"实际上是您希望在同一数据集上每次拟合调用运行的更新次数.无论如何,我们将在下一个版本中取消该选项.
Your iterations are also wrong. Just keep that value at 1 and keep your for loop. Otherwise you're just overfitting and spending way more of your training time then you need to. An "iteration" is actually the number of updates you want to run per fit call on the same dataset. Next release we are getting rid of that option anyways.
这篇关于DL4J 线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!