python - Tensorflow:将“tf.data.Dataset”迭代器转换为张量
问题描述
我有一个tf.data.Dataset
来自新tf.Datasets
模块的数据集。当然,tf.data.Dataset
它是示例的迭代器,但我实际上需要将此迭代器转换为包含加载到内存中的所有数据的完整张量。我正在处理文本数据,为了提取语料库的词汇表进行标记化,我实际上需要一次使用整个文本语料库。
我当然可以编写一个循环来执行此操作,但我想知道是否有更矢量化或更快的方法来实现相同的任务。谢谢。
我至少可以提供代码的开头。注意我正在使用 Tensorflow 2.0a 来尝试为转换做好准备:
import tensorflow_datasets as tfds
# Download the data
imdb_builder = tfds.builder('imdb_reviews')
imdb_builder.download_and_prepare()
# Setup training test split
imdb_train = imdb_builder.as_dataset(split=tfds.Split.TRAIN)
imdb_test = imdb_builder.as_dataset(split=tfds.Split.TEST)
# Look at the specs on the dataset if you wish
# print(imdb_builder.info)
看一个例子。观察数据是未标记的。
a, = imdb_train.take(1)
print(a['text'])
tf.Tensor(b"As a lifelong fan of Dickens, I have ...", shape=(), dtype=string)
这就是我卡住的地方。请注意,当尝试在此数据集上创建迭代器时,我收到了一个错误:
iter = imdb_train.batch(10).repeat(1).make_one_shot_iterator()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-35-1bf70c474a05> in <module>()
----> 1 imdb_train = imdb_train.batch(10).repeat(1).make_one_shot_iterator()
AttributeError: 'RepeatDataset' object has no attribute 'make_one_shot_iterator'
解决方案
1. 数据加载
使用tfds.load
更简单,更紧凑:
import tensorflow_datasets as tfds
train = tfds.load("imdb_reviews", as_supervised=True, split=tfds.Split.TRAIN)
2.词汇保存者
很简单,您可能希望从零开始索引。
class Tokenizer:
def __init__(self):
self.vocab = {}
self._counter: int = 1
self.tokenizer = tfds.features.text.Tokenizer()
def __call__(self, text):
# Haven't found anything working with tf.tensor, oh sweet irony
tokens = self.tokenizer.tokenize(text.numpy())
for token in tokens:
if not token in self.vocab:
self.vocab[token] = self._counter
self._counter += 1
TBH 很遗憾,tokenizer
对于普通张量没有类似的实用程序,我需要像那样转换它们,但是哦,它仍处于 alpha 阶段。
3. 标记您的数据
由于TF2.0
它的eager
模式,您可以one_shot_iterator
使用循环轻松地迭代和其他奇怪的想法:
tokenizer = Tokenizer()
for text, _ in train:
tokenizer(text)
重要提示:您不必将所有内容加载到内存中,因为它是一个迭代器。vocab
尽管对于非常大的语料库,您可能会遇到内存问题。
4. 结果
印刷品及其指标:
print(list(tokenizer.vocab.keys())[:10])
print(list(tokenizer.vocab.values())[:10])
给我们:
['This', 'was', 'soul', 'provoking', 'I', 'am', 'an', 'Iranian', 'and', 'living']
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
推荐阅读
- python - 某个呼叫类别的第一次呼叫与同一呼叫类别的后续呼叫之间的调查分数差异
- javascript - 求解未知指数
- google-apps-script - 清除单元格时出错 - 简单脚本
- java - 无法加载我的世界插件
- sql - Rails:如何在 rails 的非 id 列上创建聚集索引?
- python - 创建一个序列,其中每个元素中的两个满足特定距离标准
- powershell - 在 Powershell 的 WHERE 子句中添加多个 -and 和 -or 语句
- azure - 如何通过 Azure Logic App 使用模板将 CSV 转换为 JSON
- python - 如何比较任何值是否与使用 numpy 的任何其他值相似
- python - 解析 .dms 文件中存在的深层嵌套 JSON 数据