【问题标题】:Tensorflow concat tf.data.Dataset BatchesTensorflow concat tf.data.Dataset 批处理
【发布时间】:2018-04-23 12:50:07
【问题描述】:

使用 tf.data.Dataset 时可以连接 以某种方式批量数据集,而不是第二个数据集 在第一个的末尾连接,但是这样 第二个数据集的第一批在之后连接 第二个数据集的第一批,依此类推。

我尝试如下,但这给了我一个长度为 40 的数据集, 但是,我希望这里的长度为 80。

train_data = train_data.batch(40).concatenate(augmentation_data.batch(40))

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    不完全确定您的用例是什么,但您可能希望像这样分别连接批次中特征和标签的张量:

    def concat_batches(x, y):
        features1, labels1 = x
        features2, labels2 = y
        return ({feature: tf.concat([features1[feature], features2[feature]], axis=0) for feature in features1.keys()}, tf.concat([labels1, labels2], axis=0))
    

    这里是一个例子:

    dataset = tf.data.Dataset.from_tensor_slices(({"test": [[1], [1], [1], [1]]}, [1, 1, 1, 1]))
    b1 = dataset.repeat().batch(3).make_one_shot_iterator().get_next()
    dataset2 = tf.data.Dataset.from_tensor_slices(({"test": [[2], [2], [2], [2]]}, [2, 2, 2, 2]))
    b2 = dataset2.repeat().batch(3).make_one_shot_iterator().get_next()
    
    b_con = concat_batches(b1, b2) #tensors of batches 1 and 2 have shape (3, 1), features of the concatenated batch (6, 1)
    

    在评估您将看到的示例时,b_con 将如下所示:

    ({'test': array([[1],
           [1],
           [1],
           [2],
           [2],
           [2]], dtype=int32)}, array([1, 1, 1, 2, 2, 2], dtype=int32))
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2019-04-26
      • 2017-01-22
      • 2018-03-20
      • 2020-12-11
      • 1970-01-01
      • 2018-10-30
      • 2018-11-09
      • 2017-05-01
      • 2017-08-08
      相关资源
      最近更新 更多