【发布时间】:2018-04-17 11:59:53
【问题描述】:
我正在使用 Tensorflow 构建一个宽和深的模型。对于离散特征,我首先将它们嵌入向量空间,我想知道如何在嵌入上添加 L2 归一化。
L2 正则化运算符tf.nn.l2_loss 接受嵌入张量作为输入,但我只想正则化specific embeddings whose id appear in current batch of data,而不是整个矩阵。
【问题讨论】:
标签: tensorflow
我正在使用 Tensorflow 构建一个宽和深的模型。对于离散特征,我首先将它们嵌入向量空间,我想知道如何在嵌入上添加 L2 归一化。
L2 正则化运算符tf.nn.l2_loss 接受嵌入张量作为输入,但我只想正则化specific embeddings whose id appear in current batch of data,而不是整个矩阵。
【问题讨论】:
标签: tensorflow
只需使用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
【讨论】: