2022.3.3科研记录
《pytorch深度学习入门》曾芃壹
第五章 序列转序列模型
5.1循环神经网络模型
3.RNN实例
import torch
from torch import nn
import numpy as np
import matplotlib.pyplot as plt
steps = np.linspace(0, np.pi*2, 100, dtype=np.float32)
input_x = np.sin(steps)
target_y = np.cos(steps)
plt.plot(steps, input_x, 'b-', label='input:sin')
plt.plot(steps, target_y, 'r-', label='target:cos')
plt.legend(loc='best')
plt.show()
class LSTM(nn.Module):
def __init__(self,INPUT_SIZE):
super(LSTM, self).__init__()
self.lstm = nn.LSTM(
input_size=INPUT_SIZE,
hidden_size=20,
batch_first=True,
)
self.out = nn.Linear(20, 1)
def forward(self, x, h_state,c_state):
r_out,(h_state,c_state) = self.lstm(x,(h_state,c_state))
outputs = self.out(r_out[0,:]).unsqueeze(0)
return outputs,h_state,c_state
def InitHidden(self):
h_state = torch.randn(1,1,20)
c_state = torch.randn(1,1,20)
return h_state,c_state
lstm = LSTM(INPUT_SIZE=1)
optimizer = torch.optim.Adam(lstm.parameters(), lr=0.001)
loss_func = nn.MSELoss()
h_state,c_state = lstm.InitHidden()
plt.figure(1, figsize=(12, 5))
plt.ion()
for step in range(600):
start, end = step * np.pi, (step+1)*np.pi
steps = np.linspace(start, end, 100, dtype=np.float32) # 第一遍,生成100个从π到2π的数;第二遍,生成100个从2π到3π的数
x_np = np.sin(steps)
y_np = np.cos(steps)
x = torch.from_numpy(x_np).unsqueeze(0).unsqueeze(-1) # 在一维增加一个维度,在倒数第一个维度增加一个维度,称为(1,100,1)的一个tensor
y = torch.from_numpy(y_np).unsqueeze(0).unsqueeze(-1)
prediction, h_state,c_state = lstm(x, h_state,c_state)
h_state = h_state.data
c_state = c_state.data
loss = loss_func(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(0.05)
plt.ioff()
plt.show()
感觉不是很明白那几个参数以及维度的变化(为啥要用unsequeeze(0).unsequeeze(-1).啊)
后面的内容我不是很感兴趣(只关心RNN和LSTM的我。。。)
另:这本书的所有代码都可以在GitHub上找到:
pytorch_machine_laerning