How to load sparse data with TensorFlow?

后端 未结 5 1426
故里飘歌
故里飘歌 2021-02-12 23:23

There is a small snippet about loading sparse data but I have no idea how to use it.

SparseTensors don\'t play well with queues. If you use SparseTensors

5条回答
  •  盖世英雄少女心
    2021-02-12 23:49

    Store indices and values in your TFRecords Examples, and parse with SparseFeature. For example, to store and load a sparse representation for:

    [[0, 0, 0, 0, 0, 7],
     [0, 5, 0, 0, 0, 0],
     [0, 0, 0, 0, 9, 0],
     [0, 0, 0, 0, 0, 0]]
    

    This creates a TFRecords Example:

    my_example = tf.train.Example(features=tf.train.Features(feature={
        'index_0': tf.train.Feature(int64_list=tf.train.Int64List(value=[0, 1, 2])),
        'index_1': tf.train.Feature(int64_list=tf.train.Int64List(value=[5, 1, 4])),
        'values': tf.train.Feature(int64_list=tf.train.Int64List(value=[7, 5, 9]))
    }))
    my_example_str = my_example.SerializeToString()
    

    And this parses it with SparseFeature:

    my_example_features = {'sparse': tf.SparseFeature(index_key=['index_0', 'index_1'],
                                                      value_key='values',
                                                      dtype=tf.int64,
                                                      size=[4, 6])}
    serialized = tf.placeholder(tf.string)
    parsed = tf.parse_single_example(serialized, features=my_example_features)
    session.run(parsed, feed_dict={serialized: my_example_str})
    
    ## {'sparse': SparseTensorValue(indices=array([[0, 5], [1, 1], [2, 4]]),
    ##                              values=array([7, 5, 9]),
    ##                              dense_shape=array([4, 6]))}
    

    More exposition: Sparse Tensors and TFRecords

提交回复
热议问题