具有稀疏数据的张量流训练

tensorflow train with sparse data

我想在 python 的张量流中使用稀疏张量进行训练。我找到了很多代码如何做到这一点,但是 none 其中有效。

这里有一个示例代码来说明我的意思,它会抛出一个错误:

import numpy as np
x_vals = tf.sparse.SparseTensor([[0, 0], [0, 1], [1, 2]], [1, 2, 1], [2, 3])
#x_vals = tf.sparse.to_dense(x_vals)    #this line decides, if there is an error
y_vals = np.array([0, 1])

layer_args = lambda : None
layer_args.input_shape = (3,)
layer_args.activation = "sigmoid"
layer_args.use_bias = False

model = tf.keras.models.Sequential(tf.keras.layers.Dense(1, **layer_args.__dict__))

model.compile(loss = "mse")

model.fit(x_vals, y_vals)

错误是:

ValueError: The two structures don't have the same nested structure.

...和一个巨大的堆栈跟踪

好的,我知道它是如何工作的。最简单的解决方案是使用生成器:

from random import shuffle
def data_generator(x_vals, y_vals):
    inds = list(range(x_vals.shape[0]))
    shuffle(inds)
    for ind in inds:
        yield (x_vals[ind, :].todense(), y_vals[ind])

然后使用该生成器生成适合的 x 值:

model.fit(data_generator(x_vals, y_vals))

但是速度很慢。此外,您一次只能训练一个时期,并且有很多您无法使用的 keras 功能。也可能是 tensorflow.keras.utils.Sequence:

class SparseSequence(tf.keras.utils.Sequence):
    def __init__(self, x_vals, y_vals, batch_size = 32):
        self.x_vals = x_vals
        self.y_vals = y_vals
        self.inds = list(range(x_vals.shape[0]))
        shuffle(self.inds)
        self.batch_size = batch_size
    def __getitem__(self, item):
        from_ind = self.batch_size * item
        to_ind = self.batch_size * (item + 1)
        return (self.x_vals[self.inds[from_ind:to_ind], :].todense(),
                y_vals[self.inds[from_ind:to_ind]])
    def on_epoch_end(self):
        shuffle(self.inds)
    def __len__(self):
        return math.ceil(self.x_vals.shape[0] / self.batch_size)

然后在拟合函数中使用它:

model.fit(SparseSequence(x_vals, y_vals))

请记住,需要先将数据转换为 scipy csr 稀疏矩阵,否则代码将无法运行。还要记住不要在 Model.fit().

中使用 "y"-关键字