Skip to content

Instantly share code, notes, and snippets.

@yptheangel
Last active April 6, 2020 03:18
Show Gist options
  • Save yptheangel/8153c3daf28d3167a53eb4131a4b50e8 to your computer and use it in GitHub Desktop.
Save yptheangel/8153c3daf28d3167a53eb4131a4b50e8 to your computer and use it in GitHub Desktop.
Flatten in Eclipse Deeplearning4j, For example to flatten Rank 4 tensor to Rank 2 (batch size, nIn) input to the Dense Layer.
// Take note this config is just an example
// Take note that my input is a Rank 4 tensor, a DataSetIterator that uses ImageRecordReader
// Method 1: using .setInputTypes(InputType.convolutional(height,width,channels))
ComputationGraphConfiguration config = new NeuralNetConfiguration.Builder()
.seed(seed)
.updater(new RmsProp(1e-3))
.weightInit(WeightInit.XAVIER)
.l2(1e-4)
.graphBuilder()
.addInputs("input")
.layer("vae",new VariationalAutoencoder.Builder()
.activation(Activation.LEAKYRELU)
.encoderLayerSizes(20,10) //1 encoder layer with 20 nodes
.decoderLayerSizes(5,15) //2 decoder layers with 15 and 5 nodes respectively
.pzxActivationFunction(Activation.IDENTITY) //p(z|data) activation function
.reconstructionDistribution(new BernoulliReconstructionDistribution(Activation.SIGMOID.getActivationFunction())) //Bernoulli distribution for p(data|z) (binary or 0 to 1 data only)
.nIn(width*height*channels)
.nOut(2) //Size of the latent variable space: p(z|x). 2 dimensions here for plotting, use more in general
.build(),"input")
.setInputTypes(InputType.convolutional(height,width,channels))
.setOutputs("vae")
.build();
// Method 2: add a CnnToFeedForwardPreprocessor as a vertex, your network must be a ComputationGraph
// .addVertex("flatten",new PreprocessorVertex(new CnnToFeedForwardPreProcessor(height,width,channels)),"input")
ComputationGraphConfiguration config = new NeuralNetConfiguration.Builder()
.seed(seed)
.updater(new RmsProp(1e-3))
.weightInit(WeightInit.XAVIER)
.l2(1e-4)
.graphBuilder()
.addInputs("input")
.addVertex("flatten",new PreprocessorVertex(new CnnToFeedForwardPreProcessor(height,width,channels)),"input")
.layer("vae",new VariationalAutoencoder.Builder()
.activation(Activation.LEAKYRELU)
.encoderLayerSizes(20,10) //1 encoder layer with 20 nodes
.decoderLayerSizes(5,15) //2 decoder layers with 15 and 5 nodes respectively
.pzxActivationFunction(Activation.IDENTITY) //p(z|data) activation function
.reconstructionDistribution(new BernoulliReconstructionDistribution(Activation.SIGMOID.getActivationFunction())) //Bernoulli distribution for p(data|z) (binary or 0 to 1 data only)
.nIn(width*height*channels)
.nOut(2) //Size of the latent variable space: p(z|x). 2 dimensions here for plotting, use more in general
.build(),"flatten")
.setInputTypes(InputType.convolutional(height,width,channels))
.setOutputs("vae")
.build();
// Reference: https://deeplearning4j.konduit.ai/getting-started/cheat-sheet#input-pre-processors
// I have tried ReshapeVertex but it wont work if batch size is more than 1 because the new shape requires a dynamic size.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment