循环神经网络RNN、RNNCell、GRUCell

python 复制代码
###################### RNNCell的使用 ####################################

#初始化完全一样
# input_size - The number of expected features in the input x
# hidden_size - The number of features in the hidden state h
# num_layers - Number of recurrent layers, default 1

#但是forward是不一样的
#ht = rnncell(xt,ht_1)
x = torch.randn(10, 1, 100)
rnnCell = nn.RNNCell(input_size=100, hidden_size=30)
ht = torch.zeros(1, 30)
out = []
for xt in x:
    ht = rnnCell(xt, ht)
    out.append(ht)
print('输出总时间步:', len(out),',每个时间步输出的形状:',out[0].shape)


#####################lstm使用#########################
# 多层RNN的例子
rnn = nn.LSTM(input_size=100, hidden_size=20, num_layers=4)
print(rnn)
x = torch.randn(10, 3, 100)
out, (h, c) = rnn(x)
print(out.shape, h.shape, c.shape)
#输出
#torch.Size([10, 3, 20]) torch.Size([4, 3, 20]) torch.Size([4, 3, 20])

x = torch.rand(3,3,4)
id = [0,1]
c = x[id].transpose(0,1)
print(x)
print(c)
print(c.shape)
#实例:正弦曲线预测

input_size = 1
hidden_size = 256
output_size = 1
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.rnn = nn.LSTM(input_size=input_size, hidden_size=hidden_size, num_layers=2)
        self.linear = nn.Linear(hidden_size, output_size)

    def forward(self, x):
        _, (h, c) = self.rnn(x)
        h = h[-1]
        h = self.linear(h)
        return h


model = Net()
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)


num_time_steps =200
start = 0 #在0-3之间随机初始化
time_steps = np.linspace(start, start+20, num_time_steps)
data = np.sin(time_steps)
data = data.reshape(num_time_steps)

horizon_len = 10 #用于估计的时间序列长度
list = [1, 2, 3]


for iter in range(500):
    batch_size = 32
    s = random.sample(range(num_time_steps-horizon_len-1), batch_size)
    train_data_x = []
    train_data_y = []
    for s_i in s:
        train_data_x.append(data[s_i:s_i+horizon_len])
        train_data_y.append(data[s_i + horizon_len])
    train_data_x = torch.tensor(train_data_x, dtype=torch.float32).T
    train_data_x = train_data_x.unsqueeze(2).to(torch.float32)
    train_data_y = torch.tensor(train_data_y, dtype=torch.float64).reshape(-1, batch_size, 1).to(torch.float32)


    output = model(train_data_x)

    loss = criterion(output, train_data_y)
    model.zero_grad()
    loss.backward()
    optimizer.step()

    if iter % 50 == 0:
        print("Iteration: {} loss {}".format(iter, loss.item()))


num_time_steps =200
start = 0
time_steps = np.linspace(start, start+20, num=num_time_steps)
data = np.sin(time_steps)

predictions1 = []

h = torch.zeros(1, 1, hidden_size)
c = torch.zeros(1, 1, hidden_size)

input = deque(data[0:horizon_len], maxlen=horizon_len)
predictions1.extend(data[0:horizon_len])
for i in range(horizon_len, len(data)*5):
    input_tensor = torch.tensor(input).to(torch.float32)
    input_tensor = input_tensor.view(horizon_len, 1, 1)
    pred= model(input_tensor)
    input.append(pred.detach().numpy().ravel()[0])
    predictions1.append(pred.detach().numpy().ravel()[0])

plt.figure()
plt.plot(data)
plt.plot(predictions1)
plt.legend(['sin wave','pred1'])
plt.show()
相关推荐
王_teacher2 小时前
RNN 循环神经网络 计算过程(通俗+公式版+运行实例)
人工智能·rnn·nlp
思绪无限5 小时前
YOLOv5至YOLOv12升级:植物叶片病害识别系统的设计与实现(完整代码+界面+数据集项目)
深度学习·yolo·目标检测·yolov12·yolo全家桶·植物叶片病害检测
白羊by5 小时前
YOLOv1~v11 全版本核心演进总览
深度学习·算法·yolo
lanker就是懒蛋5 小时前
深度学习Q&A:手写反向传播与OOM排查的深层逻辑
人工智能·深度学习
STLearner6 小时前
WSDM 2026 | 时间序列(Time Series)论文总结【预测,表示学习,因果】
大数据·论文阅读·人工智能·深度学习·学习·机器学习·数据挖掘
STLearner7 小时前
WSDM 2026 | 时空数据(Spatial Temporal)论文总结
人工智能·python·深度学习·机器学习·数据挖掘·智慧城市·推荐算法
空中湖7 小时前
大模型修炼秘籍 第十二章:人师指路——RLHF之精髓
人工智能·深度学习·transformer
QQ676580089 小时前
智慧工厂之扬尘识别 铲车装载识别 工程重型机械识别 磁铁识别 深度学习YOLO格式图像识别第10435期
人工智能·深度学习·yolo·扬尘识别·铲车装载·工程重型机械·磁铁识别
思绪无限10 小时前
YOLOv5至YOLOv12升级:行人跌倒检测系统的设计与实现(完整代码+界面+数据集项目)
深度学习·yolo·目标检测·yolov12·yolo全家桶·行人跌倒检测系统
AI医影跨模态组学10 小时前
PLOS Medicine 中山大学肿瘤防治中心蔡木炎等团队:基于多视角深度学习的组织病理学分析用于II期结直肠癌的预后与治疗分层
人工智能·深度学习·论文·医学·医学影像