Skip to content
Permalink
Browse files

Fix AutoEncoder WS issue; add test (#5663)

  • Loading branch information...
AlexDBlack committed Jun 21, 2018
1 parent ee5edf3 commit 291288855e744b33e4ba8964f39afb26f52e86c0
@@ -0,0 +1,59 @@
package org.deeplearning4j.nn.layers;

import org.deeplearning4j.BaseDL4JTest;
import org.deeplearning4j.datasets.iterator.impl.SingletonMultiDataSetIterator;
import org.deeplearning4j.nn.conf.ComputationGraphConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.graph.MergeVertex;
import org.deeplearning4j.nn.conf.inputs.InputType;
import org.deeplearning4j.nn.conf.layers.AutoEncoder;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
import org.deeplearning4j.nn.conf.layers.OutputLayer;
import org.deeplearning4j.nn.graph.ComputationGraph;
import org.deeplearning4j.nn.weights.WeightInit;
import org.junit.Test;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.api.MultiDataSet;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.lossfunctions.LossFunctions;

public class AutoEncoderTest extends BaseDL4JTest {

@Test
public void sanityCheckIssue5662(){
int mergeSize = 50;
int encdecSize = 25;
int in1Size = 20;
int in2Size = 15;
int hiddenSize = 10;

ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder()
.weightInit(WeightInit.XAVIER)
.graphBuilder()
.addInputs("in1", "in2")
.addLayer("1", new DenseLayer.Builder().nOut(mergeSize).build(), "in1")
.addLayer("2", new DenseLayer.Builder().nOut(mergeSize).build(), "in2")
.addVertex("merge", new MergeVertex(), "1", "2")
.addLayer("e",new AutoEncoder.Builder().nOut(encdecSize).corruptionLevel(0.2).build(),"merge")
.addLayer("hidden",new AutoEncoder.Builder().nOut(hiddenSize).build(),"e")
.addLayer("decoder",new AutoEncoder.Builder().nOut(encdecSize).corruptionLevel(0.2).build(),"hidden")
.addLayer("L4", new DenseLayer.Builder().nOut(mergeSize).build(), "decoder")
.addLayer("out1", new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MSE).nOut(in1Size).build(),"L4")
.addLayer("out2",new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MSE).nOut(in2Size).build(),"L4")
.setOutputs("out1","out2")
.setInputTypes(InputType.feedForward(in1Size), InputType.feedForward(in2Size))
.pretrain(true).backprop(true)
.build();

ComputationGraph net = new ComputationGraph(conf);
net.init();

MultiDataSet mds = new org.nd4j.linalg.dataset.MultiDataSet(
new INDArray[]{Nd4j.create(1, in1Size), Nd4j.create(1, in2Size)},
new INDArray[]{Nd4j.create(1, in1Size), Nd4j.create(1, in2Size)});

net.summary(InputType.feedForward(in1Size), InputType.feedForward(in2Size));
net.fit(new SingletonMultiDataSetIterator(mds));
}

}
@@ -64,15 +64,15 @@ public INDArray encode(INDArray v, boolean training, LayerWorkspaceMgr workspace
INDArray ret = workspaceMgr.createUninitialized(ArrayType.ACTIVATIONS, v.size(0), W.size(1));
INDArray preAct = v.mmuli(W, ret).addiRowVector(hBias);
ret = layerConf().getActivationFn().getActivation(preAct, training);
return ret;
return workspaceMgr.leverageTo(ArrayType.ACTIVATIONS, ret);
}

// Decode
public INDArray decode(INDArray y, LayerWorkspaceMgr workspaceMgr) {
INDArray W = getParamWithNoise(PretrainParamInitializer.WEIGHT_KEY, true, workspaceMgr);
INDArray vBias = getParamWithNoise(PretrainParamInitializer.VISIBLE_BIAS_KEY, true, workspaceMgr);
INDArray preAct = y.mmul(W.transposei()).addiRowVector(vBias);
return layerConf().getActivationFn().getActivation(preAct, true);
return workspaceMgr.leverageTo(ArrayType.ACTIVATIONS, layerConf().getActivationFn().getActivation(preAct, true));

}

@@ -89,7 +89,7 @@ public boolean isPretrainLayer() {

@Override
public INDArray activate(boolean training, LayerWorkspaceMgr workspaceMgr) {
return decode(encode(input, training, workspaceMgr), workspaceMgr);
return encode(input, training, workspaceMgr);
}

@Override

0 comments on commit 2912888

Please sign in to comment.
You can’t perform that action at this time.