I am new to pytorch and I want to know how RNN many to many classification is being performed using torch for a time series dataset. The dataset consists of 135 features, 121 time steps and a label for each time step. There are total of 10 labels.

This is my code:

```
seq_len = 121
batch_size = 702
features = 135
Train_tensor = Train_tensor.view(batch_size, seq_len, features)
trialtgt = Traintgt_tensor.view(batch_size, seq_len, 1).long()
# Implement RNN network
class Model(nn.Module):
def **init** (self, input_size, output_size, hidden_dim, n_layers):
super(Model, self). **init** ()
# Defining some parameters
self.hidden_dim = hidden_dim
self.n_layers = n_layers
#Defining the layers
# RNN Layer
self.rnn = nn.RNN(input_size, hidden_dim, n_layers, batch_first=True)
# Fully connected layer
self.fc = nn.Linear(hidden_dim, output_size)
def forward(self, x):
batch_size = x.size(0)
#Initializing hidden state for first input using method defined below
hidden = self.init_hidden(batch_size)
print("Hidden", hidden.shape)
# Passing in the input and hidden state into the model and obtaining outputs
#print("X", x.shape)
out, hidden = self.rnn(x, hidden)
print("Output", out.shape)
print("Hidden", hidden.shape)
# Reshaping the outputs such that it can be fit into the fully connected layer
out = out.contiguous().view(-1, self.hidden_dim)
out = self.fc(out)
print("Linear layer", out.shape)
return out, hidden
def init_hidden(self, batch_size):
# This method generates the first hidden state of zeros which we'll use in the forward pass
hidden = torch.zeros(self.n_layers, batch_size, self.hidden_dim).to(device)
return hidden
# Instantiate the model with hyperparameters
model = Model(input_size=features, output_size= 1, hidden_dim=500, n_layers=1)
# We’ll also set the model to the device that we defined earlier (default is CPU)
model = model.to(device)
# Define hyperparameters
n_epochs = 100
lr=0.01
# Define Loss, Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
# Training Run
for epoch in range(1, n_epochs + 1):
optimizer.zero_grad() # Clears existing gradients from previous epoch
Train_tensor = Train_tensor.to(device)
Train_tensor = Train_tensor.float()
output, hidden = model(Train_tensor)
Traintgt_tensor = Traintgt_tensor.to(device)
Traintgt_tensor= Traintgt_tensor.float()
loss = criterion(output, Traintgt_tensor.view(-1).long())
loss.backward() # Does backpropagation and calculates gradients
optimizer.step() # Updates the weights accordingly
if epoch%10 == 0:
print('Epoch: {}/{}.............'.format(epoch, n_epochs), end=' ')
print("Loss: {:.4f}".format(loss.item()))
```

The shape of training set is as follows:

Train_tensor shape torch.Size([702, 121, 135])

Traintgt_tensor shape torch.Size([84942])

trialtgt shape torch.Size([702, 121, 1])

where 702 is the batch size, 121 is the sequence length and 135 is the number of features

The error message that I am getting is,

RuntimeError Traceback (most recent call last)

in ()

11 loss = criterion(output, Traintgt_tensor.view(-1).long())

12 #loss = criterion(output, trialtgt)

—> 13 loss.backward() # Does backpropagation and calculates gradients

14 optimizer.step() # Updates the weights accordingly

15~/anaconda3/lib/python3.6/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)

193 products. Defaults to`False`

.

194 “”"

–> 195 torch.autograd.backward(self, gradient, retain_graph, create_graph)

196

197 def register_hook(self, hook):~/anaconda3/lib/python3.6/site-packages/torch/autograd/

init.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)

97 Variable._execution_engine.run_backward(

98 tensors, grad_tensors, retain_graph, create_graph,

—> 99 allow_unreachable=True) # allow_unreachable flag

100

101RuntimeError: cuda runtime error (710) : device-side assert triggered at /pytorch/aten/src/THC/generic/THCTensorMath.cu:26

Could you please help me solve this issue or help me understand whether I have implemented the RNN correctly!