use of org.deeplearning4j.nn.layers.OutputLayer in project deeplearning4j by deeplearning4j.
the class BackTrackLineSearchTest method testSingleMinLineSearch.
@Test
public void testSingleMinLineSearch() throws Exception {
OutputLayer layer = getIrisLogisticLayerConfig(Activation.SOFTMAX, 100, LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD);
int nParams = layer.numParams();
layer.setBackpropGradientsViewArray(Nd4j.create(1, nParams));
layer.setInput(irisData.getFeatureMatrix());
layer.setLabels(irisData.getLabels());
layer.computeGradientAndScore();
BackTrackLineSearch lineSearch = new BackTrackLineSearch(layer, layer.getOptimizer());
double step = lineSearch.optimize(layer.params(), layer.gradient().gradient(), layer.gradient().gradient());
assertEquals(1.0, step, 1e-3);
}
use of org.deeplearning4j.nn.layers.OutputLayer in project deeplearning4j by deeplearning4j.
the class BackTrackLineSearchTest method testMultMaxLineSearch.
@Test
public void testMultMaxLineSearch() throws Exception {
double score1, score2;
irisData.normalizeZeroMeanZeroUnitVariance();
OutputLayer layer = getIrisLogisticLayerConfig(Activation.SOFTMAX, 100, LossFunctions.LossFunction.MCXENT);
int nParams = layer.numParams();
layer.setBackpropGradientsViewArray(Nd4j.create(1, nParams));
layer.setInput(irisData.getFeatureMatrix());
layer.setLabels(irisData.getLabels());
layer.computeGradientAndScore();
score1 = layer.score();
INDArray origGradient = layer.gradient().gradient().dup();
DefaultStepFunction sf = new DefaultStepFunction();
BackTrackLineSearch lineSearch = new BackTrackLineSearch(layer, sf, layer.getOptimizer());
double step = lineSearch.optimize(layer.params().dup(), layer.gradient().gradient().dup(), layer.gradient().gradient().dup());
INDArray currParams = layer.params();
sf.step(currParams, origGradient, step);
layer.setParams(currParams);
layer.computeGradientAndScore();
score2 = layer.score();
assertTrue("score1 = " + score1 + ", score2 = " + score2, score1 < score2);
}
use of org.deeplearning4j.nn.layers.OutputLayer in project deeplearning4j by deeplearning4j.
the class IterativeReduceFlatMapAdapter method call.
@Override
public Iterable<INDArray> call(Iterator<DataSet> dataSetIterator) throws Exception {
if (!dataSetIterator.hasNext()) {
return Collections.singletonList(Nd4j.zeros(params.value().shape()));
}
List<DataSet> collect = new ArrayList<>();
while (dataSetIterator.hasNext()) {
collect.add(dataSetIterator.next());
}
DataSet data = DataSet.merge(collect, false);
log.debug("Training on " + data.labelCounts());
NeuralNetConfiguration conf = NeuralNetConfiguration.fromJson(json);
int numParams = conf.getLayer().initializer().numParams(conf);
INDArray thisParams = Nd4j.create(1, numParams);
Layer network = conf.getLayer().instantiate(conf, null, 0, thisParams, true);
network.setBackpropGradientsViewArray(Nd4j.create(1, numParams));
INDArray val = params.value().unsafeDuplication();
if (val.length() != network.numParams())
throw new IllegalStateException("Network did not have same number of parameters as the broadcast set parameters");
network.setParams(val);
if (network instanceof OutputLayer) {
OutputLayer o = (OutputLayer) network;
o.fit(data);
} else
network.fit(data.getFeatureMatrix());
return Collections.singletonList(network.params());
}
Aggregations