【问题标题】:How to read from tf.data.Iterator both sequentially and randomly in tensorflow?如何在张量流中按顺序和随机读取 tf.data.Iterator?
【发布时间】:2018-10-04 22:54:37
【问题描述】:

我已经在 tensorflow 中构建了我的 tfrecords 数据库。现在我想读取记录,使得起点是一些随机值,比如 10 到 2000 之间,然后依次读取一些记录,比如 100 到 200 之间。如何使用 tf.data.iterator 或 tensorflow 中的任何替代方法.

非常感谢任何帮助!

【问题讨论】:

    标签: python tensorflow dataset


    【解决方案1】:

    您可以为此使用tf.data.Dataset.take 和tf.data.Dataset.skip。

    例如,如下构造您的tf.data.Dataset 对象:

    starting_point = tf.random_uniform(shape=[], dtype=tf.int64, minval=10, maxval=2000)
    num_records = tf.random_uniform(shape=[], dtype=tf.int64, minval=100, maxval=200)
    
    ds = tf.data.TFRecordDataset(...).skip(starting_point).take(num_records)
    

    然后你可以像任何数据集一样构造一个迭代器和“下一个值”张量。例如:

    itr = ds.make_one_shot_iterator()
    (x, y) = itr.get_next()
    

    希望对您有所帮助。

    【讨论】:

    • 当尝试打印下一个值以及 starting_point 和 num_records 时,我发现在每次调用时,starting_point 和 num_records 变量都会重新评估。 print(sess.run([itr.get_next(), starting_point, num_records]))。如何防止这种行为?我在问,因为这可能会减慢训练过程。
    • 刚刚解决的问题是:ds = tf.data.TFRecordDataset(...).skip(starting_point).take(num_records).batch(num_records)
    • 请注意,在上面的答案中,starting_point 和 num_records 将在 itr 操作时重新评估,即每次创建迭代器时。如果要修复这些问题,则可以静态设置 starting_point 和 num_records,例如,使用 np.random 设置它们。
    猜你喜欢
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    相关资源
    最近更新 更多