public static ComputationGraphConfiguration getConf() { ComputationGraphConfiguration.GraphBuilder builder = new NeuralNetConfiguration.Builder() .seed(12345) .updater(new Adam(0.01)) .weightInit(WeightInit.RELU) .graphBuilder() .addInputs("in"); String[] poolNames = new String[ngramFilters.length]; int i = 0; for (int ngram : ngramFilters) { String filterName = String.format("ngram%d", ngram); poolNames[i] = String.format("pool%d", ngram); builder = builder.addLayer(filterName, new Convolution1DLayer.Builder() .nOut(numFilters) .kernelSize(ngram) .activation(Activation.RELU) .build(), "in") .addLayer(poolNames[i], new GlobalPoolingLayer.Builder(PoolingType.MAX).build(), filterName); i++; } return builder.addVertex("concat", new MergeVertex(), poolNames) .addLayer("predict", new DenseLayer.Builder().nOut(numClasses).dropOut(dropoutRetain) .activation(Activation.SOFTMAX).build(), "concat") .addLayer("loss", new LossLayer.Builder(LossFunctions.LossFunction.MCXENT).build(), "predict") .setOutputs("loss") .setInputTypes(InputType.recurrent(W2V_VECTOR_SIZE, 1000)) .build(); } }
.nOut(cnnLayerFeatureMaps) .build(), "input") .addVertex("merge", new MergeVertex(), "cnn3", "cnn4", "cnn5") //Perform depth concatenation .addLayer("globalPool", new GlobalPoolingLayer.Builder() .poolingType(globalPoolingType)
/** * Add a {@link GraphVertex} to the network configuration. A GraphVertex defines forward and backward pass methods, * and can contain a {@link LayerVertex}, a {@link org.deeplearning4j.nn.conf.graph.ElementWiseVertex} to do element-wise * addition/subtraction, a {@link MergeVertex} to combine/concatenate the activations out of multiple layers or vertices, * a {@link org.deeplearning4j.nn.conf.graph.SubsetVertex} to select a subset of the activations out of another layer/GraphVertex.<br> * Custom GraphVertex objects (that extend the abstract {@link GraphVertex} class) may also be used. * * @param vertexName The name of the GraphVertex to add * @param vertex The GraphVertex to add * @param vertexInputs The inputs/activations to this GraphVertex */ public GraphBuilder addVertex(String vertexName, GraphVertex vertex, String... vertexInputs) { vertices.put(vertexName, vertex); //Automatically insert a MergeNode if this vertex can only take 1 input (layer vertices, etc) if (vertex.maxVertexInputs() == 1 && vertexInputs != null && vertexInputs.length > 1) { String mergeName = vertexName + "-merge"; addVertex(mergeName, new MergeVertex(), vertexInputs); this.vertexInputs.put(vertexName, Collections.singletonList(mergeName)); } else if (vertexInputs != null) { this.vertexInputs.put(vertexName, Arrays.asList(vertexInputs)); } return this; }
nameLayer(blockName, "cnn6", i)) .addVertex(nameLayer(blockName, "merge1", i), new MergeVertex(), nameLayer(blockName, "batch1", i), nameLayer(blockName, "batch3", i), nameLayer(blockName, "batch6", i)) .nOut(192).build(), nameLayer(blockName, "cnn7", i)) .addVertex(nameLayer(blockName, "scaling", i), new ScaleVertex(activationScale), nameLayer(blockName, "batch7", i)) new ActivationLayer.Builder().activation(Activation.IDENTITY).build(), previousBlock) .addVertex(nameLayer(blockName, "shortcut", i), new ElementWiseVertex(ElementWiseVertex.Op.Add), nameLayer(blockName, "scaling", i),
nameLayer(blockName, "cnn4", i)) .addVertex(nameLayer(blockName, "merge1", i), new MergeVertex(), nameLayer(blockName, "batch1", i), nameLayer(blockName, "batch4", i)) .addLayer(nameLayer(blockName, "cnn5", i), .nOut(576).build(), nameLayer(blockName, "cnn5", i)) .addVertex(nameLayer(blockName, "scaling", i), new ScaleVertex(activationScale), nameLayer(blockName, "batch5", i)) new ActivationLayer.Builder().activation(Activation.IDENTITY).build(), previousBlock) .addVertex(nameLayer(blockName, "shortcut", i), new ElementWiseVertex(ElementWiseVertex.Op.Add), nameLayer(blockName, "scaling", i),
graph.addVertex(getModuleName(moduleLayerName), new MergeVertex(), getModuleName(moduleLayerName) + "-transfer2-0", getModuleName(moduleLayerName) + "-transfer3", getModuleName(moduleLayerName) + "-transfer4"); } else if (kernelSize.length == 2 && reduceSize.length == 2) { graph.addVertex(getModuleName(moduleLayerName), new MergeVertex(), getModuleName(moduleLayerName) + "-transfer2-0", getModuleName(moduleLayerName) + "-transfer2-1"); } else if (kernelSize.length == 2 && reduceSize.length == 3) { graph.addVertex(getModuleName(moduleLayerName), new MergeVertex(), getModuleName(moduleLayerName) + "-transfer2-0", getModuleName(moduleLayerName) + "-transfer2-1", getModuleName(moduleLayerName) + "-transfer3"); } else if (kernelSize.length == 2 && reduceSize.length == 4) { graph.addVertex(getModuleName(moduleLayerName), new MergeVertex(), getModuleName(moduleLayerName) + "-transfer2-0", getModuleName(moduleLayerName) + "-transfer2-1",
nameLayer(blockName, "cnn4", i)) .addVertex(nameLayer(blockName, "merge1", i), new MergeVertex(), nameLayer(blockName, "batch1", i), nameLayer(blockName, "batch4", i)) .addLayer(nameLayer(blockName, "cnn5", i), .activation(Activation.TANH).nIn(1344).nOut(1344).build(), nameLayer(blockName, "cnn5", i)) .addVertex(nameLayer(blockName, "scaling", i), new ScaleVertex(activationScale), nameLayer(blockName, "batch5", i)) new ActivationLayer.Builder().activation(Activation.IDENTITY).build(), previousBlock) .addVertex(nameLayer(blockName, "shortcut", i), new ElementWiseVertex(ElementWiseVertex.Op.Add), nameLayer(blockName, "scaling", i),
graphBuilder.addVertex(layer.getLayerName(), layer.getVertex(), inboundLayerNamesArray); if (this.outputLayerNames.contains(layer.getLayerName()) && !(layer.getVertex() instanceof IOutputLayer)) throw new UnsupportedKerasConfigurationException("Layer " + layer.getLayerName() + " could not be mapped to Layer, Vertex, or InputPreProcessor"); graphBuilder.addVertex(layer.getLayerName(), new PreprocessorVertex(preprocessor), inboundLayerNamesArray);
.build(), lstmPreviousLayerName); build.addVertex("lstm" + lstmInputName + "LastTimeStepVertex", new LastTimeStepVertex(lstmInputName), lstmLayerName); build.addVertex("lstmFeedForwardMerge", new MergeVertex(), mergeInputs); int numInputsToDenseAfterMerge = numInputs + (lstmInputNames.length * numLSTMIndelHiddenNodes); assembler.assemble(numInputsToDenseAfterMerge, numHiddenNodes,
.build(), lstmPreviousLayerName); build.addVertex("lstmindelLastTimeStepVertex", new LastTimeStepVertex("indel"), lstmLayerName); String[] mergeInputs = new String[lstmInputNames.length + 1]; build.addVertex("lstmFeedForwardMerge", new MergeVertex(), mergeInputs); int numInputsToDenseAfterMerge = numInputs + (lstmInputNames.length * numLSTMIndelHiddenNodes); assembler.assemble(numInputsToDenseAfterMerge, numHiddenNodes,
gb.addVertex(mergeVertexName, new MergeVertex(), names); currentInput = mergeVertexName;
numReductionLayerInputNodes = numReductionLayerOutputNodes; build.addVertex("feedForwardLstmDuplicate", new DuplicateToTimeSeriesVertex("trueGenotypeInput"), reductionLayerName); String lstmLayerName = "no layer"; for (int i = 0; i < numLSTMLayers; i++) {
.addVertex(shortcutName, new ElementWiseVertex(ElementWiseVertex.Op.Add), batchName + "2c", batchName + "1") .addLayer(convName, new ActivationLayer.Builder().activation(Activation.RELU).build(),
.addLayer(batchName + "2c", new BatchNormalization(), convName + "2c") .addVertex(shortcutName, new ElementWiseVertex(ElementWiseVertex.Op.Add), batchName + "2c", input) .addLayer(convName, new ActivationLayer.Builder().activation(Activation.RELU).build(),
@Override public GraphConfiguration getValue(double[] values) { //Create ComputationGraphConfiguration... NeuralNetConfiguration.Builder builder = randomGlobalConf(values); ComputationGraphConfiguration.GraphBuilder graphBuilder = builder.graphBuilder(); graphBuilder.addInputs(this.networkInputs); graphBuilder.setOutputs(this.networkOutputs); if (inputTypes != null) graphBuilder.setInputTypes(inputTypes.getValue(values)); //Build/add our layers and vertices: for (LayerConf c : layerSpaces) { org.deeplearning4j.nn.conf.layers.Layer l = c.layerSpace.getValue(values); graphBuilder.addLayer(c.getLayerName(), l, c.getInputs()); } for (VertexConf gv : vertices) { graphBuilder.addVertex(gv.getVertexName(), gv.getGraphVertex(), gv.getInputs()); } if (backprop != null) graphBuilder.backprop(backprop.getValue(values)); if (pretrain != null) graphBuilder.pretrain(pretrain.getValue(values)); if (backpropType != null) graphBuilder.backpropType(backpropType.getValue(values)); if (tbpttFwdLength != null) graphBuilder.tBPTTForwardLength(tbpttFwdLength.getValue(values)); if (tbpttBwdLength != null) graphBuilder.tBPTTBackwardLength(tbpttBwdLength.getValue(values)); ComputationGraphConfiguration configuration = graphBuilder.build(); return new GraphConfiguration(configuration, earlyStoppingConfiguration, numEpochs); }
@Override public ComputationGraph init() { int embeddingSize = 128; ComputationGraphConfiguration.GraphBuilder graph = graphBuilder("input1"); graph.addInputs("input1").setInputTypes(InputType.convolutional(inputShape[2], inputShape[1], inputShape[0])) // Logits .addLayer("bottleneck", new DenseLayer.Builder().nIn(5376).nOut(embeddingSize).build(), "avgpool") // Embeddings .addVertex("embeddings", new L2NormalizeVertex(new int[] {1}, 1e-10), "bottleneck") // Output .addLayer("outputLayer", new CenterLossOutputLayer.Builder() .lossFunction(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) .activation(Activation.SOFTMAX).alpha(0.9).lambda(1e-4) .nIn(embeddingSize).nOut(numClasses).build(), "embeddings") .setOutputs("outputLayer").backprop(true).pretrain(false); ComputationGraphConfiguration conf = graph.build(); ComputationGraph model = new ComputationGraph(conf); model.init(); return model; }
/** * Add a layer and an {@link InputPreProcessor}, with the specified name and specified inputs. * * @param layerName Name/label of the layer to add * @param layer The layer configuration * @param preProcessor The InputPreProcessor to use with this layer. * @param layerInputs Inputs to this layer (must be 1 or more). Inputs may be other layers, GraphVertex objects, * on a combination of the two. */ public GraphBuilder addLayer(String layerName, Layer layer, InputPreProcessor preProcessor, String... layerInputs) { NeuralNetConfiguration.Builder builder = globalConfiguration.clone(); builder.layer(layer); addVertex(layerName, new LayerVertex(builder.build(), preProcessor), layerInputs); layer.setLayerName(layerName); return this; }
/** * Add a vertex of the given configuration to the computation graph * @param vertexName * @param vertex * @param vertexInputs * @return */ public GraphBuilder addVertex(String vertexName, GraphVertex vertex, String... vertexInputs) { initBuilderIfReq(); editedConfigBuilder.addVertex(vertexName, vertex, vertexInputs); editedVertices.add(vertexName); return this; }
private GraphBuilder inception(GraphBuilder graph, String name, int inputSize, int[][] config, String inputLayer) { graph.addLayer(name + "-cnn1", conv1x1(inputSize, config[0][0], 0.2), inputLayer) .addLayer(name + "-cnn2", c3x3reduce(inputSize, config[1][0], 0.2), inputLayer) .addLayer(name + "-cnn3", c5x5reduce(inputSize, config[2][0], 0.2), inputLayer) .addLayer(name + "-max1", maxPool3x3(1), inputLayer) .addLayer(name + "-cnn4", conv3x3(config[1][0], config[1][1], 0.2), name + "-cnn2") .addLayer(name + "-cnn5", conv5x5(config[2][0], config[2][1], 0.2), name + "-cnn3") .addLayer(name + "-cnn6", conv1x1(inputSize, config[3][0], 0.2), name + "-max1") .addVertex(name + "-depthconcat1", new MergeVertex(), name + "-cnn1", name + "-cnn4", name + "-cnn5", name + "-cnn6"); return graph; }