【问题标题】:Add L2 regularization to specific embeddings in Tensorflow将 L2 正则化添加到 Tensorflow 中的特定嵌入
【发布时间】:2018-04-17 11:59:53
【问题描述】:

我正在使用 Tensorflow 构建一个宽和深的模型。对于离散特征,我首先将它们嵌入向量空间,我想知道如何在嵌入上添加 L2 归一化。

L2 正则化运算符tf.nn.l2_loss 接受嵌入张量作为输入,但我只想正则化specific embeddings whose id appear in current batch of data,而不是整个矩阵。

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    只需使用specific embeddings whose id appear in current batch of data 来计算正则化损失。

    import tensorflow as tf
    
    ids = sparse_tensor.values
    uniq_ids, _ = tf.python.ops.array_ops.unique(ids)    
    embedding_index_slices = tf.gather(large_embedding_variable, uniq_ids) 
    regularization_loss = tf.nn.l2_loss(embedding_index_slices.values)
    
    ...
    
    loss = train_loss + FLAGS.l2 * regularization_loss
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-20
      • 2018-06-11
      • 2017-07-30
      • 2020-08-17
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      相关资源
      最近更新 更多