Created
September 3, 2018 07:16
-
-
Save liweigu/2e240e3532a8478080f291d9e370efa7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ComputationGraph getComputationGraph(){ | |
ComputationGraph multiLayerNetwork; | |
NeuralNetConfiguration.Builder builder = new NeuralNetConfiguration.Builder(); | |
builder.seed(140); | |
builder.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT); | |
builder.weightInit(WeightInit.XAVIER); | |
Map<Integer, Double> lrSchedule = new HashMap<>(); | |
lrSchedule.put(0, 1e-2); | |
lrSchedule.put(400, 1e-3); | |
lrSchedule.put(3000, 1e-4); | |
MapSchedule mapSchedule = new MapSchedule(ScheduleType.ITERATION, lrSchedule); | |
builder.updater(new AdaGrad(mapSchedule)); | |
int lstmHiddenCount = 200; | |
int cnnStride1 = 5; | |
int kernelSize1 = 3; | |
int cnnStride3 = 5; | |
int kernelSize3 = 3; | |
int channels = 1; | |
int padding = 1; | |
int samplingSize = 1; | |
int samplingStride = 1; | |
int cnn1Output = (501 - kernelSize1 + padding) / cnnStride1 + 1; | |
int cnn2Output = (cnn1Output - samplingSize + 0) / samplingStride + 1; | |
int cnn3Output = (cnn2Output - kernelSize3 + padding) / cnnStride3 + 1; | |
int lstmInWidth = cnn3Output; | |
int cnn4Output = (cnn3Output - samplingSize + 0) / samplingStride + 1; | |
int cnn5Output = (cnn4Output - kernelSize3 + padding) / cnnStride3 + 1; | |
lstmInWidth = cnn5Output + 1; // output of cnn | |
Map<String, InputPreProcessor> inputPreProcessors = new HashMap<String, InputPreProcessor>(); | |
inputPreProcessors.put("cnn1", new RnnToCnnPreProcessor(501, 501, channels)); | |
inputPreProcessors.put("lstm1", new CnnToRnnPreProcessor(lstmInWidth, lstmInWidth, 128)); | |
GraphBuilder graphBuilder = builder.graphBuilder().pretrain(false).backprop(true) | |
.backpropType(BackpropType.Standard) | |
.addInputs("inputs") | |
// cnn | |
.addLayer("cnn1", | |
new ConvolutionLayer.Builder(new int[] { kernelSize1, kernelSize1 }, | |
new int[] { cnnStride1, cnnStride1 }, | |
new int[] { padding, padding }) | |
.nIn(channels) | |
.nOut(64) | |
.gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue) | |
.gradientNormalizationThreshold(10) | |
.updater(new AdaGrad(mapSchedule)) | |
.weightInit(WeightInit.RELU) | |
.activation(Activation.RELU).build(), "inputs") | |
// Output: (501 - kernelSize + padding) / cnn1Stride + 1 = 125 --> x * x * nOut = paramsNum | |
.addLayer("cnn2", new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX, | |
new int[] { samplingSize, samplingSize }, new int[] { samplingStride, samplingStride }).build(), "cnn1") | |
// Output: (125-1+0)/1+1 = 125 | |
.addLayer("cnn3", | |
new ConvolutionLayer.Builder(new int[] { kernelSize3, kernelSize3 }, | |
new int[] { cnnStride3, cnnStride3 }, | |
new int[] { padding, padding }) | |
.nIn(64) | |
.nOut(128) | |
.gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue) | |
.gradientNormalizationThreshold(10) | |
.updater(new AdaGrad(mapSchedule)) | |
.weightInit(WeightInit.RELU) | |
.activation(Activation.RELU).build(), "cnn2"); | |
// Output: (125 - kernelSize + padding) / cnn3Stride + 1 = 25 --> x * x * 100 = ? | |
graphBuilder = graphBuilder.addLayer("cnn4", new SubsamplingLayer.Builder( | |
SubsamplingLayer.PoolingType.MAX, | |
new int[] { samplingSize, samplingSize }, | |
new int[] { samplingStride, samplingStride }).build(), "cnn3") | |
.addLayer("cnn5", | |
new ConvolutionLayer.Builder(new int[] { kernelSize3, kernelSize3 }, | |
new int[] { cnnStride3, cnnStride3 }, | |
new int[] { padding, padding }) | |
.nIn(128) | |
.nOut(128) | |
.updater(new AdaGrad(mapSchedule)) | |
.weightInit(WeightInit.RELU) | |
.activation(Activation.RELU).build(), "cnn4"); | |
graphBuilder = graphBuilder.addLayer("lstm1", new LSTM.Builder() | |
.activation(Activation.SOFTSIGN) | |
.nIn(lstmInWidth * lstmInWidth * 128) | |
.nOut(lstmHiddenCount) | |
.gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue) | |
.gradientNormalizationThreshold(10) | |
.updater(new AdaGrad(mapSchedule)) | |
.build(), "cnn5"); | |
graphBuilder = graphBuilder.addVertex("thoughtVector", new LastTimeStepVertex("inputs"), "lstm1"); | |
graphBuilder = graphBuilder.addVertex("dup", new DuplicateToTimeSeriesVertex("inputs"), "thoughtVector"); | |
graphBuilder = graphBuilder.addLayer("lstmDecode1", new LSTM.Builder() | |
.activation(Activation.SOFTSIGN) | |
.nIn(lstmHiddenCount) | |
.nOut(lstmHiddenCount) | |
.gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue) | |
.gradientNormalizationThreshold(10) | |
.updater(new AdaGrad(mapSchedule)) | |
.build(), "dup") | |
.addLayer("output", new RnnOutputLayer | |
.Builder(LossFunctions.LossFunction.MSE) | |
.activation(Activation.RELU) | |
.nIn(lstmHiddenCount) | |
.nOut(501 * 501) | |
.gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue) | |
.gradientNormalizationThreshold(10) | |
.updater(new AdaGrad(mapSchedule)) | |
.build(), "lstmDecode1"); | |
graphBuilder = graphBuilder.setOutputs("output"); | |
graphBuilder.setInputPreProcessors(inputPreProcessors); | |
int inputSize = 30 * 2; | |
graphBuilder.setInputTypes(InputType.recurrent(501 * 501, inputSize)); | |
multiLayerNetwork = new ComputationGraph(graphBuilder.build()); | |
multiLayerNetwork.init(); | |
return multiLayerNetwork; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment