赞
踩
nn.RNNCell(input_size, hidden_size, bias=True, nonlinearity=‘tanh’)
h
′
=
tanh
(
W
i
h
x
+
b
i
h
+
W
h
h
h
+
b
h
h
)
h^{\prime}=\tanh \left(W_{i h} x+b_{i h}+W_{h h} h+b_{h h}\right)
h′=tanh(Wihx+bih+Whhh+bhh)
输入:
输出:
参数:
#输入特征维度5,输出维度10
rnn_cell = torch.nn.RNNCell(5,10)
#Batch_size=2
input = torch.randn(2,5)
h_0 = torch.randn(2,10)
h = rnn_cell(input,h_0)
h.shape
>>torch.Size([2, 10])
[(para[0],para[1].shape) for para in list(rnn_cell.named_parameters())]
>>[('weight_ih', torch.Size([10, 5])),
('weight_hh', torch.Size([10, 10])),
('bias_ih', torch.Size([10])),
('bias_hh', torch.Size([10]))]
torch.nn.RNN(args, kwargs)*
h
t
=
tanh
(
W
i
h
x
t
+
b
i
h
+
W
h
h
h
(
t
−
1
)
+
b
h
h
)
h_{t}=\tanh \left(W_{i h} x_{t}+b_{i h}+W_{h h} h_{(t-1)}+b_{h h}\right)
ht=tanh(Wihxt+bih+Whhh(t−1)+bhh)
True
, becomes a bidirectional RNN. Default: False
输入:
输出:
参数:
#输入特征维度5,输出维度10, 层数2 rnn = torch.nn.RNN(5, 10, 2) #seq长度4,batch_size=2 input = torch.randn(4 , 2 , 5) h_0 =torch.randn(2 , 2 , 10) output,hn=rnn(input ,h_0) print(output.size(),hn.size()) >>torch.Size([4, 2, 10]) torch.Size([2, 2, 10]) [(para[0],para[1].shape) for para in list(rnn.named_parameters())] >>[('weight_ih_l0', torch.Size([10, 5])), ('weight_hh_l0', torch.Size([10, 10])), ('bias_ih_l0', torch.Size([10])), ('bias_hh_l0', torch.Size([10])), ('weight_ih_l1', torch.Size([10, 10])), ('weight_hh_l1', torch.Size([10, 10])), ('bias_ih_l1', torch.Size([10])), ('bias_hh_l1', torch.Size([10]))] rnn = torch.nn.RNN(5, 10, 2,bidirectional=True) >>[('weight_ih_l0', torch.Size([10, 5])), ('weight_hh_l0', torch.Size([10, 10])), ('bias_ih_l0', torch.Size([10])), ('bias_hh_l0', torch.Size([10])), ('weight_ih_l0_reverse', torch.Size([10, 5])), ('weight_hh_l0_reverse', torch.Size([10, 10])), ('bias_ih_l0_reverse', torch.Size([10])), ('bias_hh_l0_reverse', torch.Size([10])), ('weight_ih_l1', torch.Size([10, 20])), ('weight_hh_l1', torch.Size([10, 10])), ('bias_ih_l1', torch.Size([10])), ('bias_hh_l1', torch.Size([10])), ('weight_ih_l1_reverse', torch.Size([10, 20])), ('weight_hh_l1_reverse', torch.Size([10, 10])), ('bias_ih_l1_reverse', torch.Size([10])), ('bias_hh_l1_reverse', torch.Size([10]))]
torch.nn.LSTMCell(input_size, hidden_size, bias=True)
i
=
σ
(
W
i
i
x
+
b
i
i
+
W
h
i
h
+
b
h
i
)
f
=
σ
(
W
i
f
x
+
b
i
f
+
W
h
f
h
+
b
h
f
)
g
=
tanh
(
W
i
g
x
+
b
i
g
+
W
h
g
h
+
b
h
g
)
o
=
σ
(
W
i
o
x
+
b
i
o
+
W
h
o
h
+
b
h
o
)
c
′
=
f
∗
c
+
i
∗
g
h
′
=
o
∗
tanh
(
c
′
)
输入:input,(h0,c0) ,后两个默认为全0
输出:
参数:
lstm_cell = torch.nn.LSTMCell(5,10)
input = torch.randn(2,5)
h_0 = torch.randn(2,10)
c_0 = torch.randn(2,10)
h1,c1 = lstm_cell(input,(h_0,c_0))
print(h1.shape,c1.shape)
>>torch.Size([2, 10]) torch.Size([2, 10])
[(para[0],para[1].shape) for para in list(lstm_cell.named_parameters())]
>>[('weight_ih', torch.Size([40, 5])),
('weight_hh', torch.Size([40, 10])),
('bias_ih', torch.Size([40])),
('bias_hh', torch.Size([40]))]
torch.nn.LSTM(*args, **kwargs)
i
t
=
σ
(
W
i
i
x
t
+
b
i
i
+
W
h
i
h
(
t
−
1
)
+
b
h
i
)
f
t
=
σ
(
W
i
f
x
t
+
b
i
f
+
W
h
f
h
(
t
−
1
)
+
b
h
f
)
g
t
=
tanh
(
W
i
g
x
t
+
b
i
g
+
W
h
g
h
(
t
−
1
)
+
b
h
g
)
o
t
=
σ
(
W
i
o
x
t
+
b
i
o
+
W
h
g
h
(
t
−
1
)
+
b
h
o
)
c
t
=
f
t
∗
c
(
t
−
1
)
+
i
t
∗
g
t
h
t
=
o
t
∗
tanh
(
c
t
)
input_size:输入数据X的特征值的数目。
hidden_size:隐藏层的神经元数量,也就是隐藏层的特征数量。
num_layers:循环神经网络的层数,默认值是 1。
bias:默认为 True,如果为 false 则表示神经元不使用 $bias_{ih} 和 和 和bias_{hh}$偏移参数。
batch_first:如果设置为 True,则输入数据的维度中第一个维度就 是 batch 值,默认为 False。默认情况下第一个维度是序列的长度, 第二个维度才是 - - batch,第三个维度是特征数目。
dropout:如果不为空,则表示最后跟一个 dropout 层抛弃部分数据,抛弃数据的比例由该参数指定。默认为0。
bidirectional : If True
, becomes a bidirectional RNN. Default: False
输入:input,(h0,c0) ,后两个默认为全0
输出:
参数:
lstm = torch.nn.LSTM(5, 10, 2) #seq长度4,batch_size=2 input = torch.randn(4 , 2 , 5) h_0 =torch.randn(2 , 2 , 10) c_0 =torch.randn(2 , 2 , 10) output,(hn,cn)=lstm(input ,(h_0,c_0)) output.shape,hn.shape,cn.shape >>(torch.Size([4, 2, 10]), torch.Size([2, 2, 10]), torch.Size([2, 2, 10])) [(para[0],para[1].shape) for para in list(lstm.named_parameters())] >>[('weight_ih_l0', torch.Size([40, 5])), ('weight_hh_l0', torch.Size([40, 10])), ('bias_ih_l0', torch.Size([40])), ('bias_hh_l0', torch.Size([40])), ('weight_ih_l1', torch.Size([40, 10])), ('weight_hh_l1', torch.Size([40, 10])), ('bias_ih_l1', torch.Size([40])), ('bias_hh_l1', torch.Size([40]))]
torch.nn.GRUCell(input_size, hidden_size, bias=True)
r
=
σ
(
W
i
r
x
+
b
i
r
+
W
h
r
h
+
b
h
r
)
z
=
σ
(
W
i
r
x
+
b
i
z
+
W
h
z
h
+
b
h
z
)
n
=
tanh
(
W
i
n
x
+
b
i
n
+
r
∗
(
W
h
n
h
+
b
h
n
)
)
h
′
=
(
1
−
z
)
∗
n
+
z
∗
h
输入:
输出:
参数:
gru_cell = torch.nn.GRUCell(5,10)
input = torch.randn(2,5)
h_0 = torch.randn(2,10)
h1= gru_cell(input,h_0)
print(h1.shape)
>>torch.Size([2, 10])
[(para[0],para[1].shape) for para in list(gru_cell.named_parameters())]
>>[('weight_ih', torch.Size([30, 5])),
('weight_hh', torch.Size([30, 10])),
('bias_ih', torch.Size([30])),
('bias_hh', torch.Size([30]))]
torch.nn.GRU(*args,**kwargs)
r
t
=
σ
(
W
i
r
x
t
+
b
i
r
+
W
h
r
h
(
t
−
1
)
+
b
h
r
)
z
t
=
σ
(
W
i
z
x
t
+
b
i
z
+
W
h
z
h
(
t
−
1
)
+
b
h
z
)
n
t
=
tanh
(
W
i
n
x
t
+
b
i
n
+
r
t
∗
(
W
h
n
h
(
t
−
1
)
+
b
h
n
)
)
h
t
=
(
1
−
z
t
)
∗
n
t
+
z
t
∗
h
(
t
−
1
)
input_size:输入数据X的特征值的数目。
hidden_size:隐藏层的神经元数量,也就是隐藏层的特征数量。
num_layers:循环神经网络的层数,默认值是 1。
bias:默认为 True,如果为 false 则表示神经元不使用 b i a s i h bias_{ih} biasih和 b i a s h h bias_{hh} biashh偏移参数。
batch_first:如果设置为 True,则输入数据的维度中第一个维度就 是 batch 值,默认为 False。默认情况下第一个维度是序列的长度, 第二个维度才是 - - batch,第三个维度是特征数目。
dropout:如果不为空,则表示最后跟一个 dropout 层抛弃部分数据,抛弃数据的比例由该参数指定。默认为0。
bidirectional : If True
, becomes a bidirectional RNN. Default: False
输入:
输出:
output: [seq_len, batch, num_directions * hidden_size]
h n h_{n} hn: [num_layers * num_directions, batch, hidden_size]
参数:
gru = torch.nn.GRU(5,10,2) input = torch.randn(4,2,5) h_0 = torch.randn(2,2,10) output,h1= gru(input,h_0) print(output.shape,h1.shape) >>torch.Size([4, 2, 10]) torch.Size([2, 2, 10]) [(para[0],para[1].shape) for para in list(gru.named_parameters())] >>[('weight_ih_l0', torch.Size([30, 5])), ('weight_hh_l0', torch.Size([30, 10])), ('bias_ih_l0', torch.Size([30])), ('bias_hh_l0', torch.Size([30])), ('weight_ih_l1', torch.Size([30, 10])), ('weight_hh_l1', torch.Size([30, 10])), ('bias_ih_l1', torch.Size([30])), ('bias_hh_l1', torch.Size([30]))]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。