java-如何使用DeepLearning4J训练RBM并重建输入?
作者:互联网
我正在尝试使用DeepLearning4J 0.7训练受限的玻尔兹曼机(RBM),但没有成功.我发现的所有示例都无法在Deepeeping4J 0.7上做任何有用的事情或无法正常工作.
我需要训练一个带有对比发散的RBM,然后计算重建误差.
这是我到目前为止的内容:
import org.nd4j.linalg.factory.Nd4j;
import org.deeplearning4j.datasets.fetchers.MnistDataFetcher;
import org.deeplearning4j.nn.conf.layers.RBM;
import org.deeplearning4j.nn.api.Layer;
import static org.deeplearning4j.nn.conf.layers.RBM.VisibleUnit;
import static org.deeplearning4j.nn.conf.layers.RBM.HiddenUnit;
import org.deeplearning4j.optimize.api.IterationListener;
import org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator;
import org.deeplearning4j.eval.Evaluation;
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.multilayer.MultiLayerNetwork;
import org.deeplearning4j.nn.weights.WeightInit;
import org.deeplearning4j.optimize.listeners.ScoreIterationListener;
import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.nd4j.linalg.lossfunctions.LossFunctions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.nd4j.linalg.api.ndarray.INDArray;
public class experiment3 {
private static final Logger log = LoggerFactory.getLogger(experiment3.class);
public static void main(String[] args) throws Exception {
DataSetIterator mnistTrain = new MnistDataSetIterator(100, 60000, true);
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.regularization(false)
.iterations(1)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.list()
.layer(0, new RBM.Builder()
.nIn(784).nOut(500)
.weightInit(WeightInit.XAVIER)
.lossFunction(LossFunctions.LossFunction.RECONSTRUCTION_CROSSENTROPY)
.updater(Updater.NESTEROVS)
.learningRate(0.1)
.momentum(0.9)
.k(1)
.build())
.pretrain(true).backprop(false)
.build();
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
model.setListeners(new ScoreIterationListener(600));
for(int i = 0; i < 50; i++) {
model.fit(mnistTrain);
}
}
}
它会在每个时期编译并打印一些分数,但是当分数应该减小时分数会增加,而且我还没有找到任何进行重建的方法.
我试图使用重构函数并计算距离:
while(mnistTrain.hasNext()){
DataSet next = mnistTrain.next();
INDArray in = next.getFeatureMatrix();
INDArray out = model.reconstruct(in, 1); // tried with 0 but arrayindexoutofbounds
log.info("distance(1):" + in.distance1(out));
}
但是即使没有为单个时期训练模型,每个元素的距离始终为0.0,这是不可能的.
这是RBM训练的正确方法吗?如何使用单个RBM重构输入?
解决方法:
我意识到这个问题很古老,但是最近的活动在我的流程中显示了出来.
我只想说我最近在DL4j中一直使用RBM,无论是单层还是多层.他们可能没有得到官方的支持,但是可以正常工作.我也偶然发现了重建.要测试模型,您应该使用输出,例如在FF网络中.在您的情况下,我认为它将是:
INDArray in = next.getFeatureMatrix();
INDArray out = model.output(in);
一些补充:
我正在使用0.9.1
标签:machine-learning,deep-learning,deeplearning4j,java,rbm 来源: https://codeday.me/bug/20191112/2023511.html