Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to summarize LSTM and apply sin to cos fitting

Shulou Source: shulou.com Published: 2022-06-01 05:21:54 09月29日 Update

How to sum up LSTM and sin and cos fitting application, I believe many inexperienced people are helpless, for this reason this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.

I. Summary of LSTM

RNN in practical applications, can not deal with irrelevant information, it is difficult to deal with long-distance dependencies. LSTM thinking, in the original RNN hidden layer there is only one state h, which is very sensitive to short-term input, so we add another state c, which preserves long-term state. Its structure is as follows:

Compared with RNN,

The LSTM classes are defined as follows:

class RNN(nn.Module): def __init__(self): super(RNN, self).__ init__() self.rnn = nn.LSTM( input_size=INPUT_SIZE, hidden_size=32, num_layers=1, batch_first=True ) self.out = nn.Linear(32, 1) def forward(self, x, h_state, c_state): r_out, (h_state, c_state) = self.rnn(x, (h_state, c_state)) out = self.out(r_out).squeeze() return out, h_state, c_state

Improved GRU Version: (Gated Recurrent Unit)

2. Sin and cos fitting application

import torchfrom torch import nnimport numpy as npimport matplotlib.pyplot as pltTIME_STEP = 10INPUT_SIZE = 1learning_rate = 0.001class RNN(nn.Module): def __init__(self): super(RNN, self).__ init__() self.rnn = nn.LSTM( input_size=INPUT_SIZE, hidden_size=32, num_layers=1, batch_first=True ) self.out = nn.Linear(32, 1) def forward(self, x, h_state, c_state): r_out, (h_state, c_state) = self.rnn(x, (h_state, c_state)) out = self.out(r_out).squeeze() return out, h_state, c_staternn = RNN()criterion = nn.MSELoss()optimizer = torch.optim.Adam(rnn.parameters(), lr=learning_rate)h_state = torch.randn(1, 1, 32)c_state = torch.randn(1, 1, 32)plt.figure(1, figsize=(12, 5))plt.ion()for step in range(100): start, end = step * np.pi, (step + 1) * np.pi steps = np.linspace(start, end, TIME_STEP, dtype=np.float32, endpoint=False) x_np = np.sin(steps) # x_np.shape: 10 y_np = np.cos(steps) # y_np.shape: 10 x = torch.from_numpy(x_np[np.newaxis, :, np.newaxis]) y = torch.from_numpy(y_np) prediction, h_state, c_state = rnn(x, h_state, c_state) h_state = h_state.data c_state = c_state.data loss = criterion(prediction, y) optimizer.zero_grad() loss.backward() optimizer.step() plt.plot(steps, y_np.flatten(), 'r-') plt.plot(steps, prediction.data.numpy().flatten(), 'b-') plt.draw() plt.pause(.05)plt.ioff()plt.show() After reading the above, do you know how to summarize LSTM and how to apply sin and cos fitting? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

Tags: Application status content method more problem processing original helpless for this information reason only actual for this ideas skills version short-term article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Linux Shulou Tech Info OPPO Reno Apple