# Seq2Seq GRU for Multidimensional Timeseries

Hi there,

I’m trying to built a regression model for predicting a one dimensional timeseries from multiple timeseries signals.
I’m using a GRU. However I got a couple problems/questions:

1. If I train my model in batches. Do I also have to predict in batches? (If not, how can I predict without using a batch)

2. How can I include batch normalization for the GRU?

3. Also I’m quite new to pytorch. If anyone has some advice on how to improve my code. I’ll be thankful for any comment.

That’s my entire code:

``````import torch
from torch import nn
from torch.autograd import Variable
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt
import torch.optim as optim

class GRU_BF(nn.Module):
def __init__(self,input_size,hidden_size,num_layers,time_steps,bidirectional=False):
super().__init__()

self.input_size=input_size
self.hidden_size=hidden_size
self.num_layers=num_layers
self.time_steps=time_steps

self.bidirectional=bidirectional

self.GRU_lyrs=nn.GRU(input_size = input_size,hidden_size = hidden_size,num_layers = num_layers,batch_first=True,bidirectional=bidirectional)
self.outp=nn.Linear(time_steps,time_steps)
self.outp2=nn.Linear(time_steps,time_steps)

return

def forward(self,x,hstate):
x,hstate=self.GRU_lyrs(x,hstate)
hstate = Variable(hstate.data)
x=F.relu(self.outp(x[:, :, -1]))
x=self.outp2(x)
return x, hstate

class SamplerBF():
def __init__(self):
self.count=0
return

def sample(self,batchsize):
X1=[]
X2=[]
Y=[]
num_ts=400
for i in range(batchsize):
x=np.linspace(self.count*np.pi,(self.count+4)*np.pi,num_ts)
x1=np.sin(x)
X1.append(x1)
x2=np.cos(x)+2
X2.append(x2)
Y.append((x1/x2))
self.count+=1
return np.swapaxes(np.array([X1,X2]).T,0,1),np.array(Y)

input_dim=2
hidden_size=200
num_layers=2
time_steps=400

lossvals=[]

S=SamplerBF()

mynn=GRU_BF(input_size=input_dim,hidden_size=hidden_size,num_layers=num_layers,time_steps=time_steps)
h_state=None

scheduler = optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.954)

#train
for i in range(20):
mynn.train()
x,y=S.sample(30)
X=torch.Tensor(x)
Y=torch.Tensor(y)

Y_pred, h_state = mynn(X,h_state)

scheduler.step()

error=nn.MSELoss()
loss=error(Y_pred,Y)

loss.backward()

optimizer.step()

lossvals.append(loss.item())
print("It: ",i,lossvals[-1])

plt.semilogy(lossvals)

#predict/eval
mynn.eval()

# I would love to predict without using the same amout of batches, that I used for training
x,y=S.sample(30)

X=torch.Tensor(x)
Y=torch.Tensor(y)

Y_pred, h_state = mynn(X,h_state)

for i in range(2):
fig,ax = plt.subplots(1,1,figsize=(12,6))
ax.plot(y[i],"ko",Y_pred[i].data.numpy(),"r*")
plt.show()

``````

Thanks for any hint in advance!!

Have a good one!