Tensorflow 2.0 数据集和数据加载器 [英] Tensorflow 2.0 dataset and dataloader
问题描述
我是pytorch用户,习惯了pytorch中的data.dataset和data.dataloader api.我正在尝试使用 tensorflow 2.0 构建相同的模型,我想知道是否有一个 api 与 pytorch 中的这些 api 类似.
I am a pytorch user, and I am used to the data.dataset and data.dataloader api in pytorch. I am trying to build a same model with tensorflow 2.0, and I wonder whether there is an api that works similarly with these api in pytorch.
如果没有这样的api,你们能不能告诉我人们通常是如何在tensorflow中实现数据加载部分的?我使用过 tensorflow 1,但从未体验过数据集 api.我以前硬编码过.我希望有类似覆盖 getitem 的东西,只有索引作为输入.
If there is no such api, can any of you tell me how people usually do to implement the data loading part in tensorflow ? I've used tensorflow 1, but never had an experience with dataset api. I've hard coded before. I hope there is something like overriding getitem with only index as an input.
非常感谢.
推荐答案
在使用 tf.data
API 时,您通常还会使用 map
函数.
When using the tf.data
API, you will usually also make use of the map
function.
在 PyTorch 中,您的 __getItem__
调用基本上从 __init__
中给出的数据结构中获取一个元素,并在必要时对其进行转换.
In PyTorch, your __getItem__
call basically fetches an element from your data structure given in __init__
and transforms it if necessary.
在 TF2.0 中,您通过初始化 Dataset 来执行相同的操作
使用 Dataset.from_...
函数之一(参见 from_generator
, from_tensor_slices
, from_tensors
);这本质上是 PyTorch Dataset
的 __init__
部分.然后,您可以调用 map
来执行 __getItem__
中的元素操作.
In TF2.0, you do the same by initializing a Dataset
using one of the Dataset.from_...
functions (see from_generator
, from_tensor_slices
, from_tensors
); this is essentially the __init__
part of a PyTorch Dataset
. Then, you can call map
to do the element-wise manipulations you would have in __getItem__
.
Tensorflow 数据集是非常花哨的迭代器,因此按照设计,您不会使用索引访问它们的元素,而是通过遍历它们.
Tensorflow datasets are pretty much fancy iterators, so by design you don't access their elements using indices, but rather by traversing them.
tf.data
上的 guide 非常有用,并提供了各种各样的例子.
The guide on tf.data
is very useful and provides a wide variety of examples.
这篇关于Tensorflow 2.0 数据集和数据加载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!