python - 如何重塑 Tensorflow 数据集中的数据?
问题描述
我正在编写一个数据管道,将成批的时间序列序列和相应的标签输入到需要 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) 的形状,其中附加维度表示特征。
我的问题是如何重塑,最好是在缓存数据之前split
传递给函数的函数中?dataset.map
解决方案
这很容易。在您的拆分功能中执行此操作
def split(window):
return window[:-label_length, tf.newaxis], window[-label_length, tf.newaxis, tf.newaxis]
推荐阅读
- laravel - 在 laravel 8 中更新记录时出错
- bash - 将参数分配给位置参数
- ios - 将设备更新到 iOS 14.7.1 后,iOS 颤振应用程序未运行
- php - 我正在尝试使用 browser() 函数在 PHP 上打印浏览器名称,它在我拥有的每个浏览器上打印的只是“默认浏览器”
- python - ValueError: range() arg 3 在使用 RegexpParser 时不能为零
- python - 如何比较两个整数
- python - 熊猫数据框获取列表中分类值的数量
- android - Fresco:关于使用数据 uri 加载图像/动画时的内部工作的问题 (webp/gif)
- javascript - 在联系页面中提交表单后,无法使用 javascript 重定向到主页
- reactjs - 无法通过 React-Native 在 Android 上使用 Firebase API-s 进行身份验证