org.deeplearning4j.nn.graph.ComputationGraph Java Examples

The following examples show how to use org.deeplearning4j.nn.graph.ComputationGraph. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NeuralStyleTransfer.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 6 votes vote down vote up
private INDArray backPropagateStyles(ComputationGraph vgg16FineTune,
                                     HashMap<String, INDArray> StyleActivationsGramMap,
                                     Map<String, INDArray> generatedActivationsMap) throws Exception {
    INDArray styleBackProb = Nd4j.zeros(new int[]{1, CHANNELS, HEIGHT, WIDTH});
    CountDownLatch countDownLatch = new CountDownLatch(STYLE_LAYERS.length);

    for (String styleLayer : STYLE_LAYERS) {
        String[] split = styleLayer.split(",");
        String styleLayerName = split[0];
        INDArray styleGramValues = StyleActivationsGramMap.get(styleLayerName);
        INDArray generatedValues = generatedActivationsMap.get(styleLayerName);
        double weight = Double.parseDouble(split[1]);
        int index = findLayerIndex(styleLayerName);
        executorService.execute(() -> {
            INDArray dStyleValues = styleCostFunction.styleContentFunctionDerivative(styleGramValues, generatedValues).transpose();
            INDArray backProb = backPropagate(vgg16FineTune, dStyleValues.reshape(generatedValues.shape()), index).muli(weight);
            styleBackProb.addi(backProb);
            countDownLatch.countDown();
        });
    }
    countDownLatch.await();
    return styleBackProb;
}
 
Example #2
Source File: CenterLossOutputLayerTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private ComputationGraph getGraph(int numLabels, double lambda) {
    Nd4j.getRandom().setSeed(12345);
    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345)
                    .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                    .dist(new NormalDistribution(0, 1)).updater(new NoOp())
                    .graphBuilder().addInputs("input1")
                    .addLayer("l1", new DenseLayer.Builder().nIn(4).nOut(5).activation(Activation.RELU).build(),
                                    "input1")
                    .addLayer("lossLayer", new CenterLossOutputLayer.Builder()
                                    .lossFunction(LossFunctions.LossFunction.MCXENT).nIn(5).nOut(numLabels)
                                    .lambda(lambda).activation(Activation.SOFTMAX).build(), "l1")
                    .setOutputs("lossLayer").build();

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

    return graph;
}
 
Example #3
Source File: KerasLambdaTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testModelLambdaLayerImport() throws Exception {
    KerasLayer.registerLambdaLayer("lambda_3", new ExponentialLambda());
    KerasLayer.registerLambdaLayer("lambda_4", new TimesThreeLambda());

    String modelPath = "modelimport/keras/examples/lambda/model_lambda.h5";

    try(InputStream is = Resources.asStream(modelPath)) {
        File modelFile = testDir.newFile("tempModel" + System.currentTimeMillis() + ".h5");
        Files.copy(is, modelFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        ComputationGraph model = new KerasModel().modelBuilder().modelHdf5Filename(modelFile.getAbsolutePath())
                .enforceTrainingConfig(false).buildModel().getComputationGraph();

        System.out.println(model.summary());
        INDArray input = Nd4j.create(new int[]{10, 784});

        model.output(input);
    } finally {
        KerasLayer.clearLambdaLayers(); // Clear all lambdas, so other tests aren't affected.
    }
}
 
Example #4
Source File: Dl4jVGG.java    From wekaDeeplearning4j with GNU General Public License v3.0 6 votes vote down vote up
public ComputationGraph init(int numLabels, long seed, int[] shape, boolean filterMode) {
    ZooModel net = null;
    if (m_variation == VGG.VARIATION.VGG16) {
        net = org.deeplearning4j.zoo.model.VGG16.builder()
                .cacheMode(CacheMode.NONE)
                .workspaceMode(Preferences.WORKSPACE_MODE)
                .inputShape(shape)
                .numClasses(numLabels)
                .build();
    } else if (m_variation == VGG.VARIATION.VGG19) {
        net = org.deeplearning4j.zoo.model.VGG19.builder()
                .cacheMode(CacheMode.NONE)
                .workspaceMode(Preferences.WORKSPACE_MODE)
                .inputShape(shape)
                .numClasses(numLabels)
                .build();
    }

    ComputationGraph defaultNet = net.init();

    return attemptToLoadWeights(net, defaultNet, seed, numLabels, filterMode);
}
 
Example #5
Source File: AbstractZooModel.java    From wekaDeeplearning4j with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Attempts to download weights for the given zoo model
 * @param zooModel Model to try download weights for
 * @return new ComputationGraph initialized with the given PretrainedType
 */
protected ComputationGraph downloadWeights(org.deeplearning4j.zoo.ZooModel zooModel) {
    try {
        log.info(String.format("Downloading %s weights", m_pretrainedType));
        Object pretrained = zooModel.initPretrained(m_pretrainedType.getBackend());
        if (pretrained == null) {
            throw new Exception("Error while initialising model");
        }
        if (pretrained instanceof MultiLayerNetwork) {
            return ((MultiLayerNetwork) pretrained).toComputationGraph();
        } else {
            return (ComputationGraph) pretrained;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}
 
Example #6
Source File: TestVertxUI.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testUICompGraph() {

    StatsStorage ss = new InMemoryStatsStorage();

    UIServer uiServer = UIServer.getInstance();
    uiServer.attach(ss);

    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().graphBuilder().addInputs("in")
                    .addLayer("L0", new DenseLayer.Builder().activation(Activation.TANH).nIn(4).nOut(4).build(),
                                    "in")
                    .addLayer("L1", new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT)
                                    .activation(Activation.SOFTMAX).nIn(4).nOut(3).build(), "L0")
                    .setOutputs("L1").build();

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

    net.setListeners(new StatsListener(ss), new ScoreIterationListener(1));

    DataSetIterator iter = new IrisDataSetIterator(150, 150);

    for (int i = 0; i < 100; i++) {
        net.fit(iter);
    }
}
 
Example #7
Source File: Dl4jMlpClassifier.java    From wekaDeeplearning4j with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Build the multilayer network defined by the networkconfiguration and the list of layers.
 */
protected void createModel() throws Exception {
  final INDArray features = getFirstBatchFeatures(trainData);
  ComputationGraphConfiguration.GraphBuilder gb =
      netConfig.builder().seed(getSeed()).graphBuilder();

  // Set ouput size
  final Layer lastLayer = layers[layers.length - 1];
  final int nOut = trainData.numClasses();
  if (lastLayer instanceof FeedForwardLayer) {
    ((FeedForwardLayer) lastLayer).setNOut(nOut);
  }

  if (getInstanceIterator() instanceof CnnTextEmbeddingInstanceIterator) {
    makeCnnTextLayerSetup(gb);
  } else {
    makeDefaultLayerSetup(gb);
  }

  gb.setInputTypes(InputType.inferInputType(features));
  ComputationGraphConfiguration conf =
      gb.build();
  ComputationGraph model = new ComputationGraph(conf);
  model.init();
  this.model = model;
}
 
Example #8
Source File: DL4jServlet.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private O process(MultiDataSet mds) {
    O result = null;
    if (parallelEnabled) {
        // process result
        result = inferenceAdapter.apply(parallelInference.output(mds.getFeatures(), mds.getFeaturesMaskArrays()));
    } else {
        synchronized (this) {
            if (model instanceof ComputationGraph)
                result = inferenceAdapter.apply(((ComputationGraph) model).output(false, mds.getFeatures(), mds.getFeaturesMaskArrays()));
            else if (model instanceof MultiLayerNetwork) {
                Preconditions.checkArgument(mds.getFeatures().length > 0 || (mds.getFeaturesMaskArrays() != null && mds.getFeaturesMaskArrays().length > 0),
                        "Input data for MultilayerNetwork is invalid!");
                result = inferenceAdapter.apply(((MultiLayerNetwork) model).output(mds.getFeatures()[0], false,
                        mds.getFeaturesMaskArrays() != null ? mds.getFeaturesMaskArrays()[0] : null, null));
            }
        }
    }
    return result;
}
 
Example #9
Source File: ScoreUtil.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Score based on the loss function
 * @param model the model to score with
 * @param testData the test data to score
 * @param average whether to average the score
 *                for the whole batch or not
 * @return the score for the given test set
 */
public static double score(ComputationGraph model, MultiDataSetIterator testData, boolean average) {
    //TODO: do this properly taking into account division by N, L1/L2 etc
    double sumScore = 0.0;
    int totalExamples = 0;
    while (testData.hasNext()) {
        MultiDataSet ds = testData.next();
        long numExamples = ds.getFeatures(0).size(0);
        sumScore += numExamples * model.score(ds);
        totalExamples += numExamples;
    }

    if (!average)
        return sumScore;
    return sumScore / totalExamples;
}
 
Example #10
Source File: ReverseTimeSeriesVertex.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public ReverseTimeSeriesVertex(ComputationGraph graph, String name, int vertexIndex, String inputName, DataType dataType) {
    super(graph, name, vertexIndex, null, null, dataType);
    this.inputName = inputName;


    if (inputName == null) {
        // Don't use masks
        this.inputIdx = -1;
    } else {
        // Find the given input
        this.inputIdx = graph.getConfiguration().getNetworkInputs().indexOf(inputName);
        if (inputIdx == -1)
            throw new IllegalArgumentException("Invalid input name: \"" + inputName + "\" not found in list "
                    + "of network inputs (" + graph.getConfiguration().getNetworkInputs() + ")");
    }
}
 
Example #11
Source File: GraphTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
private ComputationGraph getOldFunction() {
    NeuralNetConfiguration.Builder netBuilder = new NeuralNetConfiguration.Builder();
    // 设置随机种子
    netBuilder.seed(6);
    netBuilder.setL1(l1Regularization);
    netBuilder.setL1Bias(l1Regularization);
    netBuilder.setL2(l2Regularization);
    netBuilder.setL2Bias(l2Regularization);
    netBuilder.weightInit(WeightInit.XAVIER_UNIFORM);
    netBuilder.updater(new Sgd(learnRatio)).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT);

    GraphBuilder graphBuilder = netBuilder.graphBuilder();
    graphBuilder.addInputs("leftInput", "rightInput");
    graphBuilder.addLayer("leftEmbed", new EmbeddingLayer.Builder().nIn(5).nOut(5).hasBias(true).activation(Activation.IDENTITY).build(), "leftInput");
    graphBuilder.addLayer("rightEmbed", new EmbeddingLayer.Builder().nIn(5).nOut(5).hasBias(true).activation(Activation.IDENTITY).build(), "rightInput");
    graphBuilder.addVertex("embed", new MergeVertex(), "leftEmbed", "rightEmbed");
    graphBuilder.addLayer("output", new OutputLayer.Builder(LossFunctions.LossFunction.MSE).activation(Activation.IDENTITY).nIn(10).nOut(1).build(), "embed");
    graphBuilder.setOutputs("output");

    ComputationGraphConfiguration configuration = graphBuilder.build();
    ComputationGraph graph = new ComputationGraph(configuration);
    graph.init();
    return graph;
}
 
Example #12
Source File: ScoringModelTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
protected Model buildComputationGraphModel(int numFeatures) throws Exception {

    final ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder()
        .graphBuilder()
        .addInputs("inputLayer")
        .addLayer("outputLayer",
          new OutputLayer.Builder().nIn(numFeatures).nOut(1).lossFunction(LossFunctions.LossFunction.MSE).activation(Activation.IDENTITY).build(),
          "inputLayer")
        .setOutputs("outputLayer")
        .build();

    final ComputationGraph model = new ComputationGraph(conf);
    model.init();

    final float[] floats = new float[numFeatures+1];
    float base = 1f;
    for (int ii=0; ii<floats.length; ++ii)
    {
      base *= 2;
      floats[ii] = base;
    }

    final INDArray params = Nd4j.create(floats);
    model.setParams(params);

    return model;
  }
 
Example #13
Source File: InplaceParallelInferenceTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testOutput_RoundRobin_1() throws Exception {
    int nIn = 5;

    val conf = new NeuralNetConfiguration.Builder()
            .graphBuilder()
            .addInputs("in")
            .layer("out0", new OutputLayer.Builder().nIn(nIn).nOut(4).activation(Activation.SOFTMAX).build(), "in")
            .layer("out1", new OutputLayer.Builder().nIn(nIn).nOut(6).activation(Activation.SOFTMAX).build(), "in")
            .setOutputs("out0", "out1")
            .build();

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

    val pi = new ParallelInference.Builder(net)
            .inferenceMode(InferenceMode.INPLACE)
            .loadBalanceMode(LoadBalanceMode.ROUND_ROBIN)
            .workers(2)
            .build();

    try {

        val result0 = pi.output(new INDArray[]{Nd4j.create(new double[]{1.0, 2.0, 3.0, 4.0, 5.0}, new long[]{1, 5})}, null)[0];
        val result1 = pi.output(new INDArray[]{Nd4j.create(new double[]{1.0, 2.0, 3.0, 4.0, 5.0}, new long[]{1, 5})}, null)[0];

        assertNotNull(result0);
        assertEquals(result0, result1);
    } finally {
        pi.shutdown();
    }
}
 
Example #14
Source File: PolicyTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testACPolicy() throws Exception {
        ComputationGraph cg = new ComputationGraph(new NeuralNetConfiguration.Builder().seed(444).graphBuilder().addInputs("input")
                .addLayer("output", new OutputLayer.Builder().nOut(1).lossFunction(LossFunctions.LossFunction.XENT).activation(Activation.SIGMOID).build(), "input").setOutputs("output").build());
        MultiLayerNetwork mln = new MultiLayerNetwork(new NeuralNetConfiguration.Builder().seed(555).list()
                .layer(0, new OutputLayer.Builder().nOut(1).lossFunction(LossFunctions.LossFunction.XENT).activation(Activation.SIGMOID).build()).build());

        ACPolicy policy = new ACPolicy(new DummyAC(cg));
        assertNotNull(policy.rnd);

        policy = new ACPolicy(new DummyAC(mln));
        assertNotNull(policy.rnd);

        INDArray input = Nd4j.create(new double[] {1.0, 0.0}, new long[]{1,2});
        for (int i = 0; i < 100; i++) {
            assertEquals(0, (int)policy.nextAction(input));
        }

        input = Nd4j.create(new double[] {0.0, 1.0}, new long[]{1,2});
        for (int i = 0; i < 100; i++) {
            assertEquals(1, (int)policy.nextAction(input));
        }

        input = Nd4j.create(new double[] {0.1, 0.2, 0.3, 0.4}, new long[]{1, 4});
        int[] count = new int[4];
        for (int i = 0; i < 100; i++) {
            count[policy.nextAction(input)]++;
        }
//        System.out.println(count[0] + " " + count[1] + " " + count[2] + " " + count[3]);
        assertTrue(count[0] < 20);
        assertTrue(count[1] < 30);
        assertTrue(count[2] < 40);
        assertTrue(count[3] < 50);
    }
 
Example #15
Source File: DataSetLossCalculator.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
protected INDArray[] output(Model network, INDArray[] input, INDArray[] fMask, INDArray[] lMask) {
    if(network instanceof MultiLayerNetwork){
        INDArray out = ((MultiLayerNetwork) network).output(input[0], false, get0(fMask), get0(lMask));
        return new INDArray[]{out};
    } else if(network instanceof ComputationGraph){
        return ((ComputationGraph) network).output(false, input, fMask, lMask);
    } else {
        throw new RuntimeException("Unknown model type: " + network.getClass());
    }
}
 
Example #16
Source File: TestEarlyStoppingCompGraph.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEarlyStoppingListenersCG() {
    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder()
            .updater(new Sgd(0.001)).weightInit(WeightInit.XAVIER)
            .graphBuilder()
            .addInputs("in")
            .layer("0", new OutputLayer.Builder().nIn(4).nOut(3)
                    .activation(Activation.SOFTMAX)
                    .lossFunction(LossFunctions.LossFunction.MCXENT).build(), "in")
            .setOutputs("0")
            .build();
    ComputationGraph net = new ComputationGraph(conf);

    TestEarlyStopping.TestListener tl = new TestEarlyStopping.TestListener();
    net.setListeners(tl);

    DataSetIterator irisIter = new IrisDataSetIterator(50, 150);
    EarlyStoppingModelSaver<ComputationGraph> saver = new InMemoryModelSaver<>();
    EarlyStoppingConfiguration<ComputationGraph> esConf =
            new EarlyStoppingConfiguration.Builder<ComputationGraph>()
                    .epochTerminationConditions(new MaxEpochsTerminationCondition(5))
                    .iterationTerminationConditions(
                            new MaxTimeIterationTerminationCondition(1, TimeUnit.MINUTES))
                    .scoreCalculator(new DataSetLossCalculator(irisIter, true)).modelSaver(saver)
                    .build();

    IEarlyStoppingTrainer<ComputationGraph> trainer = new EarlyStoppingGraphTrainer(esConf, net, irisIter);

    trainer.fit();

    assertEquals(5, tl.getCountEpochStart());
    assertEquals(5, tl.getCountEpochEnd());
    assertEquals(5 * 150/50, tl.getIterCount());

    assertEquals(4, tl.getMaxEpochStart());
    assertEquals(4, tl.getMaxEpochEnd());
}
 
Example #17
Source File: DLModel.java    From java-ml-projects with Apache License 2.0 5 votes vote down vote up
public String outputForImageFile(File file, int h, int w, int channels) throws IOException {
	NativeImageLoader loader = new NativeImageLoader(h, w, channels);
	INDArray img1 = loader.asMatrix(file);
	if (model instanceof ComputationGraph) {
		((ComputationGraph) model).output(img1);
	} else if (model instanceof MultiLayerNetwork) {
		((MultiLayerNetwork) model).output(img1);
	}
	activationsCache.clear();
	return null;
}
 
Example #18
Source File: KerasVGG.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ComputationGraph init(int numLabels, long seed, int[] shape, boolean filterMode) {
    VGG vgg = new VGG();
    vgg.setVariation(variation);

    return attemptToLoadWeights(vgg, null, seed, numLabels, filterMode);
}
 
Example #19
Source File: ImageClassifier.java    From java-ml-projects with Apache License 2.0 5 votes vote down vote up
private INDArray getOutput(INDArray image) {
	if (dl4jModel instanceof MultiLayerNetwork) {
		MultiLayerNetwork multiLayerNetwork = (MultiLayerNetwork) dl4jModel;
		multiLayerNetwork.init();
		return multiLayerNetwork.output(image);
	} else {
		ComputationGraph graph = (ComputationGraph) dl4jModel;
		graph.init();
		return graph.output(image)[0];
	}
}
 
Example #20
Source File: TransferLearningVGG16.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private void saveProgressEveryConfiguredInterval(ComputationGraph vgg16Transfer, int iEpoch, int
        iIteration) throws IOException {
    if (iIteration % SAVING_INTERVAL == 0 && iIteration != 0) {

        ModelSerializer.writeModel(vgg16Transfer, new File(SAVING_PATH + iIteration + "_epoch_" + iEpoch + ".zip"),
                false);
        evalOn(vgg16Transfer, neuralNetworkTrainingData.getDevIterator(), iIteration);
    }
}
 
Example #21
Source File: Dl4jResNet50.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ComputationGraph init(int numLabels, long seed, int[] shape, boolean filterMode) {
    org.deeplearning4j.zoo.model.ResNet50 net = org.deeplearning4j.zoo.model.ResNet50.builder()
            .cacheMode(CacheMode.NONE)
            .workspaceMode(Preferences.WORKSPACE_MODE)
            .inputShape(shape)
            .numClasses(numLabels)
            .build();

    ComputationGraph defaultNet = net.init();

    return attemptToLoadWeights(net, defaultNet, seed, numLabels, filterMode);
}
 
Example #22
Source File: TrainCifar10Model.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private void testResults(ComputationGraph cifar10, DataSetIterator testIterator, int iEpoch, String modelName) throws IOException {
        if (iEpoch % TEST_INTERVAL == 0) {
            Evaluation eval = cifar10.evaluate(testIterator);
            log.info(eval.stats());
            testIterator.reset();
        }
//        TestModels.TestResult test = TestModels.test(cifar10, modelName);
//        log.info("Test Results >> " + test);
    }
 
Example #23
Source File: KerasModelConverter.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
private static void saveH5File(File modelFile, File outputFolder) {
    try {
        INDArray testShape = Nd4j.zeros(1, 3, 224, 224);
        String modelName = modelFile.getName();
        Method method = null;
        try {
            method = InputType.class.getMethod("setDefaultCNN2DFormat", CNN2DFormat.class);
            method.invoke(null, CNN2DFormat.NCHW);
        } catch (NoSuchMethodException ex) {
            System.err.println("setDefaultCNN2DFormat() not found on InputType class... " +
                    "Are you using the custom built deeplearning4j-nn.jar?");
            System.exit(1);
        }

        if (modelName.contains("EfficientNet")) {
            // Fixes for EfficientNet family of models
            testShape = Nd4j.zeros(1, 224, 224, 3);
            method.invoke(null, CNN2DFormat.NHWC);
            // We don't want the resulting .zip files to have 'Fixed' in the name, so we'll strip it off here
            modelName = modelName.replace("Fixed", "");
        }
        ComputationGraph kerasModel = KerasModelImport.importKerasModelAndWeights(modelFile.getAbsolutePath());
        kerasModel.feedForward(testShape, false);
        // e.g. ResNet50.h5 -> KerasResNet50.zip
        modelName = "Keras" + modelName.replace(".h5", ".zip");
        String newZip = Paths.get(outputFolder.getPath(), modelName).toString();
        kerasModel.save(new File(newZip));
        System.out.println("Saved file " + newZip);
    } catch (Exception e) {
        System.err.println("\n\nCouldn't save " + modelFile.getName());
        e.printStackTrace();
    }
}
 
Example #24
Source File: Keras1ModelConfigurationTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private void runModelConfigTest(String path) throws Exception {
    try(InputStream is = Resources.asStream(path)) {
        ComputationGraphConfiguration config =
                new KerasModel().modelBuilder().modelJsonInputStream(is)
                        .enforceTrainingConfig(true).buildModel().getComputationGraphConfiguration();
        ComputationGraph model = new ComputationGraph(config);
        model.init();
    }
}
 
Example #25
Source File: ElementWiseVertexTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testElementWiseVertexForwardProduct() {
    int batchsz = 24;
    int featuresz = 17;
    ComputationGraphConfiguration cgc = new NeuralNetConfiguration.Builder().graphBuilder()
                    .addInputs("input1", "input2", "input3")
                    .addLayer("denselayer",
                                    new DenseLayer.Builder().nIn(featuresz).nOut(1).activation(Activation.IDENTITY)
                                                    .build(),
                                    "input1")
                    /* denselayer is not actually used, but it seems that you _need_ to have trainable parameters, otherwise, you get
                     * Invalid shape: Requested INDArray shape [1, 0] contains dimension size values < 1 (all dimensions must be 1 or more)
                     * at org.nd4j.linalg.factory.Nd4j.checkShapeValues(Nd4j.java:4877)
                     * at org.nd4j.linalg.factory.Nd4j.create(Nd4j.java:4867)
                     * at org.nd4j.linalg.factory.Nd4j.create(Nd4j.java:4820)
                     * at org.nd4j.linalg.factory.Nd4j.create(Nd4j.java:3948)
                     * at org.deeplearning4j.nn.graph.ComputationGraph.init(ComputationGraph.java:409)
                     * at org.deeplearning4j.nn.graph.ComputationGraph.init(ComputationGraph.java:341)
                     */
                    .addVertex("elementwiseProduct", new ElementWiseVertex(ElementWiseVertex.Op.Product), "input1",
                                    "input2", "input3")
                    .addLayer("Product", new ActivationLayer.Builder().activation(Activation.IDENTITY).build(),
                                    "elementwiseProduct")
                    .setOutputs("Product", "denselayer").build();

    ComputationGraph cg = new ComputationGraph(cgc);
    cg.init();


    INDArray input1 = Nd4j.rand(batchsz, featuresz);
    INDArray input2 = Nd4j.rand(batchsz, featuresz);
    INDArray input3 = Nd4j.rand(batchsz, featuresz);

    INDArray target = input1.dup().muli(input2).muli(input3);

    INDArray output = cg.output(input1, input2, input3)[0];
    INDArray squared = output.sub(target.castTo(output.dataType()));
    double rms = squared.mul(squared).sumNumber().doubleValue();
    Assert.assertEquals(0.0, rms, this.epsilon);
}
 
Example #26
Source File: BaseStatsListener.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private void updateExamplesMinibatchesCounts(Model model) {
    ModelInfo modelInfo = getModelInfo(model);
    int examplesThisMinibatch = 0;
    if (model instanceof MultiLayerNetwork) {
        examplesThisMinibatch = ((MultiLayerNetwork) model).batchSize();
    } else if (model instanceof ComputationGraph) {
        examplesThisMinibatch = ((ComputationGraph) model).batchSize();
    } else if (model instanceof Layer) {
        examplesThisMinibatch = ((Layer) model).getInputMiniBatchSize();
    }
    modelInfo.examplesSinceLastReport += examplesThisMinibatch;
    modelInfo.totalExamples += examplesThisMinibatch;
    modelInfo.minibatchesSinceLastReport++;
    modelInfo.totalMinibatches++;
}
 
Example #27
Source File: KerasWeightSettingTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private ComputationGraph loadComputationalGraph(String modelPath, boolean training) throws Exception {
    File modelFile = createTempFile("temp", ".h5");
    try(InputStream is = Resources.asStream(modelPath)) {
        Files.copy(is, modelFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        return new KerasModel().modelBuilder().modelHdf5Filename(modelFile.getAbsolutePath())
                .enforceTrainingConfig(training).buildModel().getComputationGraph();
    }
}
 
Example #28
Source File: Dl4jXception.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ComputationGraph init(int numLabels, long seed, int[] shape, boolean filterMode) {
  org.deeplearning4j.zoo.model.Xception net = org.deeplearning4j.zoo.model.Xception.builder()
      .cacheMode(CacheMode.NONE)
      .workspaceMode(Preferences.WORKSPACE_MODE)
      .inputShape(shape)
      .numClasses(numLabels)
      .build();

  ComputationGraph defaultNet = net.init();

  setRequiresPooling(true);

  return attemptToLoadWeights(net, defaultNet, seed, numLabels, filterMode);
}
 
Example #29
Source File: TestModels.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private static INDArray getEmbeddings(ComputationGraph vgg16, File image) throws IOException {
    INDArray indArray = LOADER.asMatrix(image);
    IMAGE_PRE_PROCESSOR.preProcess(indArray);
    Map<String, INDArray> stringINDArrayMap = vgg16.feedForward(indArray, false);
    INDArray embeddings = stringINDArrayMap.get("embeddings");
    return embeddings;
}
 
Example #30
Source File: TestMasking.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRnnCnnMaskingSimple(){
    int kernelSize1 = 2;
    int padding = 0;
    int cnnStride1 = 1;
    int channels = 1;

    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder()
            .seed(12345)
            .weightInit(WeightInit.XAVIER)
            .convolutionMode(ConvolutionMode.Same)
            .graphBuilder()
            .addInputs("inputs")
            .addLayer("cnn1",
                    new ConvolutionLayer.Builder(new int[] { kernelSize1, kernelSize1 },
                            new int[] { cnnStride1, cnnStride1 },
                            new int[] { padding, padding })
                            .nIn(channels)
                            .nOut(2).build(), "inputs")
            .addLayer("lstm1", new LSTM.Builder().nIn(7 * 7 * 2).nOut(2).build(), "cnn1")
            .addLayer("output", new RnnOutputLayer.Builder(LossFunctions.LossFunction.MSE)
                    .activation(Activation.RELU).nIn(2).nOut(2).build(), "lstm1")
            .setOutputs("output")
            .setInputTypes(InputType.recurrent(7*7, 1))
            .inputPreProcessor("cnn1", new RnnToCnnPreProcessor(7, 7, channels))
            .inputPreProcessor("lstm1", new CnnToRnnPreProcessor(7, 7, 2))
            .build();

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

    cg.fit(new DataSet(
            Nd4j.create(1, 7*7, 5),
            Nd4j.create(1, 2, 5),
            Nd4j.ones(1, 5),
            Nd4j.ones(1, 5)));
}