【问题标题】:Is there a way of knowing if `.repeat`/`.batch`/`.shuffle` have been used on a tensorflow dataset?有没有办法知道 `.repeat`/`.batch`/`.shuffle` 是否已在 tensorflow 数据集上使用?
【发布时间】:2019-03-28 17:14:00
【问题描述】:

获得一个已构建的名为 data 的 tensorflow 数据集对象 (tf.data.Dataset)。

有没有办法知道函数 repeat/batch/shuffle 是否在此对象上被调用,通过检查数据? (并可能得到其他信息,如批处理和重复的参数)

(我假设急切执行)

edit 1: 似乎 str 方法带有一些信息。调查那个。

编辑 2:属性 output_shapes 提供有关批量大小和形状的信息。

【问题讨论】:

  • batch_size 显然可以通过调用 get_next 来获取。
  • 定义“通过检查数据?”你的意思是看输出?
  • 我的意思是查看对象数据的属性/方法

标签: python tensorflow deep-learning eager-execution


【解决方案1】:

我能想到的唯一解决方案是进入 tensorflow 代码。 gen_dataset_ops.py 是在源码构建过程中生成的,所以只能在本地找到。

另一个文件是dataset_ops.py,它可以在下面的链接中找到。您只需在相关函数的返回之前插入打印语句。例如来自dataset_ops.py的shuffle函数:

def shuffle(self, buffer_size, seed=None, reshuffle_each_iteration=None):
"""Randomly shuffles the elements of this dataset.
...
print('Dataset shuffled') #inserted print here
return ShuffleDataset(self, buffer_size, seed, reshuffle_each_iteration)

Dataset 对象被包装在DatasetV1Adapter 中,因此您无法提前了解它。急切模式的唯一区别是它支持显式迭代,但是像这样做会非常低效

array = np.random.rand(10)
dataset = tf.data.Dataset.from_tensor_slices(array)
if len([i for i in dataset]) != array.shape[0]:
    print('repeated')

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/data/ops/dataset_ops.py

【讨论】:

  • 我们无权访问数组
  • 我们只有数据集对象
  • 这更像是一个反例。我更新了答案,这很粗糙,但它有效
  • 我认为您应该在回答中删除有关急切模式的部分。这是误导。
  • 可能是因为我的问题不明确。
猜你喜欢
  • 2019-11-18
  • 1970-01-01
  • 2011-06-07
  • 2020-03-25
  • 1970-01-01
  • 2019-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多