Reshape Time series data for Conv2d Block

I am modelling my time series data into a supervised learning problem for the input to a conv2d block in pytorch from this tutorial.

timesteps = 5

def to_supervised(train,n_input=timesteps,n_output=3):
n_start = 0
train_X , train_y = list(),list()
for _ in range(len(train)):
n_end = n_start + n_input
n_out = n_end + n_output
if n_out < train.shape[0]:
x = train[n_start:n_end,:]
y = train[n_end:n_out,0]
train_X.append(x)
train_y.append(y)
n_start += 1
return np.array(train_X),np.array(train_y)


How ever this code turns the input shape of (100,5) to (92,5,5). But what i am expecting it to make it a 4d array(batchsize, samples, Height, width) for the input of conv2d block. So i have changed the code a little bit

timesteps = 5

def to_supervised(train,n_input=timesteps,n_output=3):
n_start = 0
train_X , train_y = list(),list()
for _ in range(len(train)):
n_end = n_start + n_input
n_out = n_end + n_output
if n_out < train.shape[0]:
x = train[n_start:n_end,:,:] #Here
y = train[n_end:n_out,:,0] # and here
train_X.append(x)
train_y.append(y)
n_start += 1
return np.array(train_X),np.array(train_y)


but it throws error IndexError: too many indices for array

I saw many guides for multivariate sequence but I don't understand how to apply this on this case

• What does the train parameter look like in both cases? In the first version it seems to be 2-dimensions, while you are trying to retrieve 3-dimensions in the second version. This must be the reason for the error. – Romain Reboulleau Nov 12 at 18:47
• Ohh that's must be the case. But if it is possible to get 4d array like (4,23,5,5) by retrieving it from a 2d array (100,5)in the second version directly? Can you suggest some changes i need to make in the second version? – Scrappy Coco Nov 12 at 23:32
• Here's a very similar question on StackOverflow: stackoverflow.com/questions/44932704/… Ask pure code questions there, you will certainly get faster and better answers. – Romain Reboulleau Nov 13 at 6:43