仿魔酷阁网站源码,建站cms,个人网站模板儿童,网站建设的定义RNN x 为当前状态下数据的输入#xff0c; h 表示接收到的上一个节点的输入。
y为当前节点状态下的输出#xff0c;而h′h^\primeh′为传递到下一个节点的输出.
LSTM #定义网络
lstm nn.LSTM(input_size20,hidden_size50,num_layers2)
#输入变量
input_data Variable(tor…RNN x 为当前状态下数据的输入 h 表示接收到的上一个节点的输入。
y为当前节点状态下的输出而h′h^\primeh′为传递到下一个节点的输出.
LSTM #定义网络
lstm nn.LSTM(input_size20,hidden_size50,num_layers2)
#输入变量
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_gradTrue)
torch.Size([200, 20]) 注意LSTM的参数rnn.weight_ih_l0 为 wiw_i~wi 的权重 rnn.weight_hh_l0 为 whw_h~wh 的权重并且为hidden_size的4倍。
GRU 两个门控
PyTorch中的循环神经网络RNNLSTMGRU 人人都能看懂的GRU 人人都能看懂的LSTM