示例#1
0
    def tag_recurrent_dropout(self,
                              variables,
                              recurrent_dropout,
                              rng=None,
                              **hyperparameters):
        from blocks.roles import OUTPUT, has_roles
        ancestors = graph.deep_ancestors(variables)
        for lstm in self.rnn.transitions:
            variables = [
                var for var in ancestors
                if (has_roles(var, [OUTPUT]) and lstm in var.tag.annotations
                    and var.name.endswith("states"))
            ]

            # get one dropout mask for all time steps.  use the very
            # first state to get the hidden state shape, else we get
            # graph cycles.
            initial_state = util.the(
                [var for var in variables if "initial_state" in var.name])
            mask = util.get_dropout_mask(initial_state.shape,
                                         recurrent_dropout,
                                         rng=rng)

            subsequent_states = [
                var for var in variables if "initial_state" not in var.name
            ]
            graph.add_transform(subsequent_states,
                                graph.DropoutTransform("recurrent_dropout",
                                                       mask=mask),
                                reason="regularization")
示例#2
0
 def __call__(self, x, rng=None, **hyperparameters):
     p = hyperparameters[self.key]
     if isinstance(p, numbers.Number) and p <= 0:
         return x
     mask = self.mask or util.get_dropout_mask(
         x.shape, p, rng=self.rng or rng)
     return x * mask