您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
如何进行LSTM总结及sin与cos拟合应用,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
一、LSTM总结
RNN在实际应用中,无法处理无关的信息,很难处理长距离的依赖。LSTM思路,在原始RNN的隐藏层只有一个状态h,它对短期的输入非常敏感,那么,我们再增加一个状态c, 它来保存长期的状态。其结构如下:
与RNN比较,
定义LSTM类如下:
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
改进GRU版本: (Gated Recurrent Unit)
二、sin与cos拟合应用
import torch from torch import nn import numpy as np import matplotlib.pyplot as plt TIME_STEP = 10 INPUT_SIZE = 1 learning_rate = 0.001 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 rnn = 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()
看完上述内容,你们掌握如何进行LSTM总结及sin与cos拟合应用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。