How to load sparse data with TensorFlow?

后端 未结 5 1438
故里飘歌
故里飘歌 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:45

    For libsvm format you can write and parse like below, if you want sparse tensor result(as opposed to dense tensor result using padding strategy)

        #---write
        _float_feature = lambda v: tf.train.Feature(float_list=tf.train.FloatList(value=v))
        _int_feature = lambda v: tf.train.Feature(int64_list=tf.train.Int64List(value=v))
    
        indexes = []
        values = []
    
        for item in l[start:]:
          index,value = item.split(':')
          indexes.append(int(index))
          values.append(float(value))
    
        example = tf.train.Example(features=tf.train.Features(feature={
          'label': _int_feature([label]),
          'num_features': _int_feature([num_features]),
          'index': _int_feature(indexes),
          'value': _float_feature(values)
          }))
    
        writer.write(example.SerializeToString())
    
        #---read
        def decode(batch_serialized_examples):
            features = tf.parse_example(
                batch_serialized_examples,
                features={
                    'label' : tf.FixedLenFeature([], tf.int64),
                    'index' : tf.VarLenFeature(tf.int64),
                    'value' : tf.VarLenFeature(tf.float32),
                })
    
            label = features['label']
            index = features['index']
            value = features['value']
    
            return label, index, value
    

    So by this way you will get label as dense tensor, index and value as two sparse tensors, you can see one self contained example of writing libsvm format to TFRecord and read it for mlp classification from

    https://github.com/chenghuige/tensorflow-example/tree/master/examples/tf-record/sparse https://github.com/chenghuige/tensorflow-example/tree/master/examples/text-classification

提交回复
热议问题