55
55
SIMILARITY_TYPE ,
56
56
NUM_NEG ,
57
57
SPARSE_INPUT_DROPOUT ,
58
+ DENSE_INPUT_DROPOUT ,
58
59
MASKED_LM ,
59
60
ENTITY_RECOGNITION ,
60
61
TENSORBOARD_LOG_DIR ,
@@ -188,8 +189,10 @@ def required_components(cls) -> List[Type[Component]]:
188
189
DROP_RATE_ATTENTION : 0 ,
189
190
# Sparsity of the weights in dense layers
190
191
WEIGHT_SPARSITY : 0.8 ,
191
- # If 'True' apply dropout to sparse tensors
192
+ # If 'True' apply dropout to sparse input tensors
192
193
SPARSE_INPUT_DROPOUT : True ,
194
+ # If 'True' apply dropout to dense input tensors
195
+ DENSE_INPUT_DROPOUT : True ,
193
196
# ## Evaluation parameters
194
197
# How often calculate validation accuracy.
195
198
# Small values may hurt performance, e.g. model accuracy.
@@ -1075,7 +1078,10 @@ def _prepare_sparse_dense_layers(
1075
1078
)
1076
1079
1077
1080
def _prepare_input_layers (self , name : Text ) -> None :
1078
- self ._tf_layers [f"sparse_dropout.{ name } " ] = layers .SparseDropout (
1081
+ self ._tf_layers [f"sparse_input_dropout.{ name } " ] = layers .SparseDropout (
1082
+ rate = self .config [DROP_RATE ]
1083
+ )
1084
+ self ._tf_layers [f"dense_input_dropout.{ name } " ] = tf .keras .layers .Dropout (
1079
1085
rate = self .config [DROP_RATE ]
1080
1086
)
1081
1087
self ._prepare_sparse_dense_layers (
@@ -1172,21 +1178,30 @@ def _combine_sparse_dense_features(
1172
1178
mask : tf .Tensor ,
1173
1179
name : Text ,
1174
1180
sparse_dropout : bool = False ,
1181
+ dense_dropout : bool = False ,
1175
1182
) -> tf .Tensor :
1176
1183
1177
1184
dense_features = []
1178
1185
1179
1186
for f in features :
1180
1187
if isinstance (f , tf .SparseTensor ):
1181
1188
if sparse_dropout :
1182
- _f = self ._tf_layers [f"sparse_dropout.{ name } " ](f , self ._training )
1189
+ _f = self ._tf_layers [f"sparse_input_dropout.{ name } " ](
1190
+ f , self ._training
1191
+ )
1183
1192
else :
1184
1193
_f = f
1185
1194
dense_features .append (self ._tf_layers [f"sparse_to_dense.{ name } " ](_f ))
1186
1195
else :
1187
1196
dense_features .append (f )
1188
1197
1189
- return tf .concat (dense_features , axis = - 1 ) * mask
1198
+ outputs = tf .concat (dense_features , axis = - 1 ) * mask
1199
+ if dense_dropout :
1200
+ outputs = self ._tf_layers [f"dense_input_dropout.{ name } " ](
1201
+ outputs , self ._training
1202
+ )
1203
+
1204
+ return outputs
1190
1205
1191
1206
def _features_as_seq_ids (
1192
1207
self , features : List [Union [np .ndarray , tf .Tensor , tf .SparseTensor ]], name : Text
@@ -1213,9 +1228,12 @@ def _create_bow(
1213
1228
mask : tf .Tensor ,
1214
1229
name : Text ,
1215
1230
sparse_dropout : bool = False ,
1231
+ dense_dropout : bool = False ,
1216
1232
) -> tf .Tensor :
1217
1233
1218
- x = self ._combine_sparse_dense_features (features , mask , name , sparse_dropout )
1234
+ x = self ._combine_sparse_dense_features (
1235
+ features , mask , name , sparse_dropout , dense_dropout
1236
+ )
1219
1237
x = tf .reduce_sum (x , axis = 1 ) # convert to bag-of-words
1220
1238
return self ._tf_layers [f"ffnn.{ name } " ](x , self ._training )
1221
1239
@@ -1224,6 +1242,8 @@ def _create_sequence(
1224
1242
features : List [Union [tf .Tensor , tf .SparseTensor ]],
1225
1243
mask : tf .Tensor ,
1226
1244
name : Text ,
1245
+ sparse_dropout : bool = False ,
1246
+ dense_dropout : bool = False ,
1227
1247
masked_lm_loss : bool = False ,
1228
1248
sequence_ids : bool = False ,
1229
1249
) -> Tuple [tf .Tensor , tf .Tensor , Optional [tf .Tensor ], Optional [tf .Tensor ]]:
@@ -1233,7 +1253,7 @@ def _create_sequence(
1233
1253
seq_ids = None
1234
1254
1235
1255
inputs = self ._combine_sparse_dense_features (
1236
- features , mask , name , sparse_dropout = self . config [ SPARSE_INPUT_DROPOUT ]
1256
+ features , mask , name , sparse_dropout , dense_dropout ,
1237
1257
)
1238
1258
1239
1259
inputs = self ._tf_layers [f"ffnn.{ name } " ](inputs , self ._training )
@@ -1387,7 +1407,9 @@ def batch_loss(
1387
1407
tf_batch_data [TEXT_FEATURES ],
1388
1408
mask_text ,
1389
1409
self .text_name ,
1390
- self .config [MASKED_LM ],
1410
+ sparse_dropout = self .config [SPARSE_INPUT_DROPOUT ],
1411
+ dense_dropout = self .config [DENSE_INPUT_DROPOUT ],
1412
+ masked_lm_loss = self .config [MASKED_LM ],
1391
1413
sequence_ids = True ,
1392
1414
)
1393
1415
0 commit comments