python - 合并两个 Tensorflow 数据集

标签 python tensorflow deep-learning tensorflow-datasets

我有两个 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()))

输出:

[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 时,我收到另一个错误:

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

错误:

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)

然后,您可以将数据集直接提供给model.fit(*)

关于python - 合并两个 Tensorflow 数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72821373/

相关文章:

python - 如何使用wxPython通过在透明背景上拖动鼠标来选择要捕获的屏幕矩形?

Python Pandas,将字符串列导出到 Excel 文件中

tensorflow - 如何使用tensorflow-gpu GPUOptions

python - 修改TensorFlow中传入的梯度

tensorflow - 如何计算 keras 顺序模型的 input_dim?

python - 如何在代码中转换速度?

python - 从 Pandas Series 中选择行,其中行是数组

python - 在 Keras 中为 flow_from_directory 使用多个目录

machine-learning - 在小型图像数据集上训练 GAN

lua - 如何使用 Torch 生成的模型进行预测?