Example #1
0
    def build(self, input_shape):
        super(MaximumEntropyMarkovModel, self).build(input_shape)
        output_dim = input_shape[-1]

        if self.hidden_dim is None:
            self._trans = self.add_weight(name='trans',
                                          shape=(output_dim, output_dim),
                                          initializer='glorot_uniform',
                                          trainable=True)
            if self.lr_multiplier != 1:
                K.set_value(self._trans,
                            K.eval(self._trans) / self.lr_multiplier)
        else:
            self._l_trans = self.add_weight(name='l_trans',
                                            shape=(output_dim,
                                                   self.hidden_dim),
                                            initializer='glorot_uniform',
                                            trainable=True)
            self._r_trans = self.add_weight(name='r_trans',
                                            shape=(output_dim,
                                                   self.hidden_dim),
                                            initializer='glorot_uniform',
                                            trainable=True)

            if self.lr_multiplier != 1:
                K.set_value(self._l_trans,
                            K.eval(self._l_trans) / self.lr_multiplier)
                K.set_value(self._r_trans,
                            K.eval(self._r_trans) / self.lr_multiplier)
Example #2
0
 def build(self, input_shape):
     super(ConditionalRandomField, self).build(input_shape)
     output_dim = input_shape[-1]
     self._trans = self.add_weight(name='trans',
                                   shape=(output_dim, output_dim),
                                   initializer='glorot_uniform',
                                   trainable=True)
     if self.lr_multiplier != 1:
         K.set_value(self._trans, K.eval(self._trans) / self.lr_multiplier)
Example #3
0
        def apply_ema_weights(self, bias_correction=True):
            """备份原模型权重,然后将平均权重应用到模型上去。
            """
            self.old_weights = K.batch_get_value(self.model_weights)
            ema_weights = K.batch_get_value(self.ema_weights)

            if bias_correction:
                iterations = K.eval(self.iterations)
                scale = 1.0 - np.power(self.ema_momentum, iterations)
                ema_weights = [weight / scale for weight in ema_weights]

            K.batch_set_value(zip(self.model_weights, ema_weights))