本文整理汇总了Java中org.deeplearning4j.nn.conf.NeuralNetConfiguration.Builder方法的典型用法代码示例。如果您正苦于以下问题:Java NeuralNetConfiguration.Builder方法的具体用法?Java NeuralNetConfiguration.Builder怎么用?Java NeuralNetConfiguration.Builder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.deeplearning4j.nn.conf.NeuralNetConfiguration
的用法示例。
在下文中一共展示了NeuralNetConfiguration.Builder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAddOutput
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testAddOutput() {
NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.9))
.activation(Activation.IDENTITY);
ComputationGraphConfiguration conf = overallConf.graphBuilder().addInputs("inCentre", "inRight")
.addLayer("denseCentre0", new DenseLayer.Builder().nIn(2).nOut(2).build(), "inCentre")
.addLayer("denseRight0", new DenseLayer.Builder().nIn(2).nOut(2).build(), "inRight")
.addVertex("mergeRight", new MergeVertex(), "denseCentre0", "denseRight0")
.addLayer("outRight",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(4).nOut(2).build(),
"mergeRight")
.setOutputs("outRight").build();
ComputationGraph modelToTune = new ComputationGraph(conf);
modelToTune.init();
ComputationGraph modelNow =
new TransferLearning.GraphBuilder(modelToTune)
.addLayer("outCentre",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(2)
.nOut(3).build(),
"denseCentre0")
.setOutputs("outRight", "outCentre").build();
assertEquals(2, modelNow.getNumOutputArrays());
MultiDataSet rand = new MultiDataSet(new INDArray[] {Nd4j.rand(2, 2), Nd4j.rand(2, 2)},
new INDArray[] {Nd4j.rand(2, 2), Nd4j.rand(2, 3)});
modelNow.fit(rand);
log.info(modelNow.summary());
log.info(modelNow.summary(InputType.feedForward(2),InputType.feedForward(2)));
}
示例2: setConfig
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
public TimeseriesClassifierNetworkBuilder setConfig(Config tsconfig, String networkConfigPrefix) {
this.configuration = tsconfig;
NeuralNetConfiguration.Builder networkConfigurationBuilder = new NeuralNetConfiguration.Builder()
.seed(configuration.getInt(networkConfigPrefix+".seed"))
.optimizationAlgo(OptimizationAlgorithm.valueOf(configuration.getString(networkConfigPrefix+".optimizationAlgo.type")))
.iterations(configuration.getInt(networkConfigPrefix+".optimizationAlgo.iterations"))
.weightInit(WeightInit.valueOf(
configuration.getString(networkConfigPrefix+".weightInit")))
.updater(Updater.valueOf(configuration.getString(networkConfigPrefix+".updater.type")))
.momentum(configuration.getDouble(networkConfigPrefix+".updater.momentum"))
.learningRate(configuration.getDouble(networkConfigPrefix+".learningRate"))
.gradientNormalization(GradientNormalization.valueOf(
configuration.getString(networkConfigPrefix+".gradientNormalization.type")))
.gradientNormalizationThreshold(
configuration.getDouble(networkConfigPrefix+".gradientNormalization.threshold"));
ConfigList layers = configuration.getList(networkConfigPrefix+".layers");
if(layers != null && layers.size()>0){
NeuralNetConfiguration.ListBuilder networkConfigurationListBuilder =
networkConfigurationBuilder.list();
for (int i = 0; i < layers.size(); i++) {
ConfigValue value = layers.get(i);
switch (value.valueType()) {
case OBJECT:
Config configObject = ((ConfigObject) value).toConfig();
networkConfigurationListBuilder
.layer(configObject.getInt("number"),
NetworkTypeFactory.makeLayer(configObject));
break;
case NULL:
throw new RuntimeException("LAYER LIST ITEM CANT BE NULL.");
default:
throw new RuntimeException("LAYER TYPE MUST BE A CONFIG OBJECT");
}
}
this.multiLayerConfiguration = networkConfigurationListBuilder
.pretrain(false).backprop(true).build();
} else {
throw new RuntimeException("NO NETWORK LIST FOUND.");
}
// // ----- Configure the network -----
// MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
// .seed(123) //Random number generator seed for improved repeatability. Optional.
// .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1)
// .weightInit(WeightInit.XAVIER)
// .updater(Updater.NESTEROVS).momentum(0.85)
// .learningRate(0.02)
// .gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue) //Not always required, but helps with this data set
// .gradientNormalizationThreshold(0.45)
// .list()
// .layer(0, new GravesLSTM.Builder().activation("tanh").nIn(1).nOut(10).build())
// .layer(1, new RnnOutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
// .activation("softmax").nIn(10).nOut(numLabelClasses).build())
// .pretrain(false).backprop(true).build();
return this;
}
示例3: getMultiLayerConfiguration
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
/**
* Configure a MultiLayerConfiguration from this Keras Sequential model configuration.
*
* @return MultiLayerConfiguration
*/
public MultiLayerConfiguration getMultiLayerConfiguration()
throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
if (!this.className.equals(config.getFieldClassNameSequential()))
throw new InvalidKerasConfigurationException(
"Keras model class name " + this.className + " incompatible with MultiLayerNetwork");
if (this.inputLayerNames.size() != 1)
throw new InvalidKerasConfigurationException(
"MultiLayeNetwork expects only 1 input (found " + this.inputLayerNames.size() + ")");
if (this.outputLayerNames.size() != 1)
throw new InvalidKerasConfigurationException(
"MultiLayeNetwork expects only 1 output (found " + this.outputLayerNames.size() + ")");
NeuralNetConfiguration.Builder modelBuilder = new NeuralNetConfiguration.Builder();
NeuralNetConfiguration.ListBuilder listBuilder = modelBuilder.list();
/* Add layers one at a time. */
KerasLayer prevLayer = null;
int layerIndex = 0;
for (KerasLayer layer : this.layersOrdered) {
if (layer.isLayer()) {
int nbInbound = layer.getInboundLayerNames().size();
if (nbInbound != 1)
throw new InvalidKerasConfigurationException(
"Layers in MultiLayerConfiguration must have exactly one inbound layer (found "
+ nbInbound + " for layer " + layer.getLayerName() + ")");
if (prevLayer != null) {
InputType[] inputTypes = new InputType[1];
InputPreProcessor preprocessor;
if (prevLayer.isInputPreProcessor()) {
inputTypes[0] = this.outputTypes.get(prevLayer.getInboundLayerNames().get(0));
preprocessor = prevLayer.getInputPreprocessor(inputTypes);
} else {
inputTypes[0] = this.outputTypes.get(prevLayer.getLayerName());
preprocessor = layer.getInputPreprocessor(inputTypes);
}
if (preprocessor != null)
listBuilder.inputPreProcessor(layerIndex, preprocessor);
}
listBuilder.layer(layerIndex++, layer.getLayer());
if (this.outputLayerNames.contains(layer.getLayerName()) && !(layer.getLayer() instanceof IOutputLayer)) {
// TODO: since this is always true right now, it just clutters the output.
// log.warn("Model cannot be trained: output layer " + layer.getLayerName()
// + " is not an IOutputLayer (no loss function specified)");
}
} else if (layer.getVertex() != null)
throw new InvalidKerasConfigurationException("Cannot add vertex to MultiLayerConfiguration (class name "
+ layer.getClassName() + ", layer name " + layer.getLayerName() + ")");
prevLayer = layer;
}
InputType inputType = this.layersOrdered.get(0).getOutputType();
if (inputType != null)
listBuilder.setInputType(inputType);
/* Whether to use standard backprop (or BPTT) or truncated BPTT. */
if (this.useTruncatedBPTT && this.truncatedBPTT > 0)
listBuilder.backpropType(BackpropType.TruncatedBPTT).tBPTTForwardLength(truncatedBPTT)
.tBPTTBackwardLength(truncatedBPTT);
else
listBuilder.backpropType(BackpropType.Standard);
return listBuilder.build();
}
示例4: testSimplerMergeBackProp
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testSimplerMergeBackProp() {
NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.9))
.activation(Activation.IDENTITY)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT);
/*
inCentre inRight
| |
denseCentre0 denseRight0
| |
|------ mergeRight ------|
|
outRight
*/
ComputationGraphConfiguration conf = overallConf.graphBuilder().addInputs("inCentre", "inRight")
.addLayer("denseCentre0", new DenseLayer.Builder().nIn(2).nOut(2).build(), "inCentre")
.addLayer("denseRight0", new DenseLayer.Builder().nIn(2).nOut(2).build(), "inRight")
.addVertex("mergeRight", new MergeVertex(), "denseCentre0", "denseRight0")
.addLayer("outRight",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(4).nOut(2).build(),
"mergeRight")
.setOutputs("outRight").build();
ComputationGraph modelToTune = new ComputationGraph(conf);
modelToTune.init();
MultiDataSet randData = new MultiDataSet(new INDArray[] {Nd4j.rand(2, 2), Nd4j.rand(2, 2)},
new INDArray[] {Nd4j.rand(2, 2)});
INDArray denseCentre0 = modelToTune.feedForward(randData.getFeatures(), false).get("denseCentre0");
MultiDataSet otherRandData =
new MultiDataSet(new INDArray[] {denseCentre0, randData.getFeatures(1)}, randData.getLabels());
ComputationGraphConfiguration otherConf =
overallConf.graphBuilder().addInputs("denseCentre0", "inRight")
.addLayer("denseRight0", new DenseLayer.Builder().nIn(2).nOut(2).build(),
"inRight")
.addVertex("mergeRight", new MergeVertex(), "denseCentre0", "denseRight0")
.addLayer("outRight",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(4)
.nOut(2).build(),
"mergeRight")
.setOutputs("outRight").build();
ComputationGraph modelOther = new ComputationGraph(otherConf);
modelOther.init();
modelOther.getLayer("denseRight0").setParams(modelToTune.getLayer("denseRight0").params());
modelOther.getLayer("outRight").setParams(modelToTune.getLayer("outRight").params());
modelToTune.getVertex("denseCentre0").setLayerAsFrozen();
ComputationGraph modelNow =
new TransferLearning.GraphBuilder(modelToTune).setFeatureExtractor("denseCentre0").build();
int n = 0;
while (n < 5) {
if (n == 0) {
//confirm activations out of the merge are equivalent
assertEquals(modelToTune.feedForward(randData.getFeatures(), false).get("mergeRight"),
modelOther.feedForward(otherRandData.getFeatures(), false).get("mergeRight"));
assertEquals(modelNow.feedForward(randData.getFeatures(), false).get("mergeRight"),
modelOther.feedForward(otherRandData.getFeatures(), false).get("mergeRight"));
}
//confirm activations out of frozen vertex is the same as the input to the other model
modelOther.fit(otherRandData);
modelToTune.fit(randData);
modelNow.fit(randData);
assertEquals(otherRandData.getFeatures(0),
modelNow.feedForward(randData.getFeatures(), false).get("denseCentre0"));
assertEquals(otherRandData.getFeatures(0),
modelToTune.feedForward(randData.getFeatures(), false).get("denseCentre0"));
assertEquals(modelOther.getLayer("denseRight0").params(), modelNow.getLayer("denseRight0").params());
assertEquals(modelOther.getLayer("denseRight0").params(), modelToTune.getLayer("denseRight0").params());
assertEquals(modelOther.getLayer("outRight").params(), modelNow.getLayer("outRight").params());
assertEquals(modelOther.getLayer("outRight").params(), modelToTune.getLayer("outRight").params());
n++;
}
}
示例5: testLessSimpleMergeBackProp
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testLessSimpleMergeBackProp() {
NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.9))
.activation(Activation.IDENTITY);
/*
inCentre inRight
| |
denseCentre0 denseRight0
| |
|------ mergeRight ------|
| |
outCentre outRight
*/
ComputationGraphConfiguration conf = overallConf.graphBuilder().addInputs("inCentre", "inRight")
.addLayer("denseCentre0", new DenseLayer.Builder().nIn(2).nOut(2).build(), "inCentre")
.addLayer("outCentre",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(2).nOut(2).build(),
"denseCentre0")
.addLayer("denseRight0", new DenseLayer.Builder().nIn(3).nOut(2).build(), "inRight")
.addVertex("mergeRight", new MergeVertex(), "denseCentre0", "denseRight0")
.addLayer("outRight",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(4).nOut(2).build(),
"mergeRight")
.setOutputs("outCentre", "outRight").build();
ComputationGraph modelToTune = new ComputationGraph(conf);
modelToTune.init();
modelToTune.getVertex("denseCentre0").setLayerAsFrozen();
MultiDataSet randData = new MultiDataSet(new INDArray[] {Nd4j.rand(2, 2), Nd4j.rand(2, 3)},
new INDArray[] {Nd4j.rand(2, 2), Nd4j.rand(2, 2)});
INDArray denseCentre0 = modelToTune.feedForward(randData.getFeatures(), false).get("denseCentre0");
MultiDataSet otherRandData =
new MultiDataSet(new INDArray[] {denseCentre0, randData.getFeatures(1)}, randData.getLabels());
ComputationGraph modelNow =
new TransferLearning.GraphBuilder(modelToTune).setFeatureExtractor("denseCentre0").build();
assertTrue(modelNow.getLayer("denseCentre0") instanceof FrozenLayer);
int n = 0;
while (n < 5) {
if (n == 0) {
//confirm activations out of the merge are equivalent
assertEquals(modelToTune.feedForward(randData.getFeatures(), false).get("mergeRight"),
modelNow.feedForward(otherRandData.getFeatures(), false).get("mergeRight"));
}
//confirm activations out of frozen vertex is the same as the input to the other model
modelToTune.fit(randData);
modelNow.fit(randData);
assertEquals(otherRandData.getFeatures(0),
modelNow.feedForward(randData.getFeatures(), false).get("denseCentre0"));
assertEquals(otherRandData.getFeatures(0),
modelToTune.feedForward(randData.getFeatures(), false).get("denseCentre0"));
assertEquals(modelToTune.getLayer("denseRight0").params(), modelNow.getLayer("denseRight0").params());
assertEquals(modelToTune.getLayer("outRight").params(), modelNow.getLayer("outRight").params());
assertEquals(modelToTune.getLayer("outCentre").params(), modelNow.getLayer("outCentre").params());
n++;
}
}
示例6: tesUnfrozenSubset
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void tesUnfrozenSubset() {
NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().seed(124)
.activation(Activation.IDENTITY)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).updater(new Sgd(0.1));
/*
(inCentre) (inRight)
| |
denseCentre0 |
| |
,-------- denseCentre1 denseRight0
/ | |
subsetLeft(0-3) denseCentre2 ---- denseRight ---- mergeRight
| | |
denseLeft0 denseCentre3 denseRight1
| | |
(outLeft) (outCentre) (outRight)
*/
ComputationGraphConfiguration conf = overallConf.graphBuilder().addInputs("inCentre", "inRight")
.addLayer("denseCentre0", new DenseLayer.Builder().nIn(10).nOut(9).build(), "inCentre")
.addLayer("denseCentre1", new DenseLayer.Builder().nIn(9).nOut(8).build(), "denseCentre0")
.addLayer("denseCentre2", new DenseLayer.Builder().nIn(8).nOut(7).build(), "denseCentre1")
.addLayer("denseCentre3", new DenseLayer.Builder().nIn(7).nOut(7).build(), "denseCentre2")
.addLayer("outCentre",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(7).nOut(4).build(),
"denseCentre3")
.addVertex("subsetLeft", new SubsetVertex(0, 3), "denseCentre1")
.addLayer("denseLeft0", new DenseLayer.Builder().nIn(4).nOut(5).build(), "subsetLeft")
.addLayer("outLeft",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(5).nOut(6).build(),
"denseLeft0")
.addLayer("denseRight", new DenseLayer.Builder().nIn(7).nOut(7).build(), "denseCentre2")
.addLayer("denseRight0", new DenseLayer.Builder().nIn(2).nOut(3).build(), "inRight")
.addVertex("mergeRight", new MergeVertex(), "denseRight", "denseRight0")
.addLayer("denseRight1", new DenseLayer.Builder().nIn(10).nOut(5).build(), "mergeRight")
.addLayer("outRight",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(5).nOut(5).build(),
"denseRight1")
.setOutputs("outLeft", "outCentre", "outRight").build();
ComputationGraph modelToTune = new ComputationGraph(conf);
modelToTune.init();
TransferLearningHelper helper = new TransferLearningHelper(modelToTune, "denseCentre2");
ComputationGraph modelSubset = helper.unfrozenGraph();
ComputationGraphConfiguration expectedConf =
overallConf.graphBuilder().addInputs("denseCentre1", "denseCentre2", "inRight") //inputs are in sorted order
.addLayer("denseCentre3", new DenseLayer.Builder().nIn(7).nOut(7).build(),
"denseCentre2")
.addLayer("outCentre",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(7)
.nOut(4).build(),
"denseCentre3")
.addVertex("subsetLeft", new SubsetVertex(0, 3), "denseCentre1")
.addLayer("denseLeft0", new DenseLayer.Builder().nIn(4).nOut(5).build(),
"subsetLeft")
.addLayer("outLeft",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(5)
.nOut(6).build(),
"denseLeft0")
.addLayer("denseRight", new DenseLayer.Builder().nIn(7).nOut(7).build(),
"denseCentre2")
.addLayer("denseRight0", new DenseLayer.Builder().nIn(2).nOut(3).build(),
"inRight")
.addVertex("mergeRight", new MergeVertex(), "denseRight", "denseRight0")
.addLayer("denseRight1", new DenseLayer.Builder().nIn(10).nOut(5).build(),
"mergeRight")
.addLayer("outRight",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(5)
.nOut(5).build(),
"denseRight1")
.setOutputs("outLeft", "outCentre", "outRight").build();
ComputationGraph expectedModel = new ComputationGraph(expectedConf);
expectedModel.init();
assertEquals(expectedConf.toJson(), modelSubset.getConfiguration().toJson());
}
示例7: testFitUnFrozen
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testFitUnFrozen() {
NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.9)).seed(124)
.activation(Activation.IDENTITY)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT);
ComputationGraphConfiguration conf = overallConf.graphBuilder().addInputs("inCentre", "inRight")
.addLayer("denseCentre0", new DenseLayer.Builder().nIn(10).nOut(9).build(), "inCentre")
.addLayer("denseCentre1", new DenseLayer.Builder().nIn(9).nOut(8).build(), "denseCentre0")
.addLayer("denseCentre2", new DenseLayer.Builder().nIn(8).nOut(7).build(), "denseCentre1")
.addLayer("denseCentre3", new DenseLayer.Builder().nIn(7).nOut(7).build(), "denseCentre2")
.addLayer("outCentre",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(7).nOut(4).build(),
"denseCentre3")
.addVertex("subsetLeft", new SubsetVertex(0, 3), "denseCentre1")
.addLayer("denseLeft0", new DenseLayer.Builder().nIn(4).nOut(5).build(), "subsetLeft")
.addLayer("outLeft",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(5).nOut(6).build(),
"denseLeft0")
.addLayer("denseRight", new DenseLayer.Builder().nIn(7).nOut(7).build(), "denseCentre2")
.addLayer("denseRight0", new DenseLayer.Builder().nIn(2).nOut(3).build(), "inRight")
.addVertex("mergeRight", new MergeVertex(), "denseRight", "denseRight0")
.addLayer("denseRight1", new DenseLayer.Builder().nIn(10).nOut(5).build(), "mergeRight")
.addLayer("outRight",
new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(5).nOut(5).build(),
"denseRight1")
.setOutputs("outLeft", "outCentre", "outRight").build();
ComputationGraph modelToTune = new ComputationGraph(conf);
modelToTune.init();
INDArray inRight = Nd4j.rand(10, 2);
INDArray inCentre = Nd4j.rand(10, 10);
INDArray outLeft = Nd4j.rand(10, 6);
INDArray outRight = Nd4j.rand(10, 5);
INDArray outCentre = Nd4j.rand(10, 4);
MultiDataSet origData = new MultiDataSet(new INDArray[] {inCentre, inRight},
new INDArray[] {outLeft, outCentre, outRight});
ComputationGraph modelIdentical = modelToTune.clone();
modelIdentical.getVertex("denseCentre0").setLayerAsFrozen();
modelIdentical.getVertex("denseCentre1").setLayerAsFrozen();
modelIdentical.getVertex("denseCentre2").setLayerAsFrozen();
TransferLearningHelper helper = new TransferLearningHelper(modelToTune, "denseCentre2");
MultiDataSet featurizedDataSet = helper.featurize(origData);
assertEquals(modelIdentical.getLayer("denseRight0").params(), modelToTune.getLayer("denseRight0").params());
modelIdentical.fit(origData);
helper.fitFeaturized(featurizedDataSet);
assertEquals(modelIdentical.getLayer("denseCentre0").params(), modelToTune.getLayer("denseCentre0").params());
assertEquals(modelIdentical.getLayer("denseCentre1").params(), modelToTune.getLayer("denseCentre1").params());
assertEquals(modelIdentical.getLayer("denseCentre2").params(), modelToTune.getLayer("denseCentre2").params());
assertEquals(modelIdentical.getLayer("denseCentre3").params(), modelToTune.getLayer("denseCentre3").params());
assertEquals(modelIdentical.getLayer("outCentre").params(), modelToTune.getLayer("outCentre").params());
assertEquals(modelIdentical.getLayer("denseRight").conf().toJson(),
modelToTune.getLayer("denseRight").conf().toJson());
assertEquals(modelIdentical.getLayer("denseRight").params(), modelToTune.getLayer("denseRight").params());
assertEquals(modelIdentical.getLayer("denseRight0").conf().toJson(),
modelToTune.getLayer("denseRight0").conf().toJson());
//assertEquals(modelIdentical.getLayer("denseRight0").params(),modelToTune.getLayer("denseRight0").params());
assertEquals(modelIdentical.getLayer("denseRight1").params(), modelToTune.getLayer("denseRight1").params());
assertEquals(modelIdentical.getLayer("outRight").params(), modelToTune.getLayer("outRight").params());
assertEquals(modelIdentical.getLayer("denseLeft0").params(), modelToTune.getLayer("denseLeft0").params());
assertEquals(modelIdentical.getLayer("outLeft").params(), modelToTune.getLayer("outLeft").params());
log.info(modelIdentical.summary());
log.info(helper.unfrozenGraph().summary());
}
示例8: testMLN
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testMLN() {
DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 3));
NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.1))
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.activation(Activation.IDENTITY);
MultiLayerNetwork modelToFineTune = new MultiLayerNetwork(overallConf.clone().list()
.layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build())
.layer(1, new DenseLayer.Builder().nIn(3).nOut(2).build())
.layer(2, new DenseLayer.Builder().nIn(2).nOut(3).build())
.layer(3, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
.build())
.build());
modelToFineTune.init();
MultiLayerNetwork modelNow = new TransferLearning.Builder(modelToFineTune).setFeatureExtractor(1).build();
List<INDArray> ff = modelToFineTune.feedForwardToLayer(2, randomData.getFeatures(), false);
INDArray asFrozenFeatures = ff.get(2);
TransferLearningHelper helper = new TransferLearningHelper(modelToFineTune, 1);
INDArray paramsLastTwoLayers =
Nd4j.hstack(modelToFineTune.getLayer(2).params(), modelToFineTune.getLayer(3).params());
MultiLayerNetwork notFrozen = new MultiLayerNetwork(overallConf.clone().list()
.layer(0, new DenseLayer.Builder().nIn(2).nOut(3).build())
.layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
.build())
.build(), paramsLastTwoLayers);
assertEquals(asFrozenFeatures, helper.featurize(randomData).getFeatures());
assertEquals(randomData.getLabels(), helper.featurize(randomData).getLabels());
for (int i = 0; i < 5; i++) {
notFrozen.fit(new DataSet(asFrozenFeatures, randomData.getLabels()));
helper.fitFeaturized(helper.featurize(randomData));
modelNow.fit(randomData);
}
INDArray expected = Nd4j.hstack(modelToFineTune.getLayer(0).params(), modelToFineTune.getLayer(1).params(),
notFrozen.params());
INDArray act = modelNow.params();
assertEquals(expected, act);
}
示例9: simpleFineTune
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void simpleFineTune() {
long rng = 12345L;
DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 3));
//original conf
NeuralNetConfiguration.Builder confToChange =
new NeuralNetConfiguration.Builder().seed(rng).optimizationAlgo(OptimizationAlgorithm.LBFGS)
.updater(new Nesterovs(0.01, 0.99));
MultiLayerNetwork modelToFineTune = new MultiLayerNetwork(confToChange.list()
.layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build())
.layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
.build())
.build());
modelToFineTune.init();
//model after applying changes with transfer learning
MultiLayerNetwork modelNow =
new TransferLearning.Builder(modelToFineTune)
.fineTuneConfiguration(new FineTuneConfiguration.Builder().seed(rng)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.updater(new RmsProp(0.5)) //Intent: override both weight and bias LR, unless bias LR is manually set also
.l2(0.4).build())
.build();
for (org.deeplearning4j.nn.api.Layer l : modelNow.getLayers()) {
BaseLayer bl = ((BaseLayer) l.conf().getLayer());
assertEquals(new RmsProp(0.5), bl.getIUpdater());
}
NeuralNetConfiguration.Builder confSet = new NeuralNetConfiguration.Builder().seed(rng)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.updater(new RmsProp(0.5)).l2(0.4);
MultiLayerNetwork expectedModel = new MultiLayerNetwork(confSet.list()
.layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build())
.layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
.build())
.build());
expectedModel.init();
expectedModel.setParams(modelToFineTune.params().dup());
assertEquals(expectedModel.params(), modelNow.params());
//Check json
MultiLayerConfiguration expectedConf = expectedModel.getLayerWiseConfigurations();
assertEquals(expectedConf.toJson(), modelNow.getLayerWiseConfigurations().toJson());
//Check params after fit
modelNow.fit(randomData);
expectedModel.fit(randomData);
assertEquals(modelNow.score(), expectedModel.score(), 1e-6);
INDArray pExp = expectedModel.params();
INDArray pNow = modelNow.params();
assertEquals(pExp, pNow);
}
示例10: testNoutChanges
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testNoutChanges() {
DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 2));
NeuralNetConfiguration.Builder equivalentConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.1));
FineTuneConfiguration overallConf = new FineTuneConfiguration.Builder().updater(new Sgd(0.1))
.build();
MultiLayerNetwork modelToFineTune = new MultiLayerNetwork(equivalentConf.list()
.layer(0, new DenseLayer.Builder().nIn(4).nOut(5).build())
.layer(1, new DenseLayer.Builder().nIn(3).nOut(2).build())
.layer(2, new DenseLayer.Builder().nIn(2).nOut(3).build())
.layer(3, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
.build())
.build());
modelToFineTune.init();
MultiLayerNetwork modelNow = new TransferLearning.Builder(modelToFineTune).fineTuneConfiguration(overallConf)
.nOutReplace(3, 2, WeightInit.XAVIER, WeightInit.XAVIER)
.nOutReplace(0, 3, WeightInit.XAVIER, new NormalDistribution(1, 1e-1)).build();
MultiLayerNetwork modelExpectedArch = new MultiLayerNetwork(equivalentConf.list()
.layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build())
.layer(1, new DenseLayer.Builder().nIn(3).nOut(2).build())
.layer(2, new DenseLayer.Builder().nIn(2).nOut(3).build())
.layer(3, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(2)
.build())
.build());
modelExpectedArch.init();
//Will fail - expected because of dist and weight init changes
//assertEquals(modelExpectedArch.getLayerWiseConfigurations().toJson(), modelNow.getLayerWiseConfigurations().toJson());
BaseLayer bl0 = ((BaseLayer) modelNow.getLayerWiseConfigurations().getConf(0).getLayer());
BaseLayer bl1 = ((BaseLayer) modelNow.getLayerWiseConfigurations().getConf(1).getLayer());
BaseLayer bl3 = ((BaseLayer) modelNow.getLayerWiseConfigurations().getConf(3).getLayer());
assertEquals(bl0.getWeightInit(), WeightInit.XAVIER);
assertEquals(bl0.getDist(), null);
assertEquals(bl1.getWeightInit(), WeightInit.DISTRIBUTION);
assertEquals(bl1.getDist(), new NormalDistribution(1, 1e-1));
assertEquals(bl3.getWeightInit(), WeightInit.XAVIER);
//modelNow should have the same architecture as modelExpectedArch
assertArrayEquals(modelExpectedArch.params().shape(), modelNow.params().shape());
assertArrayEquals(modelExpectedArch.getLayer(0).params().shape(), modelNow.getLayer(0).params().shape());
assertArrayEquals(modelExpectedArch.getLayer(1).params().shape(), modelNow.getLayer(1).params().shape());
assertArrayEquals(modelExpectedArch.getLayer(2).params().shape(), modelNow.getLayer(2).params().shape());
assertArrayEquals(modelExpectedArch.getLayer(3).params().shape(), modelNow.getLayer(3).params().shape());
modelNow.setParams(modelExpectedArch.params());
//fit should give the same results
modelExpectedArch.fit(randomData);
modelNow.fit(randomData);
assertEquals(modelExpectedArch.score(), modelNow.score(), 0.000001);
assertEquals(modelExpectedArch.params(), modelNow.params());
}
示例11: testRemoveAndAdd
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testRemoveAndAdd() {
DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 3));
NeuralNetConfiguration.Builder equivalentConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.1));
FineTuneConfiguration overallConf = new FineTuneConfiguration.Builder().updater(new Sgd(0.1)).build();
MultiLayerNetwork modelToFineTune = new MultiLayerNetwork(//overallConf.list()
equivalentConf.list().layer(0, new DenseLayer.Builder().nIn(4).nOut(5).build())
.layer(1, new DenseLayer.Builder().nIn(5).nOut(2).build())
.layer(2, new DenseLayer.Builder().nIn(2).nOut(3).build())
.layer(3, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT)
.activation(Activation.SOFTMAX).nIn(3).nOut(3)
.build())
.build());
modelToFineTune.init();
MultiLayerNetwork modelNow =
new TransferLearning.Builder(modelToFineTune).fineTuneConfiguration(overallConf)
.nOutReplace(0, 7, WeightInit.XAVIER, WeightInit.XAVIER)
.nOutReplace(2, 5, WeightInit.XAVIER).removeOutputLayer()
.addLayer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(5)
.nOut(3).updater(new Sgd(0.5)).activation(Activation.SOFTMAX)
.build())
.build();
MultiLayerNetwork modelExpectedArch = new MultiLayerNetwork(equivalentConf.list()
.layer(0, new DenseLayer.Builder().nIn(4).nOut(7).build())
.layer(1, new DenseLayer.Builder().nIn(7).nOut(2).build())
.layer(2, new DenseLayer.Builder().nIn(2).nOut(5).build())
.layer(3, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX)
.updater(new Sgd(0.5)).nIn(5).nOut(3).build())
.build());
modelExpectedArch.init();
//modelNow should have the same architecture as modelExpectedArch
assertArrayEquals(modelExpectedArch.params().shape(), modelNow.params().shape());
assertArrayEquals(modelExpectedArch.getLayer(0).params().shape(), modelNow.getLayer(0).params().shape());
assertArrayEquals(modelExpectedArch.getLayer(1).params().shape(), modelNow.getLayer(1).params().shape());
assertArrayEquals(modelExpectedArch.getLayer(2).params().shape(), modelNow.getLayer(2).params().shape());
assertArrayEquals(modelExpectedArch.getLayer(3).params().shape(), modelNow.getLayer(3).params().shape());
modelNow.setParams(modelExpectedArch.params());
//fit should give the same results
modelExpectedArch.fit(randomData);
modelNow.fit(randomData);
assertTrue(modelExpectedArch.score() == modelNow.score());
assertEquals(modelExpectedArch.params(), modelNow.params());
}
示例12: testNoutChanges
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testNoutChanges() {
DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 2));
NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.1))
.activation(Activation.IDENTITY);
FineTuneConfiguration fineTuneConfiguration = new FineTuneConfiguration.Builder().updater(new Sgd(0.1))
.activation(Activation.IDENTITY).build();
ComputationGraph modelToFineTune = new ComputationGraph(overallConf.graphBuilder().addInputs("layer0In")
.addLayer("layer0", new DenseLayer.Builder().nIn(4).nOut(5).build(), "layer0In")
.addLayer("layer1", new DenseLayer.Builder().nIn(3).nOut(2).build(), "layer0")
.addLayer("layer2", new DenseLayer.Builder().nIn(2).nOut(3).build(), "layer1")
.addLayer("layer3",
new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT)
.activation(Activation.SOFTMAX).nIn(3).nOut(3)
.build(),
"layer2")
.setOutputs("layer3").build());
modelToFineTune.init();
ComputationGraph modelNow = new TransferLearning.GraphBuilder(modelToFineTune)
.fineTuneConfiguration(fineTuneConfiguration).nOutReplace("layer3", 2, WeightInit.XAVIER)
.nOutReplace("layer0", 3, new NormalDistribution(1, 1e-1), WeightInit.XAVIER)
//.setOutputs("layer3")
.build();
BaseLayer bl0 = ((BaseLayer) modelNow.getLayer("layer0").conf().getLayer());
BaseLayer bl1 = ((BaseLayer) modelNow.getLayer("layer1").conf().getLayer());
BaseLayer bl3 = ((BaseLayer) modelNow.getLayer("layer3").conf().getLayer());
assertEquals(bl0.getWeightInit(), WeightInit.DISTRIBUTION);
assertEquals(bl0.getDist(), new NormalDistribution(1, 1e-1));
assertEquals(bl1.getWeightInit(), WeightInit.XAVIER);
assertEquals(bl1.getDist(), null);
assertEquals(bl1.getWeightInit(), WeightInit.XAVIER);
ComputationGraph modelExpectedArch = new ComputationGraph(overallConf.graphBuilder().addInputs("layer0In")
.addLayer("layer0", new DenseLayer.Builder().nIn(4).nOut(3).build(), "layer0In")
.addLayer("layer1", new DenseLayer.Builder().nIn(3).nOut(2).build(), "layer0")
.addLayer("layer2", new DenseLayer.Builder().nIn(2).nOut(3).build(), "layer1")
.addLayer("layer3",
new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT)
.activation(Activation.SOFTMAX).nIn(3).nOut(2)
.build(),
"layer2")
.setOutputs("layer3").build());
modelExpectedArch.init();
//modelNow should have the same architecture as modelExpectedArch
assertArrayEquals(modelExpectedArch.params().shape(), modelNow.params().shape());
assertArrayEquals(modelExpectedArch.getLayer("layer0").params().shape(),
modelNow.getLayer("layer0").params().shape());
assertArrayEquals(modelExpectedArch.getLayer("layer1").params().shape(),
modelNow.getLayer("layer1").params().shape());
assertArrayEquals(modelExpectedArch.getLayer("layer2").params().shape(),
modelNow.getLayer("layer2").params().shape());
assertArrayEquals(modelExpectedArch.getLayer("layer3").params().shape(),
modelNow.getLayer("layer3").params().shape());
modelNow.setParams(modelExpectedArch.params());
//fit should give the same results
modelExpectedArch.fit(randomData);
modelNow.fit(randomData);
assertEquals(modelExpectedArch.score(), modelNow.score(), 1e-8);
assertEquals(modelExpectedArch.params(), modelNow.params());
}
示例13: testRemoveAndAdd
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testRemoveAndAdd() {
DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 3));
NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.1))
.activation(Activation.IDENTITY);
FineTuneConfiguration fineTuneConfiguration = new FineTuneConfiguration.Builder().updater(new Sgd(0.1))
.activation(Activation.IDENTITY).build();
ComputationGraph modelToFineTune = new ComputationGraph(overallConf.graphBuilder().addInputs("layer0In")
.addLayer("layer0", new DenseLayer.Builder().nIn(4).nOut(5).build(), "layer0In")
.addLayer("layer1", new DenseLayer.Builder().nIn(5).nOut(2).build(), "layer0")
.addLayer("layer2", new DenseLayer.Builder().nIn(2).nOut(3).build(), "layer1")
.addLayer("layer3",
new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT)
.activation(Activation.SOFTMAX).nIn(3).nOut(3)
.build(),
"layer2")
.setOutputs("layer3").build());
modelToFineTune.init();
ComputationGraph modelNow = new TransferLearning.GraphBuilder(modelToFineTune)
.fineTuneConfiguration(fineTuneConfiguration)
.nOutReplace("layer0", 7, WeightInit.XAVIER, WeightInit.XAVIER)
.nOutReplace("layer2", 5, WeightInit.XAVIER).removeVertexKeepConnections("layer3")
.addLayer("layer3",
new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(5).nOut(3)
.activation(Activation.SOFTMAX).build(),
"layer2")
//.setOutputs("layer3")
.build();
ComputationGraph modelExpectedArch = new ComputationGraph(overallConf.graphBuilder().addInputs("layer0In")
.addLayer("layer0", new DenseLayer.Builder().nIn(4).nOut(7).build(), "layer0In")
.addLayer("layer1", new DenseLayer.Builder().nIn(7).nOut(2).build(), "layer0")
.addLayer("layer2", new DenseLayer.Builder().nIn(2).nOut(5).build(), "layer1")
.addLayer("layer3",
new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT)
.activation(Activation.SOFTMAX).nIn(5).nOut(3)
.build(),
"layer2")
.setOutputs("layer3").build());
modelExpectedArch.init();
//modelNow should have the same architecture as modelExpectedArch
assertArrayEquals(modelExpectedArch.params().shape(), modelNow.params().shape());
assertArrayEquals(modelExpectedArch.getLayer("layer0").params().shape(),
modelNow.getLayer("layer0").params().shape());
assertArrayEquals(modelExpectedArch.getLayer("layer1").params().shape(),
modelNow.getLayer("layer1").params().shape());
assertArrayEquals(modelExpectedArch.getLayer("layer2").params().shape(),
modelNow.getLayer("layer2").params().shape());
assertArrayEquals(modelExpectedArch.getLayer("layer3").params().shape(),
modelNow.getLayer("layer3").params().shape());
modelNow.setParams(modelExpectedArch.params());
//fit should give the same results
modelExpectedArch.fit(randomData);
modelNow.fit(randomData);
assertEquals(modelExpectedArch.score(), modelNow.score(), 1e-8);
assertEquals(modelExpectedArch.params(), modelNow.params());
}
示例14: testFrozen
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void testFrozen() {
DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 3));
NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.1))
.activation(Activation.IDENTITY);
FineTuneConfiguration finetune = new FineTuneConfiguration.Builder().updater(new Sgd(0.1)).build();
MultiLayerNetwork modelToFineTune = new MultiLayerNetwork(overallConf.clone().list()
.layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build())
.layer(1, new DenseLayer.Builder().nIn(3).nOut(2).build())
.layer(2, new DenseLayer.Builder().nIn(2).nOut(3).build())
.layer(3, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
.build())
.build());
modelToFineTune.init();
List<INDArray> ff = modelToFineTune.feedForwardToLayer(2, randomData.getFeatures(), false);
INDArray asFrozenFeatures = ff.get(2);
MultiLayerNetwork modelNow = new TransferLearning.Builder(modelToFineTune).fineTuneConfiguration(finetune)
.setFeatureExtractor(1).build();
INDArray paramsLastTwoLayers =
Nd4j.hstack(modelToFineTune.getLayer(2).params(), modelToFineTune.getLayer(3).params());
MultiLayerNetwork notFrozen = new MultiLayerNetwork(overallConf.clone().list()
.layer(0, new DenseLayer.Builder().nIn(2).nOut(3).build())
.layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
.build())
.build(), paramsLastTwoLayers);
// assertEquals(modelNow.getLayer(2).conf(), notFrozen.getLayer(0).conf()); //Equal, other than names
// assertEquals(modelNow.getLayer(3).conf(), notFrozen.getLayer(1).conf()); //Equal, other than names
//Check: forward pass
INDArray outNow = modelNow.output(randomData.getFeatures());
INDArray outNotFrozen = notFrozen.output(asFrozenFeatures);
assertEquals(outNow, outNotFrozen);
for (int i = 0; i < 5; i++) {
notFrozen.fit(new DataSet(asFrozenFeatures, randomData.getLabels()));
modelNow.fit(randomData);
}
INDArray expected = Nd4j.hstack(modelToFineTune.getLayer(0).params(), modelToFineTune.getLayer(1).params(),
notFrozen.params());
INDArray act = modelNow.params();
assertEquals(expected, act);
}
示例15: cloneMLNFrozen
import org.deeplearning4j.nn.conf.NeuralNetConfiguration; //导入方法依赖的package包/类
@Test
public void cloneMLNFrozen() {
DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 3));
NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.1))
.activation(Activation.IDENTITY);
MultiLayerNetwork modelToFineTune = new MultiLayerNetwork(overallConf.list()
.layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build())
.layer(1, new DenseLayer.Builder().nIn(3).nOut(2).build())
.layer(2, new DenseLayer.Builder().nIn(2).nOut(3).build())
.layer(3, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
.build())
.build());
modelToFineTune.init();
INDArray asFrozenFeatures = modelToFineTune.feedForwardToLayer(2, randomData.getFeatures(), false).get(2);
MultiLayerNetwork modelNow = new TransferLearning.Builder(modelToFineTune).setFeatureExtractor(1).build();
MultiLayerNetwork clonedModel = modelNow.clone();
//Check json
assertEquals(modelNow.getLayerWiseConfigurations().toJson(), clonedModel.getLayerWiseConfigurations().toJson());
//Check params
assertEquals(modelNow.params(), clonedModel.params());
MultiLayerNetwork notFrozen = new MultiLayerNetwork(
overallConf.list().layer(0, new DenseLayer.Builder().nIn(2).nOut(3).build())
.layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT)
.activation(Activation.SOFTMAX).nIn(3).nOut(3)
.build())
.build(),
Nd4j.hstack(modelToFineTune.getLayer(2).params(), modelToFineTune.getLayer(3).params()));
int i = 0;
while (i < 5) {
notFrozen.fit(new DataSet(asFrozenFeatures, randomData.getLabels()));
modelNow.fit(randomData);
clonedModel.fit(randomData);
i++;
}
INDArray expectedParams = Nd4j.hstack(modelToFineTune.getLayer(0).params(),
modelToFineTune.getLayer(1).params(), notFrozen.params());
assertEquals(expectedParams, modelNow.params());
assertEquals(expectedParams, clonedModel.params());
}