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{
public static MultiLayerNetwork lenetModel() { /** * Revisde Lenet Model approach developed by ramgo2 achieves slightly above random * Reference: https://gist.github.com/ramgo2/833f12e92359a2da9e5c2fb6333351c5 **/ MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .seed(seed) .l2(0.005) // tried 0.0001, 0.0005 .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 new MultiLayerNetwork(conf); }
public InputType getUnflattenedType() { return InputType.convolutional(height, width, depth); }
@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); }
@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)); } }
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(int layerIndex, InputType inputType) { int inH; int inW; int inDepth; if (inputType instanceof InputType.InputTypeConvolutional) { InputType.InputTypeConvolutional conv = (InputType.InputTypeConvolutional) inputType; inH = conv.getHeight(); inW = conv.getWidth(); inDepth = conv.getDepth(); } else if (inputType instanceof InputType.InputTypeConvolutionalFlat) { InputType.InputTypeConvolutionalFlat conv = (InputType.InputTypeConvolutionalFlat) inputType; inH = conv.getHeight(); inW = conv.getWidth(); inDepth = conv.getDepth(); } else { throw new IllegalStateException( "Invalid input type: expected InputTypeConvolutional or InputTypeConvolutionalFlat." + " Got: " + inputType); } int outH = inH + padding[0] + padding[1]; int outW = inW + padding[2] + padding[3]; return InputType.convolutional(outH, outW, inDepth); }
public static InputType inferInputType(INDArray inputArray) { //Note: ConvolutionalFlat and FeedForward look identical... but either should work OK if using something // like FeedForwardToCnnPreProcessor switch (inputArray.rank()) { case 2: return InputType.feedForward(inputArray.size(1)); case 3: return InputType.recurrent(inputArray.size(1), inputArray.size(2)); case 4: //Order: [minibatch, depth, height, width] -> [h, w, d] return InputType.convolutional(inputArray.size(2), inputArray.size(3), inputArray.size(1)); default: throw new IllegalArgumentException( "Cannot infer input type for array with shape: " + Arrays.toString(inputArray.shape())); } }
@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]); } }
int outW = (int) Math.ceil(inWidth / ((double) stride[1])); return InputType.convolutional(outH, outW, outputDepth); return InputType.convolutional(hOut, wOut, outputDepth);
+ inputType); return InputType.convolutional(inputHeight, inputWidth, numChannels); case CNN: InputType.InputTypeConvolutional c2 = (InputType.InputTypeConvolutional) inputType;
builder.setInputTypes(InputType.convolutional(shape[1], shape[2], shape[0]));
return InputType.convolutional(fh, fw, depthSum);
return InputType.convolutional(fh, fw, depthSum);
@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; }
return InputType.convolutional(fh, fw, depthSum);
return InputType.convolutional(fh, fw, depthSum);
.backprop(true) .pretrain(false) .setInputType(InputType.convolutional(height, width, channels)) .build();
graph.addInputs("input").setInputTypes(InputType.convolutional(inputShape[2], inputShape[1], inputShape[0]))
.layer(32, new ActivationLayer.Builder().activation(Activation.SOFTMAX).build()) .setInputType(InputType.convolutional(inputShape[2], inputShape[1], inputShape[0])) .backprop(true).pretrain(false).build();