請考慮以下代碼:
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]]
uj5u.com熱心網友回復:
您可以使用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]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/351024.html
