简体   繁体   English

Tensorflow是否等效于pandas.DataFrame.resample?

[英]Tensorflow equivalent to pandas.DataFrame.resample?

I'm looking for a tensorflow equivalent way for resampling a time-series tensor. 我正在寻找一种重采样时间序列张量的张量流等效方法。

I have a tensor with the following dimensions [batch_size, time, feature]. 我有一个具有以下尺寸的张量[batch_size,时间,特征]。 what i'm trying to achieve is a way to create a new tensor that would aggregate several time steps together and the new feature should be some aggregation function (let's say average). 我试图实现的是一种创建新张量的方法,该张量将多个时间步长汇总在一起,并且新功能应该是一些汇总函数(比方说平均值)。

so for example if this is the original data: 因此,例如,如果这是原始数据:

batch_size | time | feature
    0      |  0   |    1
    0      |  1   |    4
    0      |  2   |    7
    0      |  3   |    1
    1      |  0   |    2
    1      |  1   |    8
...
    N=?    |  T=? |    ?

and I want to resample every 2 time steps together, it should result like this: 并且我想每隔两个时间步重新采样一次,结果应该是这样的:

batch_size | time (scaled)| feature
    0      |  0   |    2.5 (=(1+5)/2)
    0      |  1   |    4   (=(7+1)/2)
    1      |  0   |    5   (=(2+8)/2)
...

if there is no elegant way I was thinking maybe to use 如果没有优雅的方法,我想也许可以使用

  • tf.strided_slice to create a list of slices. tf.strided_slice创建切片列表。 where each slice have all the time steps to aggregate ('2' for the above example). 其中每个分片都具有汇总的所有时间步长(上例中为“ 2”)。
  • apply tf.scan with an avg() like function for each slice 对每个切片应用带有avg()函数的tf.scan
  • concate all slices result to a new tensor 将所有切片结果连接到新张量

I'm new to tensorflow so not sure if my pseudo code make sense. 我是tensorflow的新手,所以不确定我的伪代码是否有意义。 also straggling a bit to implement it. 也很费劲地实现它。

any help is very much appreciated :) 很感谢任何形式的帮助 :)

You can use average pooling : 您可以使用平均池

result = tf.layers.average_pooling1d(x, [1, 2, 1], [1, 2, 1])

If you'll want to get average not of 2 elements - just change 2 to 3 in both pool_size and strides 如果你要2个元素来获得平均不-只是改变2〜3两pool_sizestrides

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM