【问题标题】:how to feed Tensorflow dataset pipeline with an example of triple tensors如何为 TensorFlow 数据集管道提供三重张量示例
【发布时间】:2019-02-03 18:23:15
【问题描述】:

正如official Tensorflow website 中所述,我们可以为数据集管道提供对张量(输入、标签)的示例。我需要知道如何再添加一个项目,例如(输入、标签 1、标签 2)?

【问题讨论】:

  • 你可以只存储在标签数组中,两个标签

标签: tensorflow tensor tensorflow-datasets tensorflow-estimator


【解决方案1】:

简单!

您只需将数据集方法改为输出字典即可。

此代码来自您发布的链接,一直到底部。

def dataset_input_fn():
  filenames = ["/var/data/file1.tfrecord", "/var/data/file2.tfrecord"]
  dataset = tf.data.TFRecordDataset(filenames)

  # Use `tf.parse_single_example()` to extract data from a `tf.Example`
  # protocol buffer, and perform any additional per-record preprocessing.
  def parser(record):
    keys_to_features = {
        "image_data": tf.FixedLenFeature((), tf.string, default_value=""),
        "date_time": tf.FixedLenFeature((), tf.int64, default_value=""),
        "label": tf.FixedLenFeature((), tf.int64,
                                    default_value=tf.zeros([], dtype=tf.int64)),
    }
    parsed = tf.parse_single_example(record, keys_to_features)

    # Perform additional preprocessing on the parsed data.
    image = tf.image.decode_jpeg(parsed["image_data"])
    image = tf.reshape(image, [299, 299, 1])
    label = tf.cast(parsed["label"], tf.int32)

    return {"image_data": image, "date_time": parsed["date_time"]}, label

  # Use `Dataset.map()` to build a pair of a feature dictionary and a label
  # tensor for each example.
  dataset = dataset.map(parser)
  dataset = dataset.shuffle(buffer_size=10000)
  dataset = dataset.batch(32)
  dataset = dataset.repeat(num_epochs)
  iterator = dataset.make_one_shot_iterator()

  # `features` is a dictionary in which each value is a batch of values for
  # that feature; `labels` is a batch of labels.
  features, labels = iterator.get_next()
  return features, labels

现在,features 实际上是一个包含字段image_datadate_time 的字典。 这样,您可以根据需要添加任意数量的特征或标签,同时仍然坚持使用两个输出。

【讨论】:

  • 谢谢。事实上,第二个标签应该作为一个特征提供。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多