首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在tensorflow.python.data.ops.dataset_ops.BatchDataset中找到最大值

在tensorflow.python.data.ops.dataset_ops.BatchDataset中找到最大值
EN

Stack Overflow用户
提问于 2021-11-02 18:47:48
回答 1查看 148关注 0票数 1

假设下面的代码如下:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-03 08:44:00

您可以使用tf.data.Dataset.from_tensor_slicestf.data.Dataset.map解决问题。

代码语言:javascript
复制
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)
代码语言:javascript
复制
---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,请尝试如下:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69815548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档