Beispiel #1
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
Beispiel #2
0
 def build(self, input_shape):
     output_dim = input_shape[-1]
     if not isinstance(output_dim, int):
         output_dim = output_dim.value
     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