public static MultiLayerConfiguration lenetModelConf() { MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .seed(seed) .l2(0.005) .activation(Activation.RELU) .weightInit(WeightInit.XAVIER) .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT) .updater(new Nesterovs(0.0001, 0.9)) .list() .layer(0, new ConvolutionLayer.Builder(new int[]{5, 5}, new int[]{1, 1}, new int[]{0, 0}).name("cnn1") .nIn(channels).nOut(50).biasInit(0).build()) .layer(1, new SubsamplingLayer.Builder(new int[]{2,2}, new int[]{2,2}).name("maxpool1").build()) .layer(2, new ConvolutionLayer.Builder(new int[]{5,5}, new int[]{5, 5}, new int[]{1, 1}).name("cnn2") .nOut(100).biasInit(0).build()) .layer(3, new SubsamplingLayer.Builder(new int[]{2,2}, new int[]{2,2}).name("maxpool2").build()) .layer(4, new DenseLayer.Builder().nOut(500).build()) .layer(5, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) .nOut(4) .activation(Activation.SOFTMAX) .build()) .backprop(true).pretrain(false) .setInputType(InputType.convolutional(height, width, channels)) .build(); return conf; } public static void saveModel(FileSystem fs, Model model ) throws Exception{
.activation(Activation.SOFTMAX) .build()) .setInputType(InputType.convolutionalFlat(28,28,1)) //See note below .backprop(true).pretrain(false).build(); MultiLayerNetwork model = new MultiLayerNetwork(conf);
@Override public InputType getOutputType(int layerIndex, InputType... vertexInputs) throws InvalidInputTypeException { //Infer output shape from specified shape: switch (newShape.length) { case 2: return InputType.feedForward(newShape[1]); case 3: return InputType.recurrent(newShape[1]); case 4: return InputType.convolutional(newShape[1], newShape[2], newShape[0]); //[mb,d,h,w] for activations default: throw new UnsupportedOperationException( "Cannot infer input type for reshape array " + Arrays.toString(newShape)); } }
@Override public InputType getOutputType(int layerIndex, InputType inputType) { if (inputType == null || (inputType.getType() != InputType.Type.FF && inputType.getType() != InputType.Type.CNNFlat)) { throw new IllegalStateException("Invalid input type (layer index = " + layerIndex + ", layer name=\"" + getLayerName() + "\"): expected FeedForward input type. Got: " + inputType); } return InputType.feedForward(nOut); }
@Override public InputType getOutputType(InputType inputType) { if (inputType == null || (inputType.getType() != InputType.Type.FF && inputType.getType() != InputType.Type.CNNFlat)) { throw new IllegalStateException("Invalid input: expected input of type FeedForward, got " + inputType); } if (inputType.getType() == InputType.Type.FF) { InputType.InputTypeFeedForward ff = (InputType.InputTypeFeedForward) inputType; return InputType.recurrent(ff.getSize()); } else { InputType.InputTypeConvolutionalFlat cf = (InputType.InputTypeConvolutionalFlat) inputType; return InputType.recurrent(cf.getFlattenedSize()); } }
return vertexInputs[0]; InputType first = vertexInputs[0]; if (first.getType() == InputType.Type.CNNFlat) { "Invalid input: MergeVertex cannot currently merge CNN data in flattened format. Got: " + vertexInputs); } else if (first.getType() != InputType.Type.CNN) { if (vertexInputs[i].getType() != first.getType()) { throw new InvalidInputTypeException( "Invalid input: MergeVertex cannot merge activations of different types:" + " first type = " + first.getType() + ", input type " + (i + 1) + " = " + vertexInputs[i].getType()); switch (vertexInputs[i].getType()) { case FF: thisSize = ((InputType.InputTypeFeedForward) vertexInputs[i]).getSize(); return InputType.feedForward(size); else return InputType.recurrent(size); } else { return InputType.feedForward(-1); else return InputType.recurrent(-1); if (vertexInputs[i].getType() != InputType.Type.CNN) { throw new InvalidInputTypeException(
@Override public InputType getOutputType(int layerIndex, InputType inputType) { switch (inputType.getType()) { case FF: throw new UnsupportedOperationException( if (collapseDimensions) { return InputType.feedForward(recurrent.getSize()); } else { InputType.InputTypeConvolutional conv = (InputType.InputTypeConvolutional) inputType; if (collapseDimensions) { return InputType.feedForward(conv.getDepth()); } else { return InputType.convolutional(1, 1, conv.getDepth()); return InputType.feedForward(convFlat.getDepth()); } else { return InputType.convolutional(1, 1, convFlat.getDepth());
@Override public InputType getOutputType(InputType inputType) { if (inputType == null || inputType.getType() != InputType.Type.RNN) { throw new IllegalStateException("Invalid input type: Expected input of type RNN, got " + inputType); } InputType.InputTypeRecurrent c = (InputType.InputTypeRecurrent) inputType; int expSize = inputHeight * inputWidth * numChannels; if (c.getSize() != expSize) { throw new IllegalStateException("Invalid input: expected RNN input of size " + expSize + " = (d=" + numChannels + " * w=" + inputWidth + " * h=" + inputHeight + "), got " + inputType); } return InputType.convolutional(inputHeight, inputWidth, numChannels); }
private InputType[] getInputTypes(DomainDescriptor domainDescriptor) { String[] inputNames = getInputNames(); InputType[] inputTypes = new InputType[inputNames.length]; for (int i = 0; i < inputNames.length; i++) { switch(inputNames[i]) { case "input": inputTypes[i] = InputType.feedForward(domainDescriptor.getNumInputs(inputNames[i])[0]); break; case "indel": case "trueGenotypeInput": inputTypes[i] = InputType.recurrent(domainDescriptor.getNumInputs(inputNames[i])[0]); break; default: throw new RuntimeException("Invalid input to computation graph"); } } return inputTypes; }
@Override public InputType getOutputType(int layerIndex, InputType inputType) { if (inputType == null || inputType.getType() != InputType.Type.RNN) { throw new IllegalStateException("Invalid input for Subsampling layer (layer name=\"" + getLayerName() + "\"): Expected RNN input, got " + inputType); } return inputType; }
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(); } }
public static InputType[] inferInputTypes(INDArray... inputArrays) { InputType[] out = new InputType[inputArrays.length]; for (int i = 0; i < inputArrays.length; i++) { out[i] = inferInputType(inputArrays[i]); } return out; }
@Override public InputType getOutputType(int layerIndex, InputType... vertexInputs) throws InvalidInputTypeException { return InputType.feedForward(1); }
@Override public InputType getOutputType(int layerIndex, InputType... vertexInputs) throws InvalidInputTypeException { if (vertexInputs.length != 1) { throw new InvalidInputTypeException( "SubsetVertex expects single input type. Received: " + Arrays.toString(vertexInputs)); } switch (vertexInputs[0].getType()) { case FF: return InputType.feedForward(to - from + 1); case RNN: return InputType.recurrent(to - from + 1); case CNN: InputType.InputTypeConvolutional conv = (InputType.InputTypeConvolutional) vertexInputs[0]; int depth = conv.getDepth(); if (to >= depth) { throw new InvalidInputTypeException("Invalid range: Cannot select depth subset [" + from + "," + to + "] inclusive from CNN activations with " + " [depth,width,height] = [" + depth + "," + conv.getWidth() + "," + conv.getHeight() + "]"); } return InputType.convolutional(conv.getHeight(), conv.getWidth(), from - to + 1); case CNNFlat: //TODO work out how to do this - could be difficult... throw new UnsupportedOperationException( "Subsetting data in flattened convolutional format not yet supported"); default: throw new RuntimeException("Unknown input type: " + vertexInputs[0]); } }
@Override public InputType getOutputType(int layerIndex, InputType inputType) { if (inputType == null || inputType.getType() != InputType.Type.RNN) { throw new IllegalStateException("Invalid input for 1D CNN layer (layer index = " + layerIndex + ", layer name = \"" + getLayerName() + "\"): expect RNN input type with size > 0. Got: " + inputType); } return InputType.recurrent(nOut); }
@Override public InputType getOutputType(InputType inputType) { switch (inputType.getType()) { case FF: InputType.InputTypeFeedForward c = (InputType.InputTypeFeedForward) inputType; + inputType); return InputType.convolutional(inputHeight, inputWidth, numChannels); case CNN: InputType.InputTypeConvolutional c2 = (InputType.InputTypeConvolutional) inputType;
private InputType[] getInputTypes(DomainDescriptor domainDescriptor) { String[] inputNames = getInputNames(); InputType[] inputTypes = new InputType[inputNames.length]; for (int i = 0; i < inputNames.length; i++) { switch (inputNames[i]) { case "input": inputTypes[i] = InputType.feedForward(domainDescriptor.getNumInputs(inputNames[i])[0]); break; case "from": case "G1": case "G2": case "G3": case "trueGenotypeInput": inputTypes[i] = InputType.recurrent(domainDescriptor.getNumInputs(inputNames[i])[0]); break; default: throw new RuntimeException("Invalid input to computation graph"); } } return inputTypes; }
@Override public InputType getOutputType(int layerIndex, InputType inputType) { if (inputType == null || inputType.getType() != InputType.Type.CNN) { throw new IllegalStateException( "Invalid input type for LRN layer (layer index = " + layerIndex + ", layer name = \"" + getLayerName() + "\"): Expected input of type CNN, got " + inputType); } return inputType; }
@Override public InputType getOutputType(InputType inputType) { if (inputType == null || inputType.getType() != InputType.Type.RNN) { throw new IllegalStateException("Invalid input: expected input of type RNN, got " + inputType); } InputType.InputTypeRecurrent rnn = (InputType.InputTypeRecurrent) inputType; return InputType.feedForward(rnn.getSize()); }
private InputType getInputTypes(DomainDescriptor domainDescriptor) { final MappedDimensions inputDimensions = domainDescriptor.getFeatureMapper("input").dimensions(); System.out.printf("GenotypeSegmentsLSTM dimensions: sequence-length=%d, num-float-per-base=%d%n", inputDimensions.numElements(1), inputDimensions.numElements(2)); return InputType.recurrent(inputDimensions.numElements(1), inputDimensions.numElements(2)); }