RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [11, 44]], which is output 0 of AsStridedBackward0, is at version 3; expected version 2 instead.

import torch

Define the sequence of integers and the targets

sequences = [i for i in range(0, 9)]
targets = [i+1 for i in range(0, 9)]

Convert the sequences and targets to tensors

sequences = torch.tensor(sequences, dtype=torch.float32)
targets = torch.tensor(targets, dtype=torch.float32)

class LSTM(nn.Module):
def init(self, input_size, output_size):
super(LSTM, self).init()
self.lstm = nn.LSTM(input_size, 11)
self.fc = nn.Linear(11, output_size)

def forward(self, x,hidden):
x,(h,c) = self.lstm(x,hidden)

x = self.fc(x)
return x,(h,c)

Define the linear layer

model = LSTM(1, 11)

Define the loss function and optimization algorithm

loss_fn = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01,weight_decay=5e-4)

Train the linear layer

for epoch in range(100):
(h,c) = (torch.zeros(1, 11), torch.zeros(1, 11))

Forward pass

for x,y in zip(sequences,targets):
x=x.unsqueeze(0).unsqueeze(0)
outputs,(h,c) = model(x,(h,c))

y=y.unsqueeze(0).type(torch.LongTensor)
loss = loss_fn(outputs, y)


# Backward pass
loss.backward(retain_graph=True)

Update the weights

optimizer.step()

Reset the gradient

optimizer.zero_grad()