相当于tf.data中的tf.SparseFeature

时间:2017-10-24 09:09:48

标签: tensorflow tensorflow-datasets

我目前正在研究的神经网络是接受稀疏张量作为输入。我正在从TFRecord读取我的数据如下:

CreateMap<Form, SingleForm>()
    .ForMember(dest => dest.Filters,
        opts => opts.MapFrom(src => src.Questions));

它就像一个魅力,但我看着_, examples = tf.TFRecordReader(options=options).read_up_to( filename_queue, num_records=batch_size) features = tf.parse_example(examples, features={ 'input_feat': tf.SparseFeature(index_key='input_feat_idx', value_key='input_feat_values', dtype=tf.int64, size=SIZE_FEATURE)}) API看起来更方便很多任务,我不知道如何阅读tf.data对象,就像我对{tf.SparseTensor 1}}和tf.RecordReader。有什么想法吗?

1 个答案:

答案 0 :(得分:4)

TensorFlow 1.5将在核心转换中为tf.SparseTensor添加本机支持。 (如果您pip install tf-nightly,这当前可用,或者在TensorFlow的主分支上从源构建。)这意味着您可以按以下方式编写管道:

# Create a dataset of string records from the input files.
dataset = tf.data.TFRecordReader(filenames)

# Convert each string record into a `tf.SparseTensor` representing a single example.
dataset = dataset.map(lambda record: tf.parse_single_example(
    record, features={'input_feat': tf.SparseFeature(index_key='input_feat_idx',
                                                     value_key='input_feat_values',
                                                     dtype=tf.int64,
                                                     size=SIZE_FEATURE)})

# Stack together up to `batch_size` consecutive elements into a `tf.SparseTensor`
# representing a batch of examples.
dataset = dataset.batch(batch_size)

# Create an iterator to access the elements of `dataset` sequentially.
iterator = dataset.make_one_shot_iterator()

# `next_element` is a `tf.SparseTensor`.
next_element = iterator.get_next()