【发布时间】:2021-12-17 06:48:23
【问题描述】:
假设以下代码:
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。
【问题讨论】:
-
到目前为止您尝试过什么?什么不工作?
-
我阅读了 tensorflow 文档并尝试了
tf.reduce_max(ds[:, :, :],axis=-1, keepdims=True)的运气,但它给了我一个错误:'BatchDataset' object is not subscriptable -
dataset = ds.map(lambda x: x+max(x))但我收到错误 TypeError:() takes 1 positional argument but 2 were given -
你使用
tf.keras.preprocessing.timeseries_dataset_from_array有什么原因吗? -
我喜欢给定的 tensorflow 函数
tf.keras.preprocessing.timeseries_dataset_from_array,因为它允许我轻松调整其他项目的输入和标签
标签: python tensorflow tensorflow-datasets