use of org.deeplearning4j.nn.conf.distribution.UniformDistribution in project deeplearning4j by deeplearning4j.
the class GravesBidirectionalLSTMTest method testSerialization.
@Test
public void testSerialization() {
final MultiLayerConfiguration conf1 = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(5).learningRate(0.1).rmsDecay(0.95).regularization(true).l2(0.001).updater(Updater.ADAGRAD).seed(12345).list().pretrain(false).layer(0, new org.deeplearning4j.nn.conf.layers.GravesBidirectionalLSTM.Builder().activation(Activation.TANH).nIn(2).nOut(2).weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(-0.05, 0.05)).build()).layer(1, new org.deeplearning4j.nn.conf.layers.GravesBidirectionalLSTM.Builder().activation(Activation.TANH).nIn(2).nOut(2).weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(-0.05, 0.05)).build()).layer(2, new org.deeplearning4j.nn.conf.layers.RnnOutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT).nIn(2).nOut(2).activation(Activation.TANH).build()).backprop(true).build();
final String json1 = conf1.toJson();
final MultiLayerConfiguration conf2 = MultiLayerConfiguration.fromJson(json1);
final String json2 = conf1.toJson();
TestCase.assertEquals(json1, json2);
}
use of org.deeplearning4j.nn.conf.distribution.UniformDistribution in project deeplearning4j by deeplearning4j.
the class GravesLSTMTest method testGravesBackwardBasicHelper.
private static void testGravesBackwardBasicHelper(int nIn, int nOut, int lstmNHiddenUnits, int miniBatchSize, int timeSeriesLength) {
INDArray inputData = Nd4j.ones(miniBatchSize, nIn, timeSeriesLength);
NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().layer(new org.deeplearning4j.nn.conf.layers.GravesLSTM.Builder().nIn(nIn).nOut(lstmNHiddenUnits).weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(0, 1)).activation(Activation.TANH).build()).build();
int numParams = conf.getLayer().initializer().numParams(conf);
INDArray params = Nd4j.create(1, numParams);
GravesLSTM lstm = (GravesLSTM) conf.getLayer().instantiate(conf, null, 0, params, true);
lstm.setBackpropGradientsViewArray(Nd4j.create(1, conf.getLayer().initializer().numParams(conf)));
//Set input, do a forward pass:
lstm.activate(inputData);
assertNotNull(lstm.input());
INDArray epsilon = Nd4j.ones(miniBatchSize, lstmNHiddenUnits, timeSeriesLength);
Pair<Gradient, INDArray> out = lstm.backpropGradient(epsilon);
Gradient outGradient = out.getFirst();
INDArray nextEpsilon = out.getSecond();
INDArray biasGradient = outGradient.getGradientFor(GravesLSTMParamInitializer.BIAS_KEY);
INDArray inWeightGradient = outGradient.getGradientFor(GravesLSTMParamInitializer.INPUT_WEIGHT_KEY);
INDArray recurrentWeightGradient = outGradient.getGradientFor(GravesLSTMParamInitializer.RECURRENT_WEIGHT_KEY);
assertNotNull(biasGradient);
assertNotNull(inWeightGradient);
assertNotNull(recurrentWeightGradient);
assertArrayEquals(biasGradient.shape(), new int[] { 1, 4 * lstmNHiddenUnits });
assertArrayEquals(inWeightGradient.shape(), new int[] { nIn, 4 * lstmNHiddenUnits });
assertArrayEquals(recurrentWeightGradient.shape(), new int[] { lstmNHiddenUnits, 4 * lstmNHiddenUnits + 3 });
assertNotNull(nextEpsilon);
assertArrayEquals(nextEpsilon.shape(), new int[] { miniBatchSize, nIn, timeSeriesLength });
//Check update:
for (String s : outGradient.gradientForVariable().keySet()) {
lstm.update(outGradient.getGradientFor(s), s);
}
}
Aggregations