简体   繁体   中英

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]. 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. where each slice have all the time steps to aggregate ('2' for the above example).
  • apply tf.scan with an avg() like function for each slice
  • concate all slices result to a new tensor

I'm new to tensorflow so not sure if my pseudo code make sense. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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