예제 #1
0
    def build(self, input_shape):
        output_dim = input_shape[-1]
        if not isinstance(output_dim, int):
            output_dim = output_dim.value

        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)
                self.trans = self.lr_multiplier * self.trans
        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)
                self.l_trans = self.lr_multiplier * self.l_trans
                K.set_value(self.r_trans,
                            K.eval(self.r_trans) / self.lr_multiplier)
                self.r_trans = self.lr_multiplier * self.r_trans
예제 #2
0
 def train_function(inputs):  # 重新定义训练函数
     grads = embedding_gradients(inputs)[0]  # Embedding梯度
     delta = epsilon * grads / (np.sqrt((grads**2).sum()) + 1e-8)  # 计算扰动
     K.set_value(embeddings, K.eval(embeddings) + delta)  # 注入扰动
     outputs = old_train_function(inputs)  # 梯度下降
     K.set_value(embeddings, K.eval(embeddings) - delta)  # 删除扰动
     return outputs