Tensorflow tf.nn.嵌入\u查找\u稀疏三维稀疏张量输入

Tensorflow tf.nn.嵌入\u查找\u稀疏三维稀疏张量输入,tensorflow,Tensorflow,我有一个嵌入矩阵,有一个3D稀疏张量,用来得到嵌入输出,读了我的文档发现它只支持2D稀疏张量 sp_ID:N x M int64 ID的SparseTensor,其中N通常为批量大小,M为任意大小 这里是我的示例代码 import numpy as np import tensorflow as tf tf.enable_eager_execution() # [feature number, embedding dim] w = tf.get_variable("w", [4, 4], i

我有一个嵌入矩阵,有一个3D稀疏张量,用来得到嵌入输出,读了我的文档发现它只支持2D稀疏张量

sp_ID:N x M int64 ID的SparseTensor,其中N通常为批量大小,M为任意大小

这里是我的示例代码

import numpy as np
import tensorflow as tf
tf.enable_eager_execution()

# [feature number, embedding dim] 
w = tf.get_variable("w", [4, 4], initializer=tf.random_normal_initializer())

z = np.array(
     [
      [
        [0, 1, 2, 3],   # get the vector of row 0, 1, 2, 3 of the embedding matrix w and get the sum
        [2, 3]
      ],

      [
        [1, 3],
        [2]
      ],

      [
        [0, 1, 3],
        [1, 2]
      ]
     ])

sp = tf.SparseTensor(values=[0, 1, 2, 3, 2, 3, 1, 3, 2, 0, 1, 3, 1, 2],
                     indices=[[0,0,0],[0,0,1],[0,0,2],[0,0,3],[0,1,2],
                              [0,1,3],[1,0,1],[1,0,3],[1,1,2],[2,0,0],
                              [2,0,1],[2,0,3],[2,1,1],[2,1,2]],
                     dense_shape=[3, 2, 4])

tf.nn.embedding_lookup_sparse(w, sp, None, combiner='sum')
# the outputs
<tf.Tensor: id=970, shape=(3, 4), dtype=float32, numpy=
array([[-5.8729677 , -1.3900641 ,  0.8126096 , -3.1223912 ],
       [-1.0788026 , -1.1324122 ,  0.34160078,  0.23714277],
       [-2.497394  , -2.7855003 ,  3.0201516 , -1.8009453 ]],
      dtype=float32)>

print(w)
<tf.Variable 'w:0' shape=(4, 4) dtype=float32, numpy=
array([[-2.5669768 , -0.38916406,  1.4039794 , -2.8173826 ],
       [ 1.1483854 , -1.2639242 ,  1.2745714 ,  0.7792944 ],
       [-1.3400027 , -0.46362385, -1.3652185 ,  0.27220532],
       [-0.8871854 ,  0.5951359 ,  0.43224794, -0.8143569 ]],
      dtype=float32)>
将numpy导入为np
导入tensorflow作为tf
tf.enable_eager_execution()
#[特征编号,嵌入尺寸]
w=tf.get_变量(“w”,[4,4],初始值设定项=tf.random_normal_初始值设定项())
z=np.array(
[
[
[0,1,2,3],#得到嵌入矩阵w的第0,1,2,3行的向量并得到和
[2, 3]
],
[
[1, 3],
[2]
],
[
[0, 1, 3],
[1, 2]
]
])
sp=tf.SparseTensor(值=[0,1,2,3,2,3,1,3,2,0,1,3,1,2],
指数=[[0,0,0],[0,0,1],[0,0,2],[0,0,3],[0,1,2],
[0,1,3],[1,0,1],[1,0,3],[1,1,2],[2,0,0],
[2,0,1],[2,0,3],[2,1,1],[2,1,2]],
稠密的_形=[3,2,4])
嵌入查找稀疏(w,sp,None,combiner='sum')
#输出
打印(w)

但预期输出是一个维度为
3x2x4
的矩阵,而不是
3x4
tf.nn.embedding\u lookup\u sparse
是否支持此操作?

最简单的方法是将稀疏张量作为二维张量,并获得嵌入矩阵的权重,然后重新整形

#首先将z作为2D arr并创建稀疏张量
z=np.array([
[0,1,2,3],#得到嵌入矩阵w的第0,1,2,3行并得到和
[2, 3],
[1, 3],
[2],
[0, 1, 3],
[1, 2]
])
sp=tf.SparseTensor(值=[0,1,2,3,2,3,1,3,2,0,1,3,1,2],
指数=[[0,0]、[0,1]、[0,2]、[0,3]、[1,2]、[1,3]、[2,1],
[2,3],[3,2],[4,0],[4,1],[4,3],[5,1],[5,2]],
密集_形=[6,4])
res=tf.nn.embedding\u lookup\u sparse(w,sp,None,combiner='sum')
努比(res.numpy)
#输出
阵列([-3.6457794,-1.5215762,1.7455802,-2.5802398],
[-2.227188  ,  0.13151208, -0.9329706 , -0.5421516 ],
[ 0.2612    , -0.6687883 ,  1.7068193 , -0.03506255],
[-1.3400027 , -0.46362385, -1.3652185 ,  0.27220532],
[-2.3057768 , -1.0579524 ,  3.1107986 , -2.8524451 ],
[-0.19161725, -1.7275481 , -0.0906471 ,  1.0514997 ]],
#重塑
重塑(res,[-1,2,4])
#这正是我想要的。
阵列([-3.6457794,-1.5215762,1.7455802,-2.5802398],
[-2.227188  ,  0.13151208, -0.9329706 , -0.5421516 ]],
[[ 0.2612    , -0.6687883 ,  1.7068193 , -0.03506255],
[-1.3400027 , -0.46362385, -1.3652185 ,  0.27220532]],
[[-2.3057768 , -1.0579524 ,  3.1107986 , -2.8524451 ],
[-0.19161725, -1.7275481 , -0.0906471 ,  1.0514997 ]]])
#打印w,上面的结果是正确的
w、 numpy()
数组([[-2.5669768,-0.38916406,1.4039794,-2.8173826],
[ 1.1483854 , -1.2639242 ,  1.2745714 ,  0.7792944 ],
[-1.3400027 , -0.46362385, -1.3652185 ,  0.27220532],
[-0.8871854 ,  0.5951359 ,  0.43224794, -0.8143569 ]],
dtype=32)
因此,忘记3D稀疏张量,只需将其转换为2D张量,因为您只关心稀疏张量中的值(行索引,用于获取嵌入矩阵的对应行)