def build(self, input_shape): freeze_layers = self.params.get("freeze_layers") if freeze_layers: if not isinstance(freeze_layers, list): freeze_layers = [freeze_layers] for layer_path in freeze_layers: layer = misc.index_structure(self, layer_path) layer.trainable = False misc.set_dropout(layer, 0) # Disable dropout in frozen layers. self.examples_inputter.build(input_shape) self.built = True
def initialize(self, data_config, params=None): """Initializes the model from the data configuration. Args: data_config: A dictionary containing the data configuration set by the user (e.g. vocabularies, tokenization, pretrained embeddings, etc.). params: A dictionary of hyperparameters. """ if params is None: params = {} self.params.update(params) dropout = self.params.get("dropout") if dropout is not None: misc.set_dropout(self, dropout) self.examples_inputter.initialize(data_config)
def testSetDropout(self): class Layer(tf.keras.layers.Layer): def __init__(self): super().__init__() self.dropout = 0.3 self.x = tf.keras.layers.Dropout(0.2) class Model(tf.keras.layers.Layer): def __init__(self): super().__init__() self.output_dropout = 0.1 self.layer = Layer() self.layers = [Layer(), Layer()] model = Model() misc.set_dropout(model, 0.5) self.assertEqual(model.output_dropout, 0.5) self.assertEqual(model.layer.dropout, 0.5) self.assertEqual(model.layer.x.rate, 0.5) self.assertEqual(model.layers[1].dropout, 0.5) self.assertEqual(model.layers[1].x.rate, 0.5)