如何进行LSTM总结及sin与cos拟合应用
如何进行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拟合应用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
名称栏目:如何进行LSTM总结及sin与cos拟合应用
URL分享:http://azwzsj.com/article/ipejej.html