【问题标题】:Tensorflow: Creating a custom text dataset to use in machine translationTensorflow:创建用于机器翻译的自定义文本数据集
【发布时间】:2019-07-18 16:52:11
【问题描述】:

我想使用我自己的数据来为machine translation system using Transformers 训练模型。 TFDS(Tensorflow 数据集)中已经有一组数据集可用,并且还可以选择 add a new dataset 到 TFDS。但是如果我不必等待那些添加请求和东西并直接训练我的数据怎么办?

在示例 colab 笔记本中,他们使用以下内容创建训练和验证数据:

examples, metadata = tfds.load('ted_hrlr_translate/pt_to_en', with_info=True,
                               as_supervised=True)
train_examples, val_examples = examples['train'], examples['validation']

我相信 TFDS 做了很多预处理来适应管道,它是 Dataset 类型的。

type(train_examples)

tensorflow.python.data.ops.dataset_ops._OptionsDataset

但是对于像下面这样的自定义 CSV 数据,我如何创建与此模型兼容的“数据集”?

import pandas as pd 

# initialize list of lists 
data = [['tom', 10], ['nick', 15], ['juli', 14],['tom', 10], ['nick', 15]]
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Age']) 

# print dataframe. 
df 

【问题讨论】:

    标签: python-3.x tensorflow tensorflow2.0 machine-translation


    【解决方案1】:

    colab notebook 中的数据集只是字符串对(句子的翻译对)的集合。这似乎不是你所拥有的(你有名字和年龄??)。

    但是,当然可以从语言对的 csv(或名称和年龄!)创建数据集。这里有一个数据集 API 的综合指南:https://www.tensorflow.org/guide/datasets 但本质上,给定一个名为“translations.csv”的 csv,如下所示:

    hola,hello
    adios,goodbye
    pero,dog
    huevos,eggs
    ...
    

    那么我们可以这样做:

    my_dataset = tf.data.experimental.CsvDataset("translations.csv", [tf.string, tf.string])
    

    同样,对于您的姓名/年龄数据集,您可以执行以下操作:

    my_dataset = tf.data.experimental.CsvDataset("ages.csv", [tf.string, tf.int32])
    

    【讨论】:

    • 我的错,我创建了那个 df 作为这个问题的例子。我尝试了您的代码并尝试将其拆分为训练和验证,就像在 colab 中一样,但是这样做train_examples = my_dataset['English'] 返回了TypeError: 'CsvDatasetV2' object is not subscriptable
    • 我必须创建一个与该块兼容的数据结构,以便我进行标记化并在建模中继续进行tokenizer_en = tfds.features.text.SubwordTextEncoder.build_from_corpus( (en.numpy() for pt, en in train_examples), target_vocab_size=2**13)
    • 是的,您需要实现自己的逻辑来将数据拆分为训练和测试 - 您可以为两者设置单独的 csv。需要明确的是,在我的示例中,my_dataset 类似于演示代码中的 train_examplestest_examples,而不是演示代码(包含 2 个数据集)中的 examples。这有意义吗?
    猜你喜欢
    • 2019-06-19
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 2021-05-18
    • 1970-01-01
    • 2019-09-03
    • 2018-07-29
    相关资源
    最近更新 更多