/
NeuralNetConfiguration.java
executable file
·1190 lines (1064 loc) · 50.4 KB
/
NeuralNetConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
package org.deeplearning4j.nn.conf;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.deeplearning4j.nn.api.OptimizationAlgorithm;
import org.deeplearning4j.nn.api.layers.LayerConstraint;
import org.deeplearning4j.nn.conf.distribution.Distribution;
import org.deeplearning4j.nn.conf.dropout.Dropout;
import org.deeplearning4j.nn.conf.dropout.IDropout;
import org.deeplearning4j.nn.conf.graph.GraphVertex;
import org.deeplearning4j.nn.conf.inputs.InputType;
import org.deeplearning4j.nn.conf.layers.misc.FrozenLayerWithBackprop;
import org.deeplearning4j.nn.conf.layers.samediff.AbstractSameDiffLayer;
import org.deeplearning4j.nn.conf.layers.variational.ReconstructionDistribution;
import org.deeplearning4j.nn.conf.serde.JsonMappers;
import org.deeplearning4j.nn.conf.layers.*;
import org.deeplearning4j.nn.conf.layers.misc.FrozenLayer;
import org.deeplearning4j.nn.conf.layers.recurrent.Bidirectional;
import org.deeplearning4j.nn.conf.layers.wrapper.BaseWrapperLayer;
import org.deeplearning4j.nn.conf.serde.legacyformat.LegacyGraphVertexDeserializer;
import org.deeplearning4j.nn.conf.serde.legacyformat.LegacyLayerDeserializer;
import org.deeplearning4j.nn.conf.serde.legacyformat.LegacyPreprocessorDeserializer;
import org.deeplearning4j.nn.conf.serde.legacyformat.LegacyReconstructionDistributionDeserializer;
import org.deeplearning4j.nn.conf.stepfunctions.StepFunction;
import org.deeplearning4j.nn.conf.weightnoise.IWeightNoise;
import org.deeplearning4j.nn.weights.WeightInit;
import org.nd4j.base.Preconditions;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.activations.IActivation;
import org.nd4j.linalg.activations.impl.ActivationSigmoid;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.learning.config.IUpdater;
import org.nd4j.linalg.learning.config.Sgd;
import org.nd4j.linalg.lossfunctions.ILossFunction;
import org.nd4j.linalg.primitives.Pair;
import org.nd4j.serde.json.LegacyIActivationDeserializer;
import org.nd4j.serde.json.LegacyILossFunctionDeserializer;
import org.nd4j.shade.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
/**
* A Serializable configuration
* for neural nets that covers per layer parameters
*
* @author Adam Gibson
*/
@Data
@NoArgsConstructor
@Slf4j
@EqualsAndHashCode(exclude = {"iterationCount", "epochCount"})
public class NeuralNetConfiguration implements Serializable, Cloneable {
protected Layer layer;
//batch size: primarily used for conv nets. Will be reinforced if set.
protected boolean miniBatch = true;
//number of line search iterations
protected int maxNumLineSearchIterations;
protected long seed;
protected OptimizationAlgorithm optimizationAlgo;
//gradient keys used for ensuring order when getting and setting the gradient
protected List<String> variables = new ArrayList<>();
//whether to constrain the gradient to unit norm or not
protected StepFunction stepFunction;
//minimize or maximize objective
protected boolean minimize = true;
protected boolean pretrain;
// this field defines preOutput cache
protected CacheMode cacheMode;
//Counter for the number of parameter updates so far for this layer.
//Note that this is only used for pretrain layers (AE, VAE) - MultiLayerConfiguration and ComputationGraphConfiguration
//contain counters for standard backprop training.
// This is important for learning rate schedules, for example, and is stored here to ensure it is persisted
// for Spark and model serialization
protected int iterationCount = 0;
//Counter for the number of epochs completed so far. Used for per-epoch schedules
protected int epochCount = 0;
/**
* Creates and returns a deep copy of the configuration.
*/
@Override
public NeuralNetConfiguration clone() {
try {
NeuralNetConfiguration clone = (NeuralNetConfiguration) super.clone();
if (clone.layer != null)
clone.layer = clone.layer.clone();
if (clone.stepFunction != null)
clone.stepFunction = clone.stepFunction.clone();
if (clone.variables != null)
clone.variables = new ArrayList<>(clone.variables);
return clone;
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
public List<String> variables() {
return new ArrayList<>(variables);
}
public List<String> variables(boolean copy) {
if (copy)
return variables();
return variables;
}
public void addVariable(String variable) {
if (!variables.contains(variable)) {
variables.add(variable);
}
}
public void clearVariables() {
variables.clear();
}
public void setPretrain(boolean pretrain){
this.pretrain = pretrain;
if(layer != null){
layer.setPretrain(pretrain);
}
}
/**
* Fluent interface for building a list of configurations
*/
public static class ListBuilder extends MultiLayerConfiguration.Builder {
private int layerCounter = -1; //Used only for .layer(Layer) method
private Map<Integer, Builder> layerwise;
private Builder globalConfig;
// Constructor
public ListBuilder(Builder globalConfig, Map<Integer, Builder> layerMap) {
this.globalConfig = globalConfig;
this.layerwise = layerMap;
}
public ListBuilder(Builder globalConfig) {
this(globalConfig, new HashMap<Integer, Builder>());
}
@Deprecated
public ListBuilder backprop(boolean backprop) {
this.backprop = backprop;
return this;
}
@Deprecated
public ListBuilder pretrain(boolean pretrain) {
this.pretrain = pretrain;
return this;
}
public ListBuilder layer(int ind, @NonNull Layer layer) {
if (layerwise.containsKey(ind)) {
log.info("Layer index {} already exists, layer of type {} will be replace by layer type {}",
ind, layerwise.get(ind).getClass().getSimpleName(), layer.getClass().getSimpleName());
layerwise.get(ind).layer(layer);
} else {
layerwise.put(ind, globalConfig.clone().layer(layer));
}
if(layerCounter < ind){
//Edge case: user is mixing .layer(Layer) and .layer(int, Layer) calls
//This should allow a .layer(A, X) and .layer(Y) to work such that layer Y is index (A+1)
layerCounter = ind;
}
return this;
}
public ListBuilder layer(Layer layer){
return layer(++layerCounter, layer);
}
public Map<Integer, Builder> getLayerwise() {
return layerwise;
}
@Override
public ListBuilder setInputType(InputType inputType){
return (ListBuilder)super.setInputType(inputType);
}
/**
* A convenience method for setting input types: note that for example .inputType().convolutional(h,w,d)
* is equivalent to .setInputType(InputType.convolutional(h,w,d))
*/
public ListBuilder.InputTypeBuilder inputType(){
return new InputTypeBuilder();
}
/**
* For the (perhaps partially constructed) network configuration, return a list of activation sizes for each
* layer in the network.<br>
* Note: To use this method, the network input type must have been set using {@link #setInputType(InputType)} first
* @return A list of activation types for the network, indexed by layer number
*/
public List<InputType> getLayerActivationTypes(){
Preconditions.checkState(inputType != null, "Can only calculate activation types if input type has" +
"been set. Use setInputType(InputType)");
MultiLayerConfiguration conf;
try{
conf = build();
} catch (Exception e){
throw new RuntimeException("Error calculating layer activation types: error instantiating MultiLayerConfiguration", e);
}
return conf.getLayerActivationTypes(inputType);
}
/**
* Build the multi layer network
* based on this neural network and
* overr ridden parameters
*
* @return the configuration to build
*/
public MultiLayerConfiguration build() {
List<NeuralNetConfiguration> list = new ArrayList<>();
if (layerwise.isEmpty())
throw new IllegalStateException("Invalid configuration: no layers defined");
for (int i = 0; i < layerwise.size(); i++) {
if (layerwise.get(i) == null) {
throw new IllegalStateException("Invalid configuration: layer number " + i
+ " not specified. Expect layer " + "numbers to be 0 to " + (layerwise.size() - 1)
+ " inclusive (number of layers defined: " + layerwise.size() + ")");
}
if (layerwise.get(i).getLayer() == null)
throw new IllegalStateException("Cannot construct network: Layer config for" + "layer with index "
+ i + " is not defined)");
//Layer names: set to default, if not set
if (layerwise.get(i).getLayer().getLayerName() == null) {
layerwise.get(i).getLayer().setLayerName("layer" + i);
}
list.add(layerwise.get(i).build());
}
WorkspaceMode wsmTrain = (globalConfig.setTWM ? globalConfig.trainingWorkspaceMode : trainingWorkspaceMode);
WorkspaceMode wsmTest = (globalConfig.setIWM ? globalConfig.inferenceWorkspaceMode : inferenceWorkspaceMode);
return new MultiLayerConfiguration.Builder().backprop(backprop).inputPreProcessors(inputPreProcessors)
.pretrain(pretrain).backpropType(backpropType).tBPTTForwardLength(tbpttFwdLength)
.tBPTTBackwardLength(tbpttBackLength).setInputType(this.inputType)
.trainingWorkspaceMode(wsmTrain).cacheMode(globalConfig.cacheMode)
.inferenceWorkspaceMode(wsmTest).confs(list).validateOutputLayerConfig(validateOutputConfig)
.legacyBatchScaledL2(legacyBatchScaledL2).build();
}
/** Helper class for setting input types */
public class InputTypeBuilder {
/**
* See {@link InputType#convolutional(long, long, long)}
*/
public ListBuilder convolutional(int height, int width, int depth){
return ListBuilder.this.setInputType(InputType.convolutional(height, width, depth));
}
/**
* * See {@link InputType#convolutionalFlat(long, long, long)}
*/
public ListBuilder convolutionalFlat(int height, int width, int depth){
return ListBuilder.this.setInputType(InputType.convolutionalFlat(height, width, depth));
}
/**
* See {@link InputType#feedForward(long)}
*/
public ListBuilder feedForward(int size){
return ListBuilder.this.setInputType(InputType.feedForward(size));
}
/**
* See {@link InputType#recurrent(long)}}
*/
public ListBuilder recurrent(int size){
return ListBuilder.this.setInputType(InputType.recurrent(size));
}
}
}
/**
* Return this configuration as json
*
* @return this configuration represented as json
*/
public String toYaml() {
ObjectMapper mapper = mapperYaml();
try {
String ret = mapper.writeValueAsString(this);
return ret;
} catch (org.nd4j.shade.jackson.core.JsonProcessingException e) {
throw new RuntimeException(e);
}
}
/**
* Create a neural net configuration from json
*
* @param json the neural net configuration from json
* @return
*/
public static NeuralNetConfiguration fromYaml(String json) {
ObjectMapper mapper = mapperYaml();
try {
NeuralNetConfiguration ret = mapper.readValue(json, NeuralNetConfiguration.class);
return ret;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Return this configuration as json
*
* @return this configuration represented as json
*/
public String toJson() {
ObjectMapper mapper = mapper();
try {
String ret = mapper.writeValueAsString(this);
return ret;
} catch (org.nd4j.shade.jackson.core.JsonProcessingException e) {
throw new RuntimeException(e);
}
}
/**
* Create a neural net configuration from json
*
* @param json the neural net configuration from json
* @return
*/
public static NeuralNetConfiguration fromJson(String json) {
ObjectMapper mapper = mapper();
try {
NeuralNetConfiguration ret = mapper.readValue(json, NeuralNetConfiguration.class);
return ret;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Object mapper for serialization of configurations
*
* @return
*/
public static ObjectMapper mapperYaml() {
return JsonMappers.getMapperYaml();
}
/**
* Object mapper for serialization of configurations
*
* @return
*/
public static ObjectMapper mapper() {
return JsonMappers.getMapper();
}
/**
* Set of classes that can be registered for legacy deserialization.
*/
private static List<Class<?>> REGISTERABLE_CUSTOM_CLASSES = (List<Class<?>>) Arrays.<Class<?>>asList(
Layer.class,
GraphVertex.class,
InputPreProcessor.class,
IActivation.class,
ILossFunction.class,
ReconstructionDistribution.class
);
/**
* Register a set of classes (Layer, GraphVertex, InputPreProcessor, IActivation, ILossFunction, ReconstructionDistribution
* ONLY) for JSON deserialization.<br>
* <br>
* This is required ONLY when BOTH of the following conditions are met:<br>
* 1. You want to load a serialized net, saved in 1.0.0-alpha or before, AND<br>
* 2. The serialized net has a custom Layer, GraphVertex, etc (i.e., one not defined in DL4J)<br>
* <br>
* By passing the classes of these layers here, DL4J should be able to deserialize them, in spite of the JSON
* format change between versions.
*
* @param classes Classes to register
*/
public static void registerLegacyCustomClassesForJSON(Class<?>... classes) {
registerLegacyCustomClassesForJSONList(Arrays.<Class<?>>asList(classes));
}
/**
* @see #registerLegacyCustomClassesForJSON(Class[])
*/
public static void registerLegacyCustomClassesForJSONList(List<Class<?>> classes){
//Default names (i.e., old format for custom JSON format)
List<Pair<String,Class>> list = new ArrayList<>();
for(Class<?> c : classes){
list.add(new Pair<String,Class>(c.getSimpleName(), c));
}
registerLegacyCustomClassesForJSON(list);
}
/**
* Register a set of classes (Layer, GraphVertex, InputPreProcessor, IActivation, ILossFunction, ReconstructionDistribution
* ONLY) for JSON deserialization, with custom names.<br>
* Using this method directly should never be required (instead: use {@link #registerLegacyCustomClassesForJSON(Class[])}
* but is added in case it is required in non-standard circumstances.
*/
public static void registerLegacyCustomClassesForJSON(List<Pair<String,Class>> classes){
for(Pair<String,Class> p : classes){
String s = p.getFirst();
Class c = p.getRight();
//Check if it's a valid class to register...
boolean found = false;
for( Class<?> c2 : REGISTERABLE_CUSTOM_CLASSES){
if(c2.isAssignableFrom(c)){
if(c2 == Layer.class){
LegacyLayerDeserializer.registerLegacyClassSpecifiedName(s, (Class<? extends Layer>)c);
} else if(c2 == GraphVertex.class){
LegacyGraphVertexDeserializer.registerLegacyClassSpecifiedName(s, (Class<? extends GraphVertex>)c);
} else if(c2 == InputPreProcessor.class){
LegacyPreprocessorDeserializer.registerLegacyClassSpecifiedName(s, (Class<? extends InputPreProcessor>)c);
} else if(c2 == IActivation.class ){
LegacyIActivationDeserializer.registerLegacyClassSpecifiedName(s, (Class<? extends IActivation>)c);
} else if(c2 == ILossFunction.class ){
LegacyILossFunctionDeserializer.registerLegacyClassSpecifiedName(s, (Class<? extends ILossFunction>)c);
} else if(c2 == ReconstructionDistribution.class){
LegacyReconstructionDistributionDeserializer.registerLegacyClassSpecifiedName(s, (Class<? extends ReconstructionDistribution>)c);
}
found = true;
}
}
if(!found){
throw new IllegalArgumentException("Cannot register class for legacy JSON deserialization: class " +
c.getName() + " is not a subtype of classes " + REGISTERABLE_CUSTOM_CLASSES);
}
}
}
/**
* NeuralNetConfiguration builder, used as a starting point for creating a MultiLayerConfiguration or
* ComputationGraphConfiguration.<br>
* Note that values set here on the layer will be applied to all relevant layers - unless the value is overridden
* on a layer's configuration
*/
@Data
public static class Builder implements Cloneable {
protected IActivation activationFn = new ActivationSigmoid();
protected WeightInit weightInit = WeightInit.XAVIER;
protected double biasInit = 0.0;
protected Distribution dist = null;
protected double l1 = Double.NaN;
protected double l2 = Double.NaN;
protected double l1Bias = Double.NaN;
protected double l2Bias = Double.NaN;
protected IDropout idropOut;
protected IWeightNoise weightNoise;
protected IUpdater iUpdater = new Sgd();
protected IUpdater biasUpdater = null;
protected Layer layer;
protected boolean miniBatch = true;
protected int maxNumLineSearchIterations = 5;
protected long seed = System.currentTimeMillis();
protected OptimizationAlgorithm optimizationAlgo = OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT;
protected StepFunction stepFunction = null;
protected boolean minimize = true;
protected GradientNormalization gradientNormalization = GradientNormalization.None;
protected double gradientNormalizationThreshold = 1.0;
protected boolean pretrain = false;
protected List<LayerConstraint> allParamConstraints;
protected List<LayerConstraint> weightConstraints;
protected List<LayerConstraint> biasConstraints;
protected boolean legacyBatchScaledL2 = false;
protected WorkspaceMode trainingWorkspaceMode = WorkspaceMode.ENABLED;
protected WorkspaceMode inferenceWorkspaceMode = WorkspaceMode.ENABLED;
protected boolean setTWM = false;
protected boolean setIWM = false;
protected CacheMode cacheMode = CacheMode.NONE;
protected ConvolutionMode convolutionMode = ConvolutionMode.Truncate;
protected ConvolutionLayer.AlgoMode cudnnAlgoMode = ConvolutionLayer.AlgoMode.PREFER_FASTEST;
public Builder() {
//
}
public Builder(NeuralNetConfiguration newConf) {
if (newConf != null) {
minimize = newConf.minimize;
maxNumLineSearchIterations = newConf.maxNumLineSearchIterations;
layer = newConf.layer;
optimizationAlgo = newConf.optimizationAlgo;
seed = newConf.seed;
stepFunction = newConf.stepFunction;
miniBatch = newConf.miniBatch;
pretrain = newConf.pretrain;
}
}
/**
* Process input as minibatch vs full dataset.
* Default set to true.
*/
public Builder miniBatch(boolean miniBatch) {
this.miniBatch = miniBatch;
return this;
}
/**
* This method defines Workspace mode being used during training:<br>
* NONE: workspace won't be used<br>
* ENABLED: workspaces will be used for training (reduced memory and better performance)
*
* @param workspaceMode Workspace mode for training
* @return Builder
*/
public Builder trainingWorkspaceMode(@NonNull WorkspaceMode workspaceMode) {
this.trainingWorkspaceMode = workspaceMode;
this.setTWM = true;
return this;
}
/**
* This method defines Workspace mode being used during inference:<br>
* NONE: workspace won't be used<br>
* ENABLED: workspaces will be used for inference (reduced memory and better performance)
*
* @param workspaceMode Workspace mode for inference
* @return Builder
*/
public Builder inferenceWorkspaceMode(@NonNull WorkspaceMode workspaceMode) {
this.inferenceWorkspaceMode = workspaceMode;
this.setIWM = true;
return this;
}
/**
* This method defines how/if preOutput cache is handled:
* NONE: cache disabled (default value)
* HOST: Host memory will be used
* DEVICE: GPU memory will be used (on CPU backends effect will be the same as for HOST)
*
* @param cacheMode Cache mode to use
* @return Builder
*/
public Builder cacheMode(@NonNull CacheMode cacheMode) {
this.cacheMode = cacheMode;
return this;
}
/**
* Objective function to minimize or maximize cost function
* Default set to minimize true.
*/
public Builder minimize(boolean minimize) {
this.minimize = minimize;
return this;
}
/**
* Maximum number of line search iterations.
* Only applies for line search optimizers: Line Search SGD, Conjugate Gradient, LBFGS
* is NOT applicable for standard SGD
*
* @param maxNumLineSearchIterations > 0
* @return
*/
public Builder maxNumLineSearchIterations(int maxNumLineSearchIterations) {
this.maxNumLineSearchIterations = maxNumLineSearchIterations;
return this;
}
/**
* Layer class.
*/
public Builder layer(Layer layer) {
this.layer = layer;
return this;
}
/**
* Step function to apply for back track line search.
* Only applies for line search optimizers: Line Search SGD, Conjugate Gradient, LBFGS
* Options: DefaultStepFunction (default), NegativeDefaultStepFunction
* GradientStepFunction (for SGD), NegativeGradientStepFunction
*/
@Deprecated
public Builder stepFunction(StepFunction stepFunction) {
this.stepFunction = stepFunction;
return this;
}
/**
* Create a ListBuilder (for creating a MultiLayerConfiguration)<br>
* Usage:<br>
* <pre>
* {@code .list()
* .layer(new DenseLayer.Builder()...build())
* ...
* .layer(new OutputLayer.Builder()...build())
* }
* </pre>
*/
public ListBuilder list() {
return new ListBuilder(this);
}
/**
* Create a ListBuilder (for creating a MultiLayerConfiguration) with the specified layers<br>
* Usage:<br>
* <pre>
* {@code .list(
* new DenseLayer.Builder()...build(),
* ...,
* new OutputLayer.Builder()...build())
* }
* </pre>
*
* @param layers The layer configurations for the network
*/
public ListBuilder list(Layer... layers) {
if (layers == null || layers.length == 0)
throw new IllegalArgumentException("Cannot create network with no layers");
Map<Integer, Builder> layerMap = new HashMap<>();
for (int i = 0; i < layers.length; i++) {
Builder b = this.clone();
b.layer(layers[i]);
layerMap.put(i, b);
}
return new ListBuilder(this, layerMap);
}
/**
* Create a GraphBuilder (for creating a ComputationGraphConfiguration).
*/
public ComputationGraphConfiguration.GraphBuilder graphBuilder() {
return new ComputationGraphConfiguration.GraphBuilder(this);
}
/**
* Random number generator seed. Used for reproducability between runs
*/
public Builder seed(long seed) {
this.seed = seed;
Nd4j.getRandom().setSeed(seed);
return this;
}
/**
* Optimization algorithm to use. Most common: OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT
*
* @param optimizationAlgo Optimization algorithm to use when training
*/
public Builder optimizationAlgo(OptimizationAlgorithm optimizationAlgo) {
this.optimizationAlgo = optimizationAlgo;
return this;
}
@Override
public Builder clone() {
try {
Builder clone = (Builder) super.clone();
if (clone.layer != null)
clone.layer = clone.layer.clone();
if (clone.stepFunction != null)
clone.stepFunction = clone.stepFunction.clone();
return clone;
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
/**
* Activation function / neuron non-linearity<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*
* @see #activation(Activation)
*/
public Builder activation(IActivation activationFunction) {
this.activationFn = activationFunction;
return this;
}
/**
* Activation function / neuron non-linearity<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*/
public Builder activation(Activation activation) {
return activation(activation.getActivationFunction());
}
/**
* Weight initialization scheme.<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*
* @see org.deeplearning4j.nn.weights.WeightInit
*/
public Builder weightInit(WeightInit weightInit) {
this.weightInit = weightInit;
return this;
}
/**
* Set weight initialization scheme to random sampling via the specified distribution.
* Equivalent to: {@code .weightInit(WeightInit.DISTRIBUTION).dist(distribution)}<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*
* @param distribution Distribution to use for weight initialization
*/
public Builder weightInit(Distribution distribution){
weightInit(WeightInit.DISTRIBUTION);
return dist(distribution);
}
/**
* Constant for bias initialization. Default: 0.0<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*
* @param biasInit Constant for bias initialization
*/
public Builder biasInit(double biasInit) {
this.biasInit = biasInit;
return this;
}
/**
* Distribution to sample initial weights from. Used in conjunction with
* .weightInit(WeightInit.DISTRIBUTION).<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*
* @see #weightInit(Distribution)
*/
public Builder dist(Distribution dist) {
this.dist = dist;
return this;
}
/**
* L1 regularization coefficient for the weights.<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*/
public Builder l1(double l1) {
this.l1 = l1;
return this;
}
/**
* L2 regularization coefficient for the weights.<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*/
public Builder l2(double l2) {
this.l2 = l2;
return this;
}
/**
* L1 regularization coefficient for the bias.<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*/
public Builder l1Bias(double l1Bias) {
this.l1Bias = l1Bias;
return this;
}
/**
* L2 regularization coefficient for the bias.<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*/
public Builder l2Bias(double l2Bias) {
this.l2Bias = l2Bias;
return this;
}
/**
* Dropout probability. This is the probability of <it>retaining</it> each input activation value for a layer.
* dropOut(x) will keep an input activation with probability x, and set to 0 with probability 1-x.<br>
* dropOut(0.0) is a special value / special case - when set to 0.0., dropout is disabled (not applied). Note
* that a dropout value of 1.0 is functionally equivalent to no dropout: i.e., 100% probability of retaining
* each input activation.<br>
* <p>
* Note 1: Dropout is applied at training time only - and is automatically not applied at test time
* (for evaluation, etc)<br>
* Note 2: This sets the probability per-layer. Care should be taken when setting lower values for
* complex networks (too much information may be lost with aggressive (very low) dropout values).<br>
* Note 3: Frequently, dropout is not applied to (or, has higher retain probability for) input (first layer)
* layers. Dropout is also often not applied to output layers. This needs to be handled MANUALLY by the user
* - set .dropout(0) on those layers when using global dropout setting.<br>
* Note 4: Implementation detail (most users can ignore): DL4J uses inverted dropout, as described here:
* <a href="http://cs231n.github.io/neural-networks-2/">http://cs231n.github.io/neural-networks-2/</a>
* </p>
*<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*
* @param inputRetainProbability Dropout probability (probability of retaining each input activation value for a layer)
* @see #dropOut(IDropout)
*/
public Builder dropOut(double inputRetainProbability) {
if(inputRetainProbability == 0.0){
return dropOut(null);
}
return dropOut(new Dropout(inputRetainProbability));
}
/**
* Set the dropout for all layers in this network<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*
* @param dropout Dropout, such as {@link Dropout}, {@link org.deeplearning4j.nn.conf.dropout.GaussianDropout},
* {@link org.deeplearning4j.nn.conf.dropout.GaussianNoise} etc
* @return
*/
public Builder dropOut(IDropout dropout){
//Clone: Dropout is stateful usually - don't want to have the same instance shared in multiple places
this.idropOut = (dropout == null ? null : dropout.clone());
return this;
}
/**
* Set the weight noise (such as {@link org.deeplearning4j.nn.conf.weightnoise.DropConnect} and
* {@link org.deeplearning4j.nn.conf.weightnoise.WeightNoise}) for the layers in this network.<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*
* @param weightNoise Weight noise instance to use
*/
public Builder weightNoise(IWeightNoise weightNoise){
this.weightNoise = weightNoise;
return this;
}
/**
* @deprecated Use {@link #updater(IUpdater)}
*/
@Deprecated
public Builder updater(Updater updater) {
return updater(updater.getIUpdaterWithDefaultConfig());
}
/**
* Gradient updater configuration. For example, {@link org.nd4j.linalg.learning.config.Adam}
* or {@link org.nd4j.linalg.learning.config.Nesterovs}<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*
* @param updater Updater to use
*/
public Builder updater(IUpdater updater) {
this.iUpdater = updater;
return this;
}
/**
* Gradient updater configuration, for the biases only. If not set, biases will use the updater as
* set by {@link #updater(IUpdater)}<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*
* @param updater Updater to use for bias parameters
*/
public Builder biasUpdater(IUpdater updater){
this.biasUpdater = updater;
return this;
}
/**
* Gradient normalization strategy. Used to specify gradient renormalization, gradient clipping etc.
* See {@link GradientNormalization} for details<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*
* @param gradientNormalization Type of normalization to use. Defaults to None.
* @see GradientNormalization
*/
public Builder gradientNormalization(GradientNormalization gradientNormalization) {
this.gradientNormalization = gradientNormalization;
return this;
}
/**
* Threshold for gradient normalization, only used for GradientNormalization.ClipL2PerLayer,
* GradientNormalization.ClipL2PerParamType, and GradientNormalization.ClipElementWiseAbsoluteValue<br>
* Not used otherwise.<br>
* L2 threshold for first two types of clipping, or absolute value threshold for last type of clipping.<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*/
public Builder gradientNormalizationThreshold(double threshold) {
this.gradientNormalizationThreshold = threshold;
return this;
}
/**
* Sets the convolution mode for convolutional layers, which impacts padding and output sizes.
* See {@link ConvolutionMode} for details. Defaults to ConvolutionMode.TRUNCATE<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
* @param convolutionMode Convolution mode to use
*/
public Builder convolutionMode(ConvolutionMode convolutionMode) {
this.convolutionMode = convolutionMode;
return this;
}
/**
* Sets the cuDNN algo mode for convolutional layers, which impacts performance and memory usage of cuDNN.
* See {@link ConvolutionLayer.AlgoMode} for details. Defaults to "PREFER_FASTEST", but "NO_WORKSPACE" uses less memory.
* <br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
* @param cudnnAlgoMode cuDNN algo mode to use
*/
public Builder cudnnAlgoMode(ConvolutionLayer.AlgoMode cudnnAlgoMode) {
this.cudnnAlgoMode = cudnnAlgoMode;
return this;
}
/**
* Set constraints to be applied to all layers. Default: no constraints.<br>
* Constraints can be used to enforce certain conditions (non-negativity of parameters, max-norm regularization,
* etc). These constraints are applied at each iteration, after the parameters have been updated.<br>
* Note: values set by this method will be applied to all applicable layers in the network, unless a different
* value is explicitly set on a given layer. In other words: values set via this method are used as the default
* value, and can be overridden on a per-layer basis.
*
* @param constraints Constraints to apply to all parameters of all layers
*/
public Builder constrainAllParameters(LayerConstraint... constraints){
this.allParamConstraints = Arrays.asList(constraints);