I got sequential time series data. At each time stamp, there is only variable to observe (if my understanding is correct this means number of features = 1). I want to train a simple RNN with more than one layer to predict the next observation.
I created training data using sliding window, with window size set to 8. To give a concrete idea, below is my original data, training data and target .
Sample Data
0.40 0.82 0.14 0.01 0.98 0.53 2.5 0.49 0.53 3.37 0.49
Training Data
X =
0.40 0.82 0.14 0.01 0.98 0.53 2.5 0.49
0.82 0.14 0.01 0.98 0.53 2.5 0.49 0.53
0.14 0.01 0.98 0.53 2.5 0.49 0.53 3.37
corresponding targets are
Y =
0.53
3.37
0.49
I set the batch size to 3. But it gives me an error saying
RuntimeError: input.size(-1) must be equal to input_size. Expected 8, got 1
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data
import numpy as np
X = np.array( [ [0.40, 0.82, 0.14, 0.01, 0.98, 0.53, 2.5, 0.49], [0.82, 0.14, 0.01, 0.98, 0.53, 2.5, 0.49, 0.53], [0.14, 0.01, 0.98, 0.53, 2.5, 0.49, 0.53, 3.37] ], dtype=np.float32)
Y = np.array([[0.53], [3.37], [0.49]], dtype=np.float32)
class RNNModel(nn.Module):
def __init__(self, input_sz, n_layers):
super(RNNModel, self).__init__()
self.hidden_dim = 3*input_sz
self.n_layers = n_layers
output_sz = 1
self.rnn = nn.RNN(input_sz, self.hidden_dim, num_layers=n_layers, batch_first=True)
self.linear = nn.Linear(self.hidden_dim, output_sz)
def forward(self,x):
batch_sz = x.size(0)
hidden = torch.zeros(self.n_layers, batch_sz, self.hidden_dim) #initialize n_layer*batch_sz number of hidden states of dimension hidden_dim)
out, hidden = self.rnn(x, hidden)
out = out.contiguous().view(-1, self.hidden_dim)
return out,hidden
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = RNNModel(8,2)
X = torch.tensor(X[:,:,np.newaxis])
Y = torch.tensor(Y[:,:,np.newaxis])
X = X.to(device)
Y = Y.to(device)
model = model.to(device)
optimizer = optim.Adam(model.parameters())
loss_fn = nn.MSELoss()
loader = data.DataLoader(data.TensorDataset(X, Y), shuffle=False, batch_size=3)
n_epoch = 10
for epoch in range(n_epoch):
model.train()
for X_batch, Y_batch in loader:
Y_pred = model(X_batch)
loss = loss_fn(Y_pred,Y_batch)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if epoch % 10 != 0:
continue
model.eval()
with torch.no_grad():
Y_pred = model(X)
train_rmse = np.sqrt(loss_fn(Y_pred,Y))
print("Epoch %d: train RMSE %.4f" % (epoch, train_rmse))
What am I doing wrong? Can anyone help me?