gpt4 book ai didi

python - 合并 tensorflow 数据集批处理

转载 作者:行者123 更新时间:2023-12-01 23:11:29 25 4
gpt4 key购买 nike

请考虑以下代码:

import tensorflow as tf
import numpy as np

simple_features = np.array([
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],

])

simple_labels = np.array([
[-1, -1],
[-2, -2],
[-3, -3],
[-4, -4],
[-5, -5],

])


simple_features1 = np.array([
[1, 4, 1],
[2, 2, 2],
[3, 3, 3],
[6, 4, 4],
[5, 4, 5],

])

simple_labels1 = np.array([
[8, -7],
[-2, -2],
[-3, 7],
[-4, 9],
[-5, -5],

])

def print_dataset(ds):
for inputs, targets in ds:
print("---Batch---")
print("Feature:", inputs.numpy())
print("Label:", targets.numpy())
print("")

ds1 = tf.keras.preprocessing.timeseries_dataset_from_array(simple_features, simple_labels, sequence_length=4, batch_size=1)
print_dataset(ds1)

ds2 = tf.keras.preprocessing.timeseries_dataset_from_array(simple_features1, simple_labels1, sequence_length=4, batch_size=1)
print_dataset(ds2)

以上代码将创建特征和标签。我想按以下方式合并两个相应的批处理。例如第一批ds1是这样显示的:

---Batch---
Feature: [[[1 1 1]
[2 2 2]
[3 3 3]
[4 4 4]]]
Label: [[-1 -1]]

...第一批 ds2 看起来像这样。

---Batch---
Feature: [[[1 4 1]
[2 2 2]
[3 3 3]
[6 4 4]]]
Label: [[ 8 -7]]

第一批 ds1 和第一批 ds2 应该以这样的方式合并给我以下输出:

---Batch---
Feature: [[[1 1 1 1 4 1]
[2 2 2 2 2 2]
[3 3 3 3 3 3]
[4 4 4 6 4 4 ]]]
Label: [[-1 -1 8 -7]]

最佳答案

您可以使用 tf.concat 连接两个数据集:

import tensorflow as tf
import numpy as np

simple_features = np.array([
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
])
simple_labels = np.array([
[-1, -1],
[-2, -2],
[-3, -3],
[-4, -4],
[-5, -5],
])
simple_features1 = np.array([
[1, 4, 1],
[2, 2, 2],
[3, 3, 3],
[6, 4, 4],
[5, 4, 5],
])
simple_labels1 = np.array([
[8, -7],
[-2, -2],
[-3, 7],
[-4, 9],
[-5, -5],
])

def print_dataset(ds):
for inputs, targets in ds:
print("---Batch---")
print("Feature:", inputs.numpy())
print("Label:", targets.numpy())
print("")

ds1 = tf.keras.preprocessing.timeseries_dataset_from_array(simple_features, simple_labels, sequence_length=4, batch_size=1)
ds2 = tf.keras.preprocessing.timeseries_dataset_from_array(simple_features1, simple_labels1, sequence_length=4, batch_size=1)

def merge(data1, data2):
x1, y1 = data1
x2, y2 = data2
return tf.concat([x1, x2], axis=-1), tf.concat([y1, y2], axis=-1)

dataset = tf.data.Dataset.zip((ds1, ds2)).map(merge)
print_dataset(dataset)
---Batch---
Feature: [[[1 1 1 1 4 1]
[2 2 2 2 2 2]
[3 3 3 3 3 3]
[4 4 4 6 4 4]]]
Label: [[-1 -1 8 -7]]

---Batch---
Feature: [[[2 2 2 2 2 2]
[3 3 3 3 3 3]
[4 4 4 6 4 4]
[5 5 5 5 4 5]]]
Label: [[-2 -2 -2 -2]]

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com