将可变长度张量的集合保存到 TensorFlow 中的 TFRecords 文件

Saving a collection of variable length tensors to a TFRecords file in TensorFlow

我正在尝试将不同长度的张量列表保存到 TFRecords 文件中,以便以后可以轻松加载它们。所讨论的张量是一维整数数组。

这是因为张量是处理大型文本文件的结果。此文件非常大,处理速度很慢,所以我不想每次 运行 我的算法时都必须重复该步骤。我最初想将文本文件加载到常规 Python 列表或 numpy 数组中,然后对它们进行酸洗,但是从这些列表到张量的转换本身需要很长时间,所以我不想等待每次我 运行 我的脚本,要么。似乎张量不能直接 pickle,即使有一些解决方法,我的印象是 TFRecords 是 "correct" 保存张量数据的方法。

但是,我不确定如何将张量正确保存到 TFRecords 文件,然后将它们作为张量加载回来。我确实完成了 TensorFlow 教程,其中 MNIST 数据被保存到 TFRecords 文件然后加载,但它与我的用例之间存在一些差异。

以下是一段代码,旨在复制我在一个更简单的案例中遇到的问题。

import tensorflow as tf

def _int64_list_feature(values):
   return tf.train.Feature(int64_list=tf.train.Int64List(value=values))

filename = "/Users/me/tensorflow/test.tfrecords"
writer = tf.python_io.TFRecordWriter(filename)
example = tf.train.Example(features=tf.train.Features(feature={'datalist': _int64_list_feature([2,3])}))
writer.write(example.SerializeToString())
example = tf.train.Example(features=tf.train.Features(feature={'datalist': _int64_list_feature([8,5,7])}))
writer.write(example.SerializeToString())
writer.close()

前几行是标准的。我将两个一维张量写入 TFRecords 文件,一个长度为 2,一个长度为 3。

def read_my_file(filename_queue):
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(serialized_example, features={'datalist': tf.VarLenFeature(tf.int64), })
    datalist = features['datalist']
    return datalist

您似乎应该使用的辅助函数。我不是 100% 确定为什么这是必要的,但如果不写这个我就无法让它工作,而且所有的例子都有这样的东西。就我而言,数据未标记,因此我没有标签变量。

filename_queue = tf.train.string_input_producer([filename], 2)
datalists = read_my_file(filename_queue)
datalists_batch = tf.train.batch([datalists], batch_size=2)

示例中的更多 "boilerplate" 样式代码。批量大小为 2,因为我在这段代码中只有 2 个示例。

datalists_batch 现在将是一个稀疏张量,其中包含我的向量 [2, 3][8, 5, 7],第一个在第二个之上。因此,我想将它们拆分回单独的张量。在这一点上,我已经担心运行这个时间可能也很长,因为在我的真实代码中有超过 100,000 个单独的张量将被拆分。

split_list = tf.sparse_split(0, 2, datalists_batch)
sp0 = split_list[0]
sp1 = split_list[1]
sp0_dense = tf.sparse_tensor_to_dense(sp0)
sp1_dense = tf.sparse_tensor_to_dense(sp1)
sp0_dense = tf.squeeze(sp0_dense)
sp1_dense = tf.squeeze(sp1_dense)

split_list 现在是单个张量的列表,仍然是稀疏格式(并且所有的长度都等于最长张量的长度,在本例中为 3。它们也是 2-维度与另一个维度 1,因为 datalists_batch 张量是二维的)。我现在必须操纵张量以将它们转换为正确的格式。在实际代码中,我当然会使用 for 循环,但在这种情况下只有 2 个示例。首先,我将它们转换为密集张量。然而,在 sp0 的情况下,这会用 0 填充最后一个索引,因为这个张量的长度为 3。(这个问题将在下面讨论。)然后,我 "squeeze" 他们所以它们实际上被认为是长度为 3 而不是 1x3 的张量。

最后,我需要从 sp0 中删除尾随零。这部分给我带来了困难。我不知道以编程方式知道特定张量有多少个尾随零。它等于最长张量的长度减去该张量的长度,但如果不查看稀疏索引,我不知道张量的 "real" 长度,但如果不评估 "temp" 张量(因为索引本身就是张量)。

indices_0 = sp0.indices
indices_1 = sp1.indices
indices0_size = tf.shape(indices_0)
indices1_size = tf.shape(indices_1)

这些是上述切片操作所必需的。

sess = tf.Session()
init_op = tf.initialize_all_variables()
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)

初始化。

sp0_fixed = tf.slice(sp0_dense, [0], [sess.run(indices0_size[0])])
sp1_fixed = tf.slice(sp1_dense, [0], [sess.run(indices1_size[0])])
sess.run(sp0_fixed)
sess.run(sp1_fixed)

这就是我要做的。问题是,当 运行 执行这最后三个命令时,我得到了奇怪的错误。我推测问题是我在调用 sess.run 之后(在 sp0_fixed 行中)创建了新的操作,因此该图同时被 运行 调用。我想我应该只 运行 sess.run 一次。但是,这使我无法找出对每个张量进行切片的正确索引(以删除尾随零)。因此,我不知道下一步该怎么做。

我惊讶地发现,在 Google、TensorFlow 文档和 Whosebug 上,对于如何执行此类操作(保存和加载可变长度张量 to/from 文件)没有任何帮助。我很确定我的做法是错误的;即使有重写最后四行以使程序正常运行的变通方法,代码总体上似乎过于复杂,无法执行非常基本的功能。

如果有任何建议和反馈,我将不胜感激。

我对 tfRecords 没有太多经验,但这是使用 tfRecords 存储和检索可变长度数组的一种方法

写一个 tfrecord

# creating a default session we'll use it later 

sess = tf.InteractiveSession( )

def get_writable( arr ):
    """
        this fucntion returns a serialized string 
        for input array of integers
        arr : input array
    """
    arr = tf.train.Int64List( value = arr)
    arr = tf.train.Feature(int64_list= arr )
    arr = tf.train.Features(feature =  { 'seqs': arr})
    arr = tf.train.Example( features = arr)
    return arr.SerializeToString()


filename = "./test2.tfrecords"
writer = tf.python_io.TFRecordWriter(filename)

#writing 3 different sized arrays 
writer.write( get_writable([1,3,5,9]))
writer.write( get_writable([2,7,9]))
writer.write( get_writable([3,4,6,5,9]))
writer.close()

将数组写入'test2.tfrecord'

正在读取文件

##Reading from the tf_record file 

## creating a filename queue 
reader = tf.TFRecordReader( )
filename_queue = tf.train.string_input_producer(['test2.tfrecords'])

##getting the reader 
_, ser_ex = reader.read(filename_queue, )

##features that you want to extract 
read_features = {
    'seqs' : tf.VarLenFeature(dtype = tf.int64)
}
batchSize  = 2
# before parsing examples you must wrap it in tf.batch to get desired batch size  
batch = tf.train.batch([ser_ex], batch_size= batchSize , capacity=10)
read_data = tf.parse_example( batch, features= read_features )
tf.train.start_queue_runners( sess) 
# starting reading queues are requred before reding the data  

现在我们准备读取 tfRecord 文件的内容

batches = 3
for _ in range(batches):

    #get the next sparse tensor of shape (batchSize X elements in the largest array ) 
    #every time you invoke read_data.values()

    sparse_tensor = (list(read_data.values())[0]).eval()

    # as the batch size is larger than 1 
    # you'd want seperate lists that you fed 
    #at the time of writing the tf_record file 

    for i in tf.sparse_split(axis=  0, num_split=batchSize, sp_input= sparse_tensor  ):
        i = i.eval()
        shape =  [1, (i).indices.shape[0]] 
        #getting individual shapes of different sparse tensors  
        tens = tf.sparse_to_dense(sparse_indices=i.indices ,sparse_values= i.values , output_shape= shape) 
        #converting them into dense tensors
        print(tens.eval()) 
        #evaluating the final Dense Tensor 

查看 this post,了解如何开始使用 tfRecords