本文记录学习循环神经网络layer 使用,是时间序列学习(3) 。
时间序列学习(1)
https://editor.csdn.net/md/?articleId=124550867
时间序列学习(2)
https://editor.csdn.net/md/?articleId=124551429
输入数据x: [seq, batch, feature_len]
seq
:表示一句话有几个单词batch
:表示一次输入几句话feature_len
: 表示每个单词用feature_len 维的向量来表示例如: [10, 3, 100]
表示10个单词一句话,一次性输入3句话,每个单词用100d的向量表示。
每个单元的 x t x_t xt 表示,[batch, feature_len], 例如 [3, 100]
所以,最开始的 h 0 h_0 h0 必须要初始化为 [batch, hidden] 才能作为 memory 往后传。
input_size
: 就是每个词向量的维数,也就是输入x中的vec(100d)hidden_size
: 特征深度num_layers
: 默认是1 x x x : [seq_len, b, word_vec]
h o 和 h t h_o 和h_t ho和ht :[num_layers, b, h_dim]
o u t out out : [seq_len, b, h_dim]
注意:out是一个聚合过的信息,每一个单词都会有一个输出
import torch
from torch import nn
# 定义一个单层的RNN
rnn_layer = nn.RNN(input_size=100, hidden_size=20, num_layers=1)
print(rnn_layer)
x = torch.rand(10, 3, 100)
out, h = rnn_layer(x, torch.zeros(1, 3, 20)) # h0也可以不写
print(out.shape, h.shape)
RNN(100, 20)
torch.Size([10, 3, 20]) torch.Size([1, 3, 20])
我会专门写一篇博文来分析这个正弦函数预测问题,包括代码+详解,对于RNN的输入输出分析!!
欢迎大家移步下方链接学习!
https://blog.csdn.net/weixin_42521185/article/details/124566528