Python <;tf.io.parse_single_示例>;及<;tf.data.experimental.parse_example_dataset>;?

Python <;tf.io.parse_single_示例>;及<;tf.data.experimental.parse_example_dataset>;?,python,tensorflow,tfrecord,Python,Tensorflow,Tfrecord,我是tensorflow的初学者,正在学习如何使用tfrecord数据集 和之间有什么区别吗 tensorflow的版本是2.3.0 举个例子 然后,按如下方式加载tfrecord数据集 我试图查找,但找不到这个,认为两个输出是相同的。。。。, 我应该使用哪一种?这两种情况没有太大区别 在tf.data.experimental.parse_example_dataset中,将重新调整下面的apply函数 def _apply_fn(dataset): """

我是tensorflow的初学者,正在学习如何使用tfrecord数据集

和之间有什么区别吗

tensorflow的版本是2.3.0

举个例子 然后,按如下方式加载tfrecord数据集 我试图查找,但找不到这个,认为两个输出是相同的。。。。,
我应该使用哪一种?

这两种情况没有太大区别

tf.data.experimental.parse_example_dataset
中,将重新调整下面的apply函数

def _apply_fn(dataset):
    """Function from Dataset to Dataset that applies the transformation."""
    out_dataset = _ParseExampleDataset(dataset, features, num_parallel_calls,
                                       deterministic)
    if any(
        isinstance(feature, parsing_ops.SparseFeature) or
        (isinstance(feature, parsing_ops.RaggedFeature) and feature.partitions)
        for feature in features.values()):
      # pylint: disable=protected-access
      # pylint: disable=g-long-lambda
      out_dataset = out_dataset.map(
          lambda x: parsing_ops._construct_tensors_for_composite_features(
              features, x),
          num_parallel_calls=num_parallel_calls)
    return out_dataset

  return _apply_fn  
其中,
tf.io.parse\u single\u example
解析序列化中给出的序列化示例protos,类似于
tf.io.parse\u example
,除了
tf.io.parse\u example
是成批序列化的


您可以使用
tf.io.parse\u示例
来获得比批处理样本更高的性能优势。

很抱歉,时间太晚了。在tensorflow中制作迭代器和数据集与pytorch相比是独一无二的,,,,,这对我很有帮助。谢谢你的建议/

feature_dim=784 

def parse_example(example):
    features = tf.io.parse_single_example(example, features={
        "x": tf.io.FixedLenFeature([feature_dim], dtype=tf.float32),
        "y": tf.io.FixedLenFeature([], dtype=tf.float32)
    })
    x = features["x"]
    y = features["y"]
    return x, y

#-----tf.io.parse_single_example -----ver-----

def create_dataset_1():
    
    dataset = tf.data.TFRecordDataset(["test.tfrecords"]).map(parse_example)

    dataset = dataset.repeat()

    buffer_size=10
    dataset = dataset.shuffle(buffer_size)
    
    #batchsize
    batch_size=10
    dataset = dataset.batch(batch_size)
    
    # set iterator
    iterator = tf.compat.v1.data.make_one_shot_iterator(dataset)
    image, label = iterator.get_next()

    return image, label

image,label=create_dataset_1()
print(image.shape)
print(label.shape)

#(10, 784)
#(10,)
#-----tf.data.experimental.parse_example_dataset -----ver-----

def dict2tuple(feature):
    return feature["x"], feature["y"]

def create_dataset_2():

    feature_dim=784
    dataset = tf.data.TFRecordDataset(["test.tfrecords"]).batch(100).apply(tf.data.experimental.parse_example_dataset(
        {
              "x": tf.io.FixedLenFeature([feature_dim], dtype=tf.float32),
              "y": tf.io.FixedLenFeature([], dtype=tf.float32)
          })).map(dict2tuple)
        
    #set repeat and shuffle
    dataset = dataset.repeat()
    
    dataset = dataset.shuffle(10)
    
    # batchsize
    #batch_size=10
    #dataset = dataset.batch(batch_size)
    
    #iterator
    iterator = tf.compat.v1.data.make_one_shot_iterator(dataset)
    
    image, label = iterator.get_next()

    return image, label

image,label=create_dataset_2()
print(image.shape)
print(label.shape)

#(100, 784)
#(100,)


def _apply_fn(dataset):
    """Function from Dataset to Dataset that applies the transformation."""
    out_dataset = _ParseExampleDataset(dataset, features, num_parallel_calls,
                                       deterministic)
    if any(
        isinstance(feature, parsing_ops.SparseFeature) or
        (isinstance(feature, parsing_ops.RaggedFeature) and feature.partitions)
        for feature in features.values()):
      # pylint: disable=protected-access
      # pylint: disable=g-long-lambda
      out_dataset = out_dataset.map(
          lambda x: parsing_ops._construct_tensors_for_composite_features(
              features, x),
          num_parallel_calls=num_parallel_calls)
    return out_dataset

  return _apply_fn