0

I want to create a custom min max pooling layer in tensorflow. What do i mean by that is as max pooling takes the maximum value in a matrix of size k I want to implement a layer which gets the min and max values in a matrix of size k and then concats them according to there index.

for eg:

sequence = [1,2,3,6,5,4]

i want to iterate over this tensor in chunks according to filter size say 3 and get the min and max values

1st chunk [1,2,3]
    min value = 1
    max value = 3
    concat according to sequence [1,3]

2nd chunk [6,5,4]
    min value = 4
    max value = 6
    concat according to sequence [6,4]

and then return whole downsampled sequence as [1,3,6,4]
thus converting 
our original input [1,2,3,6,5,4]
to                 [1,3,6,4]

I tried to code a coustom layer in tensor flow using layer subclassing but got a lot of problems. Posted the question in stackoverflow but know one answered. I have provided a lot of details there you can use it as a reference. link = Custom - minmax pooling - Keras - Tensorflow

In the question the problem is with the while loop

I just want to implement a custom layer with min max pooling functionality as above in tensorflow using layer subclassing so it can be used to downsample the inputs by giving same importance to min values as max values.

Or any other efficient or simple way to implement this functionality in tensorflow.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
AB Music Box
  • 81
  • 1
  • 6

1 Answers1

0

Usually you don't want to use loops in tf graphs, because they create many bottlenecks, instead, you should try to use TF functions, and IMO the following can be a decent implementation (maybe some steps at the end might be optimized, but i don't think it will lead to a big improvement):

# make your sequence 3D so that maxpool work fine
seq = tf.convert_to_tensor([1,2,3,6,5,4])[tf.newaxis,...,tf.newaxis]
# your window size, which will also be the stride size
window_size = 3
# find the maximum for each chunk
max_seq = tf.nn.max_pool1d(seq,window_size,window_size,"VALID")
# find the minumum for each chunk 
# (minimum is the "inverse" of maximum, thus subtracting max and mult by -1 works)
max = tf.reduce_max(seq)
inverted = -1 * (seq - max)
min_seq = -1 * tf.nn.max_pool1d(inverted,window_size,window_size,"VALID") + max
# concatenate the two sequence, transpose to have the desired order, and reshape to get the right shape
tf.reshape(tf.transpose(tf.concat((min_seq, max_seq), axis=0)), (-1,))

Output:

<tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 3, 4, 6], dtype=int32)>
Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
  • but the output sequence dose not match the desired output. [1,3,6,4] – AB Music Box Aug 28 '22 at 12:37
  • 1
    @ABMusicBox why would you have "min,max" for the first concatenation, and "max, min" for the second concatenation? you called your variable `sequence`, thus suggesting the variable length... in that case, what's the policy on the concatenation – Alberto Sinigaglia Aug 28 '22 at 12:39
  • The sequence or structure of the time series should not be disturbed. Its actually a time_series and the order in which the values occured contains crucial information. The plots before and after would look different. – AB Music Box Aug 28 '22 at 12:45
  • apologies it is a time-series – AB Music Box Aug 28 '22 at 12:50
  • Is there any way we can get the index's of max values in pooling operation. It can solve the problem. – AB Music Box Aug 28 '22 at 12:55
  • @ABMusicBox you have not explained why would you change the order. Your NN will build a mathematical model between input and output, which is absolutely not invariant to the order of them, thus changing order of min-max at each step, would lead to nothing (and _the plot is different_ makes no scientific point, you have to explain what you aim to, not "oh i want to see this pic") – Alberto Sinigaglia Aug 28 '22 at 13:03
  • The local maxima's and minima's in the time-series would change. Which is what i am trying to preserve. If you plot the time series you would see the difference the local maximum's and minimum's will be destroyed. Dose this make sense? – AB Music Box Aug 28 '22 at 13:18
  • hey are you there? – AB Music Box Aug 28 '22 at 13:52
  • @ABMusicBox I'm going to be light on you, because you seems new, but we are not here to do your job, nor we are obliged to be ready to answer, so please, NEVER call out for someone here, wait with patient, and if somebody, that has free time, and a good will to help other, and is online, and has nothing better to do than staying on a website, on which is not paid, not asked to be there, not nothing else, then he might help you – Alberto Sinigaglia Aug 28 '22 at 14:20
  • @ABMusicBox now, I'll try to be clear, PLEASE, explain WHY you want to change the order, otherwise there is no way for me to help YOU. I can produce code that outputs what you expect, but it wont be correct, because it just might happen to have the same output, but with a different input, it might not work. – Alberto Sinigaglia Aug 28 '22 at 14:22
  • @ABMusicBox having said that, either you explain CLEARLY the ordering policy, or you can take the code I've provided, use the `min_seq` and `max_seq` as input, and you just need to write a for loop, looping over those 2, to get the order that you prefer – Alberto Sinigaglia Aug 28 '22 at 14:23
  • apologies wont do it again. – AB Music Box Aug 28 '22 at 15:21