假設以下代碼:
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]and ,[-1,-1]然后我會得到[2,2,2]and [0,0]。最終資料集應保留為tensorflow.python.data.ops.dataset_ops.BatchDataset.
uj5u.com熱心網友回復:
您可以使用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://www.uj5u.com/qiye/346293.html
標籤:张量流
