合并两个 Tensorflow 数据集

2024-03-13

我有两个 Tensorflow 数据集,我分别处理它们以获得不同的特征和目标窗口:

window_size_x = 3
window_size_y = 2
shift_size = 1

x = np.arange(10)
y = x * 10

x = x[:-window_size_y]
y = y[window_size_x:]

ds_x = tf.data.Dataset.from_tensor_slices(x).window(window_size_x, shift=shift_size, drop_remainder=True)
ds_y = tf.data.Dataset.from_tensor_slices(y).window(window_size_y, shift=shift_size, drop_remainder=True)

for i, j in zip(ds_x, ds_y):
  print(list(i.as_numpy_iterator()), list(j.as_numpy_iterator()))

Output:

[0, 1, 2] [30, 40]
[1, 2, 3] [40, 50]
[2, 3, 4] [50, 60]
[3, 4, 5] [60, 70]
[4, 5, 6] [70, 80]
[5, 6, 7] [80, 90]

当我最终使用这两个数据集将其输入模型时model.fit(ds_x, ds_y)我收到以下错误:

ValueError: `y` argument is not supported when using dataset as input.

当我尝试像这样组合两个数据集时answer https://stackoverflow.com/a/69605943/8973620,我收到另一个错误:

ds_all = tf.data.Dataset.from_tensor_slices((ds_x, ds_y))

Error:

ValueError: Slicing dataset elements is not supported for rank 0.

合并两个数据集的正确方法是什么?


也许尝试这样的事情:

import tensorflow as tf
import numpy as np

window_size_x = 3
window_size_y = 2
shift_size = 1

x = np.arange(10)
y = x * 10

x = x[:-window_size_y]
y = y[window_size_x:]

ds_x = tf.data.Dataset.from_tensor_slices(x).window(window_size_x, shift=shift_size, drop_remainder=True).flat_map(lambda x: x.batch(window_size_x))
ds_y = tf.data.Dataset.from_tensor_slices(y).window(window_size_y, shift=shift_size, drop_remainder=True).flat_map(lambda x: x.batch(window_size_y))
dataset = tf.data.Dataset.zip((ds_x, ds_y))
for i, j in dataset:
  print(i, j)

然后就可以喂食了dataset直接到model.fit(*).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

合并两个 Tensorflow 数据集 的相关文章

随机推荐