use of org.deeplearning4j.nn.conf.layers.ConvolutionLayer in project deeplearning4j by deeplearning4j.
the class ConvolutionLayerSetupTest method testLRN.
@Test
public void testLRN() throws Exception {
List<String> labels = new ArrayList<>(Arrays.asList("Zico", "Ziwang_Xu"));
String rootDir = new ClassPathResource("lfwtest").getFile().getAbsolutePath();
RecordReader reader = new ImageRecordReader(28, 28, 3);
reader.initialize(new FileSplit(new File(rootDir)));
DataSetIterator recordReader = new RecordReaderDataSetIterator(reader, 10, 1, labels.size());
labels.remove("lfwtest");
NeuralNetConfiguration.ListBuilder builder = (NeuralNetConfiguration.ListBuilder) incompleteLRN();
builder.setInputType(InputType.convolutional(28, 28, 3));
MultiLayerConfiguration conf = builder.build();
ConvolutionLayer layer2 = (ConvolutionLayer) conf.getConf(3).getLayer();
assertEquals(6, layer2.getNIn());
}
use of org.deeplearning4j.nn.conf.layers.ConvolutionLayer in project deeplearning4j by deeplearning4j.
the class ConvolutionLayerTest method testCNNBiasInit.
@Test
public void testCNNBiasInit() {
ConvolutionLayer cnn = new ConvolutionLayer.Builder().nIn(1).nOut(3).biasInit(1).build();
NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().layer(cnn).build();
int numParams = conf.getLayer().initializer().numParams(conf);
INDArray params = Nd4j.create(1, numParams);
Layer layer = conf.getLayer().instantiate(conf, null, 0, params, true);
assertEquals(1, layer.getParam("b").size(0));
}
use of org.deeplearning4j.nn.conf.layers.ConvolutionLayer in project deeplearning4j by deeplearning4j.
the class TestConvolutionModes method testGlobalLocalConfigCompGraph.
@Test
public void testGlobalLocalConfigCompGraph() {
for (ConvolutionMode cm : new ConvolutionMode[] { ConvolutionMode.Strict, ConvolutionMode.Truncate, ConvolutionMode.Same }) {
ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER).convolutionMode(cm).graphBuilder().addInputs("in").addLayer("0", new ConvolutionLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0).nIn(3).nOut(3).build(), "in").addLayer("1", new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Strict).kernelSize(3, 3).stride(3, 3).padding(0, 0).nIn(3).nOut(3).build(), "0").addLayer("2", new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Truncate).kernelSize(3, 3).stride(3, 3).padding(0, 0).nIn(3).nOut(3).build(), "1").addLayer("3", new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Same).kernelSize(3, 3).stride(3, 3).padding(0, 0).nIn(3).nOut(3).build(), "2").addLayer("4", new SubsamplingLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0).build(), "3").addLayer("5", new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Strict).kernelSize(3, 3).stride(3, 3).padding(0, 0).build(), "4").addLayer("6", new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Truncate).kernelSize(3, 3).stride(3, 3).padding(0, 0).build(), "5").addLayer("7", new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Same).kernelSize(3, 3).stride(3, 3).padding(0, 0).build(), "6").addLayer("8", new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT).nOut(3).build(), "7").setOutputs("8").build();
assertEquals(cm, ((ConvolutionLayer) ((LayerVertex) conf.getVertices().get("0")).getLayerConf().getLayer()).getConvolutionMode());
assertEquals(ConvolutionMode.Strict, ((ConvolutionLayer) ((LayerVertex) conf.getVertices().get("1")).getLayerConf().getLayer()).getConvolutionMode());
assertEquals(ConvolutionMode.Truncate, ((ConvolutionLayer) ((LayerVertex) conf.getVertices().get("2")).getLayerConf().getLayer()).getConvolutionMode());
assertEquals(ConvolutionMode.Same, ((ConvolutionLayer) ((LayerVertex) conf.getVertices().get("3")).getLayerConf().getLayer()).getConvolutionMode());
assertEquals(cm, ((SubsamplingLayer) ((LayerVertex) conf.getVertices().get("4")).getLayerConf().getLayer()).getConvolutionMode());
assertEquals(ConvolutionMode.Strict, ((SubsamplingLayer) ((LayerVertex) conf.getVertices().get("5")).getLayerConf().getLayer()).getConvolutionMode());
assertEquals(ConvolutionMode.Truncate, ((SubsamplingLayer) ((LayerVertex) conf.getVertices().get("6")).getLayerConf().getLayer()).getConvolutionMode());
assertEquals(ConvolutionMode.Same, ((SubsamplingLayer) ((LayerVertex) conf.getVertices().get("7")).getLayerConf().getLayer()).getConvolutionMode());
}
}
use of org.deeplearning4j.nn.conf.layers.ConvolutionLayer in project deeplearning4j by deeplearning4j.
the class FineTuneConfiguration method applyToNeuralNetConfiguration.
public void applyToNeuralNetConfiguration(NeuralNetConfiguration nnc) {
Layer l = nnc.getLayer();
Updater originalUpdater = null;
WeightInit origWeightInit = null;
if (l != null) {
originalUpdater = l.getUpdater();
origWeightInit = l.getWeightInit();
if (activationFn != null)
l.setActivationFn(activationFn);
if (weightInit != null)
l.setWeightInit(weightInit);
if (biasInit != null)
l.setBiasInit(biasInit);
if (dist != null)
l.setDist(dist);
if (learningRate != null) {
//usually the same learning rate is applied to both bias and weights
//so always overwrite the learning rate to both?
l.setLearningRate(learningRate);
l.setBiasLearningRate(learningRate);
}
if (biasLearningRate != null)
l.setBiasLearningRate(biasLearningRate);
if (learningRateSchedule != null)
l.setLearningRateSchedule(learningRateSchedule);
// if(lrScoreBasedDecay != null)
if (l1 != null)
l.setL1(l1);
if (l2 != null)
l.setL2(l2);
if (l1Bias != null)
l.setL1Bias(l1Bias);
if (l2Bias != null)
l.setL2Bias(l2Bias);
if (dropOut != null)
l.setDropOut(dropOut);
if (updater != null)
l.setUpdater(updater);
if (momentum != null)
l.setMomentum(momentum);
if (momentumSchedule != null)
l.setMomentum(momentum);
if (epsilon != null)
l.setEpsilon(epsilon);
if (rho != null)
l.setRho(rho);
if (rmsDecay != null)
l.setRmsDecay(rmsDecay);
if (adamMeanDecay != null)
l.setAdamMeanDecay(adamMeanDecay);
if (adamVarDecay != null)
l.setAdamVarDecay(adamVarDecay);
}
if (miniBatch != null)
nnc.setMiniBatch(miniBatch);
if (numIterations != null)
nnc.setNumIterations(numIterations);
if (maxNumLineSearchIterations != null)
nnc.setMaxNumLineSearchIterations(maxNumLineSearchIterations);
if (seed != null)
nnc.setSeed(seed);
if (useRegularization != null)
nnc.setUseRegularization(useRegularization);
if (optimizationAlgo != null)
nnc.setOptimizationAlgo(optimizationAlgo);
if (stepFunction != null)
nnc.setStepFunction(stepFunction);
if (useDropConnect != null)
nnc.setUseDropConnect(useDropConnect);
if (minimize != null)
nnc.setMinimize(minimize);
if (gradientNormalization != null)
l.setGradientNormalization(gradientNormalization);
if (gradientNormalizationThreshold != null)
l.setGradientNormalizationThreshold(gradientNormalizationThreshold);
if (learningRatePolicy != null)
nnc.setLearningRatePolicy(learningRatePolicy);
if (lrPolicySteps != null)
nnc.setLrPolicySteps(lrPolicySteps);
if (lrPolicyPower != null)
nnc.setLrPolicyPower(lrPolicyPower);
if (convolutionMode != null && l instanceof ConvolutionLayer) {
((ConvolutionLayer) l).setConvolutionMode(convolutionMode);
}
if (convolutionMode != null && l instanceof SubsamplingLayer) {
((SubsamplingLayer) l).setConvolutionMode(convolutionMode);
}
//Check the updater config. If we change updaters, we want to remove the old config to avoid warnings
if (l != null && updater != null && originalUpdater != null && updater != originalUpdater) {
switch(originalUpdater) {
case ADAM:
if (adamMeanDecay == null)
l.setAdamMeanDecay(Double.NaN);
if (adamVarDecay == null)
l.setAdamVarDecay(Double.NaN);
break;
case ADADELTA:
if (rho == null)
l.setRho(Double.NaN);
if (epsilon == null)
l.setEpsilon(Double.NaN);
break;
case NESTEROVS:
if (momentum == null)
l.setMomentum(Double.NaN);
if (momentumSchedule == null)
l.setMomentumSchedule(null);
if (epsilon == null)
l.setEpsilon(Double.NaN);
break;
case ADAGRAD:
if (epsilon == null)
l.setEpsilon(Double.NaN);
break;
case RMSPROP:
if (rmsDecay == null)
l.setRmsDecay(Double.NaN);
if (epsilon == null)
l.setEpsilon(Double.NaN);
break;
}
}
//Check weight init. Remove dist if originally was DISTRIBUTION, and isn't now -> remove no longer needed distribution
if (l != null && origWeightInit == WeightInit.DISTRIBUTION && weightInit != null && weightInit != WeightInit.DISTRIBUTION) {
l.setDist(null);
}
//Perform validation. This also sets the defaults for updaters. For example, Updater.RMSProp -> set rmsDecay
if (l != null) {
LayerValidation.updaterValidation(l.getLayerName(), l, momentum, momentumSchedule, adamMeanDecay, adamVarDecay, rho, rmsDecay, epsilon);
boolean useDropCon = (useDropConnect == null ? nnc.isUseDropConnect() : useDropConnect);
LayerValidation.generalValidation(l.getLayerName(), l, nnc.isUseRegularization(), useDropCon, dropOut, l2, l2Bias, l1, l1Bias, dist);
}
//Also: update the LR, L1 and L2 maps, based on current config (which might be different to original config)
if (nnc.variables(false) != null) {
for (String s : nnc.variables(false)) {
nnc.setLayerParamLR(s);
}
}
}
Aggregations