首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在特定列数的tensorflow数据集中找到最大值?

如何在特定列数的tensorflow数据集中找到最大值?
EN

Stack Overflow用户
提问于 2021-11-05 09:27:22
回答 1查看 109关注 0票数 0

假设下面的代码:

代码语言:javascript
运行
AI代码解释
复制
import tensorflow as tf
import numpy as np
 
simple_data_samples = np.array([
         [1, 1, 1, 7, -1],
         [2, -2, 2, -2, -2],
         [3, 3, 3, -3, -3],
         [-4, 4, 4, -4, -4],
         [5, 5, 5, -5, -5],
         [6, 6, 6, -4, -6],
         [7, 7, 8, -7, -7],
         [8, 8, 8, -8, -8],
         [9, 4, 9, -9, -9],
         [10, 10, 10, -10, -10],
         [11, 5, 11, -11, -11],
         [12, 12, 12, -12, -12],
])


def print_dataset(ds):
    for inputs, targets in ds:
        print("---Batch---")
        print("Feature:", inputs.numpy())
        print("Label:", targets.numpy())
        print("")
 
    
def timeseries_dataset_multistep_combined(features, label_slice, input_sequence_length, output_sequence_length, sequence_stride, batch_size):
    feature_ds = tf.keras.preprocessing.timeseries_dataset_from_array(features, None, sequence_length=input_sequence_length + output_sequence_length, sequence_stride=sequence_stride ,batch_size=batch_size, shuffle=False)
     
    def split_feature_label(x):
        return x[:, :input_sequence_length, :]+ tf.reduce_max(x[:,:,:],axis=1), x[:, input_sequence_length:, label_slice]+ tf.reduce_max(x[:,:,:],axis=1)
         
    feature_ds = feature_ds.map(split_feature_label)
     
    return feature_ds
 
ds = timeseries_dataset_multistep_combined(simple_data_samples, slice(None, None, None), input_sequence_length=4, output_sequence_length=2, sequence_stride=2, batch_size=1)
print_dataset(ds)

让我解释一下上面的代码是做什么的。它创建了许多特征和标签。然后,它从每列中获取最大值,并将其添加到该列中的各个值中。例如,此功能及其对应的标签:

代码语言:javascript
运行
AI代码解释
复制
Feature: [[[ 1  1  1  7 -1]
  [ 2 -2  2 -2 -2]
  [ 3  3  3 -3 -3]
  [-4  4  4 -4 -4]]]
Label: [[[ 5  5  5 -5 -5]
  [ 6  6  6 -4 -6]]]

在每列中设置以下最大值:

代码语言:javascript
运行
AI代码解释
复制
6,6,6,7,-1

然后将最大值添加到相应的列,您将获得最终输出:

代码语言:javascript
运行
AI代码解释
复制
[[ 7  7  7 14 -2]
  [ 8  4  8  4 -3]
  [ 9  9  9  3 -4]
  [ 2 10 10  2 -5]]]
Label: [[[11 11 11  1 -6]
  [12 12 12  2 -7]]]

我希望从每个特征的前三列和后两列及其对应的标签中提取最大值,而不是从每列中提取最大值。在提取之后,我希望将最大值添加到相应列中的每个值。例如,在上面的示例中,前三列的最大值为6,后两列的最大值为7。之后,前三列的每个值加6,后两列的每个值加7。第一批的最终输出将如下所示:

代码语言:javascript
运行
AI代码解释
复制
Feature: [[[ 7  7  7  14 6]
  [ 8 4  8 5 5]
  [ 9  9  9 4 4]
  [ 2  10  10 3 3]]]
Label: [[[ 11  11  11 2 2]
  [ 12  12 12 3 1]]]

有没有人知道如何从每个批次的前三列和后两列中提取最大值?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-06 01:30:14

tf.reduce_max中使用这样的tf.tile是否适合您:

代码语言:javascript
运行
AI代码解释
复制
import tensorflow as tf
import numpy as np
 
simple_data_samples = np.array([
         [1, 1, 1, 7, -1],
         [2, -2, 2, -2, -2],
         [3, 3, 3, -3, -3],
         [-4, 4, 4, -4, -4],
         [5, 5, 5, -5, -5],
         [6, 6, 6, -4, -6],
         [7, 7, 8, -7, -7],
         [8, 8, 8, -8, -8],
         [9, 4, 9, -9, -9],
         [10, 10, 10, -10, -10],
         [11, 5, 11, -11, -11],
         [12, 12, 12, -12, -12],
])


def print_dataset(ds):
    for inputs, targets in ds:
        print("---Batch---")
        print("Feature:", inputs.numpy())
        print("Label:", targets.numpy())
        print("")
 
    
def timeseries_dataset_multistep_combined(features, label_slice, input_sequence_length, output_sequence_length, sequence_stride, batch_size):
    feature_ds = tf.keras.preprocessing.timeseries_dataset_from_array(features, None, sequence_length=input_sequence_length + output_sequence_length, sequence_stride=sequence_stride ,batch_size=batch_size, shuffle=False)
     
    def split_feature_label(x):
        reduced_first_max_columns = tf.reduce_max(x[:,:,:3], axis=1, keepdims=True) 
        reduced_last_max_columns = tf.reduce_max(x[:,:,3:], axis=1, keepdims=True)
        reduced_first_max_columns = tf.tile(tf.reduce_max(reduced_first_max_columns, axis=-1), [1, 3])
        reduced_last_max_columns = tf.tile(tf.reduce_max(reduced_last_max_columns, axis=-1), [1, 2])
        reduced_x = tf.expand_dims(tf.concat([reduced_first_max_columns, reduced_last_max_columns], axis=1), axis=0)
        
        return x[:, :input_sequence_length, :] + reduced_x, x[:, input_sequence_length:, label_slice] + reduced_x
         
    feature_ds = feature_ds.map(split_feature_label)
     
    return feature_ds
 
ds = timeseries_dataset_multistep_combined(simple_data_samples, slice(None, None, None), input_sequence_length=4, output_sequence_length=2, sequence_stride=2, batch_size=1)
print_dataset(ds)
代码语言:javascript
运行
AI代码解释
复制
---Batch---
Feature: [[[ 7  7  7 14  6]
  [ 8  4  8  5  5]
  [ 9  9  9  4  4]
  [ 2 10 10  3  3]]]
Label: [[[11 11 11  2  2]
  [12 12 12  3  1]]]

---Batch---
Feature: [[[11 11 11 -6 -6]
  [ 4 12 12 -7 -7]
  [13 13 13 -8 -8]
  [14 14 14 -7 -9]]]
Label: [[[ 15  15  16 -10 -10]
  [ 16  16  16 -11 -11]]]
...
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69857097

复制
相关文章

相似问题

领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文