import torch
import torch.nn as nn
import numpy as np
torch.manual_seed(42)
class LSTMModel(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(LSTMModel, self).__init__()
self.hidden_size = hidden_size
self.lstm = nn.LSTM(input_size, hidden_size, num_layers=2)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, input):
lstm_out, self.hidden = self.lstm(input.view(len(input), 1, -1))
output = self.fc(lstm_out.view(len(input), -1))
return output[-1]
input_size = 1
hidden_size = 16
output_size = 1
lr = 0.001
num_epochs = 100
data = np.sin(np.arange(0, 100, 0.1))
model = LSTMModel(input_size, hidden_size, output_size)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
for epoch in range(num_epochs):
inputs = torch.Tensor(data[:-1]).view(-1, 1, 1)
labels = torch.Tensor(data[1:])
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if (epoch+1) % 10 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
with torch.no_grad():
future = 100
pred_data = [data[-1]]
for _ in np.arange(future, future+10, 0.1):
inputs = torch.Tensor([pred_data[-1]]).view(-1, 1, 1)
pred = model(inputs)
pred_data.append(pred.item())
import matplotlib.pyplot as plt
plt.plot(data, label='Original data')
plt.plot(np.arange(len(data), len(data) + future + 1), pred_data, label='Predictions')
plt.legend()
plt.show()
The above code aims to simulate the sine function, but in reality, the result is just a straight line. Please help me to find out the reason why it shows the straight line and help me to fix the code.Thanks
The following graph shows the result of the code.