Tensorflow 低稀疏张量有效批处理

Tensorflow 低稀疏张量有效批处理,tensorflow,Tensorflow,我有一个方法(如下所示)可以从tensorflow Sparsetenservalue获取批处理。然而,这种方法相当慢(对于大小为32的批次,需要10-20秒),这是有问题的,因为它被调用了数千次 def get_batch(index, tensors, batch_size, nItems): xs, ys = tensors begin = (index * batch_size) end = min((index+1)*batch_size, nItems)

我有一个方法(如下所示)可以从tensorflow Sparsetenservalue获取批处理。然而,这种方法相当慢(对于大小为32的批次,需要10-20秒),这是有问题的,因为它被调用了数千次

def get_batch(index, tensors, batch_size, nItems):
    xs, ys = tensors
    begin = (index * batch_size)
    end = min((index+1)*batch_size, nItems)
    y_b = ys[begin:end]

    (inds, vals, dsize) = xs
    nInds = [[ind[0] - begin, ind[1]] for ind in inds if begin <= ind[0] < end]
    nInds = np.array(nInds)
    nVals = vals[:nInds.shape[0]]
    nDsize = (end - begin, dsize[1])
    x_b = tf.SparseTensorValue(nInds, nVals, nDsize)
    return (x_b, y_b)
def get_batch(索引、张量、批次大小、nItems):
xs,ys=张量
开始=(索引*批量大小)
结束=最小值((索引+1)*批量大小,单位)
y_b=ys[开始:结束]
(inds、VAL、dsize)=xs

nInds=[[ind[0]-begin,ind[1]]对于inds中的ind,如果begin我建议您使用
tf.data
编写输入管道,那么如果有任何问题,您可以将此重新匹配卸载到另一个核心,而不阻塞主线程