Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to summarize LSTM and apply sin to cos fitting

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report