【问题标题】:Equivalent of tf.SparseFeature in tf.datatf.data 中 tf.SparseFeature 的等价物
【发布时间】:2017-10-24 09:09:47
【问题描述】:

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

_, 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)})

它的工作原理很吸引人,但我正在查看 tf.data API,它看起来更方便执行许多任务,我不知道如何像使用 tf.RecordReader 和 @987654325 那样读取 tf.SparseTensor 对象@。有什么想法吗?

【问题讨论】:

    标签: tensorflow tensorflow-datasets


    【解决方案1】:

    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()
    

    【讨论】:

      猜你喜欢
      • 2019-11-03
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多