我正在尝试使用 DeepLearning4J 0.7 训练受限玻尔兹曼机 (RBM),但没有成功。我发现的所有示例要么没有做任何有用的事情,要么不再适用于 DeepLearning4J 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));
}
但是即使模型没有针对单个 epoch 进行过训练,每个元素的距离也始终为 0.0,这是不可能的。
这是训练 RBM 的正确方法吗?如何使用单个 RBM 重建输入?