【问题标题】:How to make padding to max sequence lengths in batch with tensorflow dataset api?如何使用 tensorflow 数据集 api 批量填充最大序列长度?
【发布时间】:2019-10-01 22:25:03
【问题描述】:

我有例子

123
1234
12345
1234556
1234567890

像这样制作全局填充很容易

0000000123
0000001234
0000012345
0001234556
1234567890

但我想在每个数据集 api 批处理生成的内容中进行填充。 例如,批量大小为 3 时需要 3 个随机样本

123
1234
12345

然后像这样填充它

00123
01234
12345

例如,我可以在 numpy 中做到这一点,但这就是在 tf api 中构建批次的方式:

data = tf.data.Dataset.from_tensor_slices((X, y))
data = data.apply(tf.data.experimental.shuffle_and_repeat(buffer_size=len(y)))
data = data.batch(batch_size, drop_remainder=False)
data = data.prefetch(2)

【问题讨论】:

    标签: python tensorflow variable-length-array


    【解决方案1】:

    如果我猜对了,你可以使用 keras pad_sequences:

    sequence = np.array([[1,2], [1, 2, 3, 4], [1, 2, 3,4, 5, 6]])  
    
    tf.keras.preprocessing.sequence.pad_sequences(sequence, padding='pre', value=0)  
    
    array([[0, 0, 0, 0, 1, 2],  
           [0, 0, 1, 2, 3, 4],  
           [1, 2, 3, 4, 5, 6]])
    

    【讨论】:

    • 如果序列是这样的sequence = np.array([[3,2], [5, 2, 3, 4], [1, 2, 3,4, 5, 6]]) ,是否可以使用每行的第一个值作为值参数来获得以下内容? array([[3, 3, 3, 3, 3, 2], [5, 5, 5, 2, 3, 4], [1, 2, 3, 4, 5, 6]])
    【解决方案2】:

    您可以使用 padded_batch 方法。

    data.padded_batch(batch_size, padded_shapes=max_shape)
    

    其中 max_shape 是您想要的填充张量的大小。

    我相信这将附加尾随零而不是前导零,但它可能仍然适合您的目的。

    编辑

    完整的工作示例:

    import tensorflow as tf
    import numpy as np
    
    def gen():
        yield (np.array([1,2,3]), np.array(1))
        yield (np.array([1,2,3,4]), np.array(0))
    
    data = tf.data.Dataset.from_generator(gen, output_types=(tf.int32, tf.int32))
    data = data.apply(tf.contrib.data.shuffle_and_repeat(buffer_size=2))
    data = data.padded_batch(10, padded_shapes=([None], []))
    iterator = tf.data.Iterator.from_structure(data.output_types, data.output_shapes)
    batch = iterator.get_next()
    init_op = iterator.make_initializer(data)
    
    with tf.Session() as sess:
        sess.run(init_op)
        batch_out = sess.run(batch)
        print(batch_out)
    
    
    

    【讨论】:

    • 我试过这样 data = data.padded_batch(batch_size, padded_shapes=([None, None], [None]), drop_remainder=True ) 但出现错误 TypeError: Expected binary or unicode string,得到数组([[-264.],...
    • 我不确定您的错误是否相关。我已经更新了我的答案以提供一个完整的工作示例。您可能需要根据实际数据的形状进行调整。
    • 也许它与 from_tensor_slices 有某种关系我用你的代码和错误colab.research.google.com/drive/…做笔记本
    • from_tensor_slices 不适用于可变维度 ndarray。
    【解决方案3】:

    如果我理解正确,你可以这样做:

    import os
    
    data = """123
    1234
    12345"""
    
    lines = data.splitlines()
    max_len = max((len(i) for i in lines))
    
    lines = (i.rjust(max_len, '0') for i in lines)
    data = os.linesep.join(lines)
    
    print(data)
    

    输出:

    00123
    01234
    12345
    

    【讨论】:

    • 嗯,是的,例如,我可以用 numpy 来做,但不知道如何在 tensorflow 中做。向问题添加了 tensorflow 数据集代码。 Batch 在第二行构造,是张量对象。
    猜你喜欢
    • 1970-01-01
    • 2018-11-11
    • 2020-09-25
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多