将L2正则化添加到Tensorflow中的特定嵌入

将L2正则化添加到Tensorflow中的特定嵌入,tensorflow,Tensorflow,我正在使用Tensorflow建立一个像wide&deep这样的模型。对于离散特征,我首先将它们嵌入向量空间,我想知道如何在嵌入上添加L2规范化 L2正则化操作符tf.nn.L2_loss接受嵌入张量作为输入,但我只想正则化id出现在当前数据批中的特定嵌入,不是整个矩阵。只需使用id出现在当前数据批中的特定嵌入来计算正则化损失 import tensorflow as tf ids = sparse_tensor.values uniq_ids, _ = tf.python.ops.array

我正在使用Tensorflow建立一个像wide&deep这样的模型。对于离散特征,我首先将它们嵌入向量空间,我想知道如何在嵌入上添加L2规范化


L2正则化操作符
tf.nn.L2_loss
接受嵌入张量作为输入,但我只想正则化id出现在当前数据批中的特定嵌入,不是整个矩阵。

只需使用id出现在当前数据批中的
特定嵌入来计算正则化损失

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