pytorch rnn输入输出权重偏差维度解释
RNN

1.将上一层的隐状态和这一层的X作为输入
2.输入*权重 + 偏差作为第一个输出
3.第一个输出经过激活函数后得到当前的隐状态
4.当前隐状态*权重+ 偏差最为最终的输出
5.将输出 和 当前隐状态输出 作为结果
输入层参数:
input.shape —> (batch_size, word_features)
weight_xh ----> (word_features, num_hiddens)
H.shape -----> (batch_size, num_hiddens)
weight_hh ----->(num_hiddens, num_hiddens)
bias -----> (num_hiddens)
实际上可以将 x 和 H 进行拼接后 计算
[x] * [w_xh] + [b_xh]
[H] * [w_hh] + [b_hh]

输出层参数:
W_hq = normal((num_hiddens, num_outputs))
b_q = torch.zeros(num_outputs)
前向传播计算过程:
state 用来传入初始隐状态H
params 传入每层计算所需参数

总结输入输出,以及权重维度
1.输入x的维度 (时间步(句子长度/单词长度),bs, word_embedding)
#word_embedding 唯一的确认一个单词
进行前向传播时,对每个时间步/句子长度/单词长度进行循环
for x in range(时间步/句子长度/单词长度):
forward(x)
w_xh 维度 word_embedding * num_hiddens
2.输入H的维度
(bs, num_hiddens)
w_hh (num_hiddens, num_hiddens)
b _h (num_hiddens,)
3.输出
w_o (num_hiddens, output_size/class_nums)
b_o (class_nums)
import torch
from torch import nn
# input
# |
# 0 0 0 0 0
# 0 0 0 0 0
# |
# output
# 隐藏层层数为2 每个隐藏层的神经单元数为5
# word_embedding、num_hiddens、hidden_layers
# 词嵌入维度(输入维度)、隐藏层神经元个数、隐藏层的层数
rnn = nn.RNN(10, 20, 2)
# sequence_length/time_step/word_length batch_size word_embedding
# 词序列长度(几个词)、批次大小、输入维度
input = torch.randn(5, 3, 10)
# 初始状态 h0
# 隐藏层层数 batch_size 隐藏层神经元个数
# 层数*bi、批次大小、输出维度(隐藏层神经元个数)
h0 = torch.randn(2, 3, 20)
output, hn = rnn(input, h0)

更多推荐


所有评论(0)