欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

RNN,LSTM,GRU的理解

发布时间:2025/3/8 编程问答 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 RNN,LSTM,GRU的理解 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

RNN


x 为当前状态下数据的输入, h 表示接收到的上一个节点的输入。

y为当前节点状态下的输出,而h′h^\primeh为传递到下一个节点的输出.

LSTM





#定义网络 lstm = nn.LSTM(input_size=20,hidden_size=50,num_layers=2) #输入变量 input_data = Variable(torch.randn(100,32,20)) #初始隐状态 h_0 = Variable(torch.randn(2,32,50)) #输出记忆细胞 c_0 = Variable(torch.randn(2,32,50)) #输出变量 output,(h_t,c_t) = lstm(input_data,(h_0,c_0)) print(output.size()) print(h_t.size()) print(c_t.size()) #参数大小为(50x4,20),是RNN的四倍 print(lstm.weight_ih_l0) print(lstm.weight_ih_l0.size()) 打印结果: torch.Size([100, 32, 50]) torch.Size([2, 32, 50]) torch.Size([2, 32, 50]) tensor([[ 0.0068, -0.0925, -0.0343,, -0.1059, 0.0045, -0.1335], [-0.0509, 0.0135, 0.0100,, 0.0282, -0.1232, 0.0330], [-0.0425, 0.1392, 0.1140,, -0.0740, -0.1214, 0.1087],, [ 0.0217, -0.0032, 0.0815,, -0.0605, 0.0636, 0.1197], [ 0.0144, 0.1288, -0.0569,, 0.1361, 0.0837, -0.0021], [ 0.0355, 0.1045, 0.0339,, 0.1412, 0.0371, 0.0649]], requires_grad=True) torch.Size([200, 20])

注意LSTM的参数,rnn.weight_ih_l0 为 wiw_i~wi  的权重
rnn.weight_hh_l0 为 whw_h~wh  的权重,并且为hidden_size的4倍。

GRU


两个门控

PyTorch中的循环神经网络(RNN+LSTM+GRU)
人人都能看懂的GRU
人人都能看懂的LSTM

总结

以上是生活随笔为你收集整理的RNN,LSTM,GRU的理解的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。