Is there an equivalent of pytorch.nn.functional.unfold() in keras or tensorflow?
I want to perform a similar operation in keras. However, I am unable to do the unfold operation in keras. I tried it with conv1D layer, but unable to figure out. Any help would be appreciated
'''
import numpy as np
import torch

x = torch.tensor(np.random.rand(25,100,24))   # tensor of shape (batch_size, seq_length,feature_dim)
x = x.unsqueeze(1) # shape=(25,1,100,24)

import torch.nn.functional as F
x = F.unfold(x,(5, 24), stride=(1,24),dilation=(1,1)) #shape (25,120,96)

'''

I don't think there is. But you can do one thing. Use tensorly for unfolding. Make a function that unfolds the input array. Then using that funtion make a lambda layer in keras or tf2.0 . Suppose you have input  array X :
 X = np.array([[[ 0,  1],
                   [ 2,  3],
                   [ 4,  5],
                   [ 6,  7]],
    
                  [[ 8,  9],
                   [10, 11],
                   [12, 13],
                   [14, 15]],
    
                  [[16, 17],
                   [18, 19],
                   [20, 21],
                   [22, 23]]])


To unfold a tensor, simply use the unfold function from TensorLy:

&gt; from tensorly import unfold unfold(X, 0)
&gt;&gt; array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23]])


Now create a function that takes input array and returns unfolded
array

def unfold(X):
        return unfold(X, 0)


Now use this function as a layer in keras

from keras.layers import Lambda
from keras.models import Sequential

model = Sequential()
model.add(....some_layer....)
model.add(....anotenter code hereher_layer....)
model.add(Lambda(unfold))   &lt;&lt;&lt;&lt;=== using our unfold function as keras layer
model.add(...more_layers..)


Hope this will help !

