55from __future__ import division
66from __future__ import print_function
77
8+ import time
89from keras import backend as K
910graph_unique_name = K .get_graph ().unique_name
1011
1112from keras .layers import Dense
1213from keras .layers import Activation
14+ from keras .layers import Dropout
1315from keras .layers import Concatenate
1416from keras .layers import Lambda
1517# from keras.layers import BatchNormalization
4648 Last layer will have a linear output.
4749 output_activation: defaulted to "linear".
4850 Activation function to be applied to the network output.
51+ dropout_rate: Float.
52+ Dropout rate for the hidden layers. Active only if > 0.
53+ NOTE: Do not use dropout for PINNs.
4954 res_net: (True, False). Constructs a resnet architecture.
5055 Defaulted to False.
5156 kernel_initializer: Initializer of the `Kernel`, from `k.initializers`.
@@ -68,6 +73,7 @@ def Functional(
6873 hidden_layers = None ,
6974 activation = "tanh" ,
7075 output_activation = "linear" ,
76+ dropout_rate = 0. ,
7177 res_net = False ,
7278 kernel_initializer = None ,
7379 bias_initializer = None ,
@@ -116,6 +122,11 @@ def Functional(
116122 )
117123 else :
118124 bias_initializer = [bias_initializer for l in len (hidden_layers ) * [activation ] + [output_activation ]]
125+ # check dropout rate.
126+ if dropout_rate > 0.0 :
127+ print ("\n NOTE: Dropout layer does not work with PINN setup!!! \n " )
128+ time .sleep (1 )
129+
119130 # prepare regularizers.
120131 kernel_regularizer = default_regularizer (kernel_regularizer )
121132 bias_regularizer = default_regularizer (bias_regularizer )
@@ -222,6 +233,11 @@ def Functional(
222233 if res_net is True :
223234 layer = Lambda (lambda xs : (1 - xs [0 ])* xs [1 ] + xs [0 ]* xs [2 ], name = graph_unique_name ("ResLayer" ))
224235 net [- 1 ] = layer ([net [- 1 ]] + res_outputs [:2 ])
236+ # adding dropout at the end of each layer.
237+ if dropout_rate > 0.0 :
238+ layer = Dropout (dropout_rate , name = graph_unique_name ("Dropout" ))
239+ layers .append (layer )
240+ net [- 1 ] = layer (net [- 1 ])
225241
226242 # Assign to the output variable
227243 if len (net ) == 1 :
0 commit comments