【问题标题】:How to reshape data in Tensorflow dataset?如何重塑 Tensorflow 数据集中的数据?
【发布时间】:2021-06-25 23:02:08
【问题描述】:

我正在编写一个数据管道,将成批的时间序列序列和相应的标签输入到需要 3D 输入形状的 LSTM 模型中。我目前有以下内容:

def split(window):
    return window[:-label_length], window[-label_length]

dataset = tf.data.Dataset.from_tensor_slices(data.sin)
dataset = dataset.window(input_length + label_length, shift=label_shift, stride=1, drop_remainder=True)
dataset = dataset.flat_map(lambda window: window.batch(input_length + label_length))
dataset = dataset.map(split, num_parallel_calls=tf.data.AUTOTUNE)
dataset = dataset.cache()
dataset = dataset.shuffle(shuffle_buffer, seed=shuffle_seed, reshuffle_each_iteration=False)
dataset = dataset.batch(batch_size=batch_size, drop_remainder=True)
dataset = dataset.prefetch(tf.data.AUTOTUNE)

for x, y in dataset.take(1): x.shape 的结果形状是 (32, 20),其中 32 是批量大小,20 是序列长度,但我需要 (32, 20, 1) 的形状,其中附加维度表示功能。

我的问题是如何重塑,最好是在缓存数据之前传递给dataset.map 函数的split 函数?

【问题讨论】:

    标签: python pandas tensorflow deep-learning reshape


    【解决方案1】:

    这很容易。在您的拆分函数中执行此操作

    def split(window):
        return window[:-label_length, tf.newaxis], window[-label_length, tf.newaxis, tf.newaxis]
    

    【讨论】:

    • 这会为 x 数据 (32,20,1) 返回正确的形状,但不会为目标返回正确的形状。预期的形状是 (32, 1, 1) 但它返回的形状是 (32,)
    • 会是return window[:-label_length, tf.newaxis], window[-label_length, tf.newaxis, tf.newaxis]吗?
    • 嘿,是的,很抱歉没有意识到您也需要目标具有额外的维度。是的,您需要做的就是将tf.newaxis 添加到标签中。我会更新我的答案
    猜你喜欢
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    相关资源
    最近更新 更多