我已经从youtube-8m project下载了一些*.tfrecord
数据。您可以使用以下命令下载数据的一小部分:
curl data.yt8m.org/download.py | shard=1,100 partition=2/video/train mirror=us python
我正在尝试了解如何使用新的tf.data API。我想熟悉人们遍历数据集的典型方式。我一直在TF网站和这张幻灯片上使用该指南:Derek Murray's Slides
这是我定义数据集的方式:
# Use interleave() and prefetch() to read many files concurrently.
files = tf.data.Dataset.list_files("./youtube_vids/*.tfrecord")
dataset = files.interleave(lambda x: tf.data.TFRecordDataset(x).prefetch(100),
cycle_length=8)
# Use num_parallel_calls to parallelize map().
dataset = dataset.map(lambda record: tf.parse_single_example(record, feature_map),
num_parallel_calls=2) #
# put in x,y output form
dataset = dataset.map(lambda x: (x['mean_rgb'], x['id']))
# shuffle
dataset = dataset.shuffle(10000)
#one epoch
dataset = dataset.repeat(1)
dataset = dataset.batch(200)
#Use prefetch() to overlap the producer and consumer.
dataset = dataset.prefetch(10)
现在,我知道在急切的执行模式下我可以做到
for x,y in dataset:
x,y
但是,当我尝试如下创建迭代器时:
# A one-shot iterator automatically initializes itself on first use.
iterator = dset.make_one_shot_iterator()
# The return value of get_next() matches the dataset element type.
images, labels = iterator.get_next()
并运行会话
with tf.Session() as sess:
# Loop until all elements have been consumed.
try:
while True:
r = sess.run(images)
except tf.errors.OutOfRangeError:
pass
我得到警告
Use `for ... in dataset:` to iterate over a dataset. If using `tf.estimator`, return the `Dataset` object directly from your input function. As a last resort, you can use `tf.compat.v1.data.make_one_shot_iterator(dataset)`.
所以,这是我的问题:
在会话中迭代数据集的正确方法是什么?这仅仅是v1和v2差异的问题吗?
此外,将数据集直接传递给估算器的建议还意味着输入函数还具有一个迭代器,如上面Derek Murray的幻灯片中所定义的,对吗?
答案 0 :(得分:1)
对于Estimator API,不需要指定迭代器,只需将数据集对象作为输入函数即可。
var myVar;
try {
myVar;
console.log('myVar was declared.');
} catch(error) {
if (error instanceof ReferenceError) {
console.log('myVar was NOT declared');
} else {
console.log('Caught some other error.');
}
}
try {
notDeclared;
console.log('notDeclared was declared');
} catch(error) {
if (error instanceof ReferenceError) {
console.log('notDeclared was NOT declared.');
} else {
console.log('Caught some other error.');
}
}
在TF 2.0数据集中变得可迭代,因此,正如警告消息所述,您可以使用
def input_fn(filename):
dataset = tf.data.TFRecordDataset(filename)
dataset = dataset.shuffle().repeat()
dataset = dataset.map(parse_func)
dataset = dataset.batch()
return dataset
estimator.train(input_fn=lambda: input_fn())