假设下面的代码如下:
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],
[6, 6, 6],
[7, 7, 7],
[8, 8, 8],
[9, 9, 9],
[10, 10, 10],
[11, 11, 11],
[12, 12, 12],
])
simple_labels = np.array([
[-1, -1],
[-2, -2],
[-3, -3],
[-4, -4],
[-5, -5],
[-6, -6],
[-7, -7],
[-8, -8],
[-9, -9],
[-10, -10],
[-11, -11],
[-12, -12],
])
def print_dataset(ds):
for inputs, targets in ds:
print("---Batch---")
print("Feature:", inputs.numpy())
print("Label:", targets.numpy())
print("")
ds = tf.keras.preprocessing.timeseries_dataset_from_array(simple_features, simple_labels, sequence_length=4, batch_size=32)
print_dataset(ds)我想从每个simple_feature及其相应的simple_label中提取最大值。提取最大值后,我想将该值添加到simple_feature及其相应的simple_label中。例如,第一个simple_feature给我[1,1,1],它相应的标签给我[-1,-1]。最大值为1,然后将1添加到[1,1,1]和[-1,-1]中,然后得到[2,2,2]和[0,0]。最后的数据集应保留为tensorflow.python.data.ops.dataset_ops.BatchDataset。
发布于 2021-11-03 08:44:00
您可以使用tf.data.Dataset.from_tensor_slices和tf.data.Dataset.map解决问题。
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],
[6, 6, 6],
[7, 7, 7],
[8, 8, 8],
[9, 9, 9],
[10, 10, 10],
[11, 11, 11],
[12, 12, 12],
])
simple_labels = np.array([
[-1, -1],
[-2, -2],
[-3, -3],
[-4, -4],
[-5, -5],
[-6, -6],
[-7, -7],
[-8, -8],
[-9, -9],
[-10, -10],
[-11, -11],
[-12, -12],
])
def print_dataset(ds):
for inputs, targets in ds:
print("---Batch---")
print("Feature: \n", inputs.numpy())
print("Label: \n", targets.numpy())
print("")
def map_max_values(x, y):
max_values = tf.reduce_max(x, axis=1)
temp_x = tf.reshape(tf.repeat(max_values, repeats=x.shape[1]), shape=(x.shape[0], x.shape[1]))
temp_y = tf.reshape(tf.repeat(max_values, repeats=y.shape[1]), shape=(y.shape[0], y.shape[1]))
x = x + temp_x
y = y + temp_y
return x, y
batch_size = 4
ds = tf.data.Dataset.from_tensor_slices((simple_features,simple_labels)).batch(batch_size, drop_remainder=True)
ds = ds.map(map_max_values)
print_dataset(ds)---Batch---
Feature:
[[2 2 2]
[4 4 4]
[6 6 6]
[8 8 8]]
Label:
[[0 0]
[0 0]
[0 0]
[0 0]]
---Batch---
Feature:
[[10 10 10]
[12 12 12]
[14 14 14]
[16 16 16]]
Label:
[[0 0]
[0 0]
[0 0]
[0 0]]
---Batch---
Feature:
[[18 18 18]
[20 20 20]
[22 22 22]
[24 24 24]]
Label:
[[0 0]
[0 0]
[0 0]
[0 0]]或者,如果您真的想使用tf.keras.preprocessing.timeseries_dataset_from_array,请尝试如下:
def map_max_values(x, y):
max_values = tf.reduce_max(x, axis=2)
temp_x = tf.reshape(tf.repeat(max_values, repeats=tf.shape(x)[2], axis=1), shape=tf.shape(x))
temp_y = tf.reshape(tf.repeat(tf.expand_dims(max_values[:, 0], axis=1), repeats=tf.shape(y)[1], axis=1), shape=tf.shape(y))
x = x + temp_x
y = y + temp_y
return x, y
ds = tf.keras.preprocessing.timeseries_dataset_from_array(simple_features, simple_labels, sequence_length=4, batch_size=32)
ds = ds.map(map_max_values)
print_dataset(ds)https://stackoverflow.com/questions/69815548
复制相似问题